Last active
August 29, 2015 14:14
-
-
Save novoj/86cb829359265bb9ddb9 to your computer and use it in GitHub Desktop.
SQL extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<bean id="newsfeedMetadataManager" class="com.fg.metadata.business.MetadataManagerFactory"> | |
<property name="metadataConfiguration" value="classpath:/META-INF/lib_newsfeed/metadata/metadataDescriptor.xml"/> | |
<property name="dataSource" ref="dataSource"/> | |
<property name="additionalConstraintTranslators"> | |
<list> | |
<bean class="com.fg.newsfeed.model.query.ExclusionTranslator"/> | |
</list> | |
</property> | |
<property name="sqlQueryInterceptors"> | |
<list> | |
<bean class="com.fg.newsfeed.dao.CustomNewsfeedExclusionSqlQuery"/> | |
<bean class="com.fg.newsfeed.dao.CustomNewsfeedCommentExclusionSqlQuery"/> | |
</list> | |
</property> | |
</bean> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.fg.newsfeed.dao; | |
import com.fg.metadata.business.SqlContainerQueryInterceptor; | |
import com.fg.metadata.business.SqlOneToManyBundleQueryInterceptor; | |
import com.fg.metadata.business.dto.ContainerId; | |
import com.fg.metadata.model.config.MetadataBundleDefinition; | |
import com.fg.metadata.model.config.MetadataContainerDefinition; | |
import com.fg.metadata.model.query.AdamQuery; | |
import com.fg.metadata.storage.model.Join; | |
import com.fg.metadata.storage.model.QueryDecomposition; | |
import com.fg.metadata.storage.model.WhereClausule; | |
import com.fg.newsfeed.model.NewsfeedItem; | |
import com.fg.newsfeed.model.query.ExceptExcluded; | |
import com.fg.query.util.QueryUtils; | |
import java.util.Arrays; | |
import java.util.List; | |
import static com.fg.metadata.storage.model.Join.JoinType.LEFT_JOIN; | |
import static com.fg.metadata.storage.model.WhereClausule.AppendAsType.AND; | |
/** | |
* This interceptor will add specific virtual join that excludes newsfeed items comments set by specified user. | |
* | |
* @author Jan Novotný, FG Forrest a.s. (c) 2007 | |
* @version $Id: CustomNewsfeedCommentExclusionSqlQuery.java,v 1.4 2011/04/20 15:21:06 u_novoj Exp $ | |
*/ | |
public class CustomNewsfeedCommentExclusionSqlQuery implements SqlOneToManyBundleQueryInterceptor, SqlContainerQueryInterceptor { | |
/** | |
* This method could analyze and alter query to a 1:N metadata bundle if business logic requires. | |
* | |
* @param sqlQuery | |
* @param bundleDefinition | |
* @param containerIds | |
* @param query | |
*/ | |
public QueryDecomposition intercept(QueryDecomposition sqlQuery, MetadataBundleDefinition bundleDefinition, List<ContainerId> containerIds, AdamQuery query) { | |
MetadataContainerDefinition containerDefinition = bundleDefinition.getParent(); | |
if ("newsfeed".equals(containerDefinition.getContainerType())) { | |
ExceptExcluded except = QueryUtils.findSpecificConstraint( | |
query.getFilteringForBundle(NewsfeedItem.BUNDLE_COMMENTS), | |
ExceptExcluded.class | |
); | |
if (except != null) { | |
addCommentExcludeJoin(sqlQuery, except, bundleDefinition); | |
} | |
} | |
return sqlQuery; | |
} | |
public QueryDecomposition intercept(QueryDecomposition sqlQuery, MetadataContainerDefinition containerDefinition, AdamQuery query) { | |
if ("newsfeedComment".equals(containerDefinition.getContainerType())) { | |
MetadataBundleDefinition bundleDefinition = containerDefinition.getPropertyBundle(containerDefinition.getDefaultBundle()); | |
ExceptExcluded except = QueryUtils.findSpecificConstraint( | |
query.getUnboundFiltering(), | |
ExceptExcluded.class | |
); | |
if (except == null) { | |
except = QueryUtils.findSpecificConstraint( | |
query.getFilteringForBundle(bundleDefinition.getName()), | |
ExceptExcluded.class | |
); | |
} | |
if (except != null) { | |
addCommentExcludeJoin(sqlQuery, except, bundleDefinition); | |
} | |
} | |
return sqlQuery; | |
} | |
private void addCommentExcludeJoin(QueryDecomposition sqlQuery, ExceptExcluded except, MetadataBundleDefinition bundleDefinition) { | |
sqlQuery.addJoin( | |
bundleDefinition, | |
new Join( | |
sqlQuery.getPlatform(), | |
sqlQuery.getContainerTable(), | |
sqlQuery.getContainerPrimaryKeyColumn(), | |
"T_NF_FEED_COMMENT_EXCLUDE", | |
"p", | |
"idNewsfeedComment", | |
LEFT_JOIN, | |
Arrays.asList( | |
new WhereClausule( | |
AND, | |
"p.`idUser` = ?", | |
Arrays.asList(except.getIdUser()) | |
) | |
) | |
) | |
); | |
sqlQuery.addFiltering( | |
new WhereClausule( | |
AND, | |
"p.`id` IS NULL" | |
) | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.fg.newsfeed.dao; | |
import com.fg.metadata.business.SqlContainerQueryInterceptor; | |
import com.fg.metadata.model.config.MetadataBundleDefinition; | |
import com.fg.metadata.model.config.MetadataContainerDefinition; | |
import com.fg.metadata.model.query.AdamQuery; | |
import com.fg.metadata.storage.model.Join; | |
import com.fg.metadata.storage.model.QueryDecomposition; | |
import com.fg.metadata.storage.model.WhereClausule; | |
import com.fg.newsfeed.model.query.ExceptExcluded; | |
import com.fg.query.util.QueryUtils; | |
import java.util.Arrays; | |
import static com.fg.metadata.storage.model.Join.JoinType.LEFT_JOIN; | |
import static com.fg.metadata.storage.model.WhereClausule.AppendAsType.AND; | |
/** | |
* This interceptor will add specific virtual join that excludes newsfeed items set by specified user. | |
* | |
* @author Jan Novotný, FG Forrest a.s. (c) 2007 | |
* @version $Id: CustomNewsfeedExclusionSqlQuery.java,v 1.2 2011/04/20 15:21:06 u_novoj Exp $ | |
*/ | |
public class CustomNewsfeedExclusionSqlQuery implements SqlContainerQueryInterceptor { | |
/** | |
* This method could analyze and alter query if business logic requires. | |
* | |
* @param sqlQuery | |
* @param containerDefinition | |
*/ | |
public QueryDecomposition intercept(QueryDecomposition sqlQuery, MetadataContainerDefinition containerDefinition, AdamQuery query) { | |
if ("newsfeed".equals(containerDefinition.getContainerType())) { | |
MetadataBundleDefinition defaultBundle = containerDefinition.getPropertyBundle(containerDefinition.getDefaultBundle()); | |
ExceptExcluded except = QueryUtils.findSpecificConstraint( | |
query.getFilteringForBundle(defaultBundle.getName()), | |
ExceptExcluded.class | |
); | |
if (except != null) { | |
sqlQuery.addJoin( | |
defaultBundle, | |
new Join( | |
sqlQuery.getPlatform(), | |
sqlQuery.getContainerTable(), | |
sqlQuery.getContainerPrimaryKeyColumn(), | |
"T_NF_FEED_EXCLUDE", | |
"o", | |
"idNewsfeed", | |
LEFT_JOIN, | |
Arrays.asList( | |
new WhereClausule( | |
AND, | |
"o.`idUser` = ?", | |
Arrays.asList(except.getIdUser()) | |
) | |
) | |
) | |
); | |
sqlQuery.addFiltering( | |
new WhereClausule( | |
AND, | |
"o.`id` IS NULL" | |
) | |
); | |
} | |
} | |
return sqlQuery; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.fg.newsfeed.model.query; | |
import com.fg.query.constraint.*; | |
import com.fg.query.exception.*; | |
import com.fg.query.extractor.database.*; | |
/** | |
* Description | |
* | |
* @author Jan Novotný, FG Forrest a.s. (c) 2007 | |
* @version $Id: ExclusionTranslator.java,v 1.1 2010/10/13 11:40:30 u_novoj Exp $ | |
*/ | |
public class ExclusionTranslator implements DatabaseConstraintTranslator { | |
/** | |
* Returns true if this translator is able to process specified constraint. | |
* | |
* @return | |
*/ | |
public boolean canTranslate(Constraint constraint) { | |
return constraint instanceof ExceptExcluded; | |
} | |
/** | |
* Returns true if filter constraint is valid for object in context. | |
* | |
* @param constraint | |
* @return | |
*/ | |
public void translate(Constraint constraint, DatabaseConstraintExtractor databaseConstraintExtractor) throws QueryComposeException { | |
//donothing | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment