| <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>FindEntityByCategoryQuery.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">jUDDI Core Services</a> > <a href="index.source.html" class="el_package">org.apache.juddi.query</a> > <span class="el_source">FindEntityByCategoryQuery.java</span></div><h1>FindEntityByCategoryQuery.java</h1><pre class="source lang-java linenums">/* |
| * Copyright 2001-2008 The Apache Software Foundation. |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| * |
| */ |
| |
| package org.apache.juddi.query; |
| |
| import java.util.ArrayList; |
| import java.util.Collections; |
| import java.util.List; |
| |
| import javax.persistence.EntityManager; |
| |
| import org.apache.commons.logging.Log; |
| import org.apache.commons.logging.LogFactory; |
| import org.apache.juddi.config.Constants; |
| import org.apache.juddi.query.util.DynamicQuery; |
| import org.apache.juddi.query.util.FindQualifiers; |
| import org.apache.juddi.query.util.KeyedRefTModelComparator; |
| import org.uddi.api_v3.CategoryBag; |
| import org.uddi.api_v3.KeyedReference; |
| |
| /** |
| * Returns the list of "entity" keys possessing the keyedReferences in the passed category bag. |
| * Output is restricted by list of "entity" keys passed in. If null, all entities are searched. |
| * Output is produced by building the appropriate JPA query based on input and find qualifiers. |
| * |
| * NOTES: |
| * 1) Categories are grouped with a logical AND by default. |
| * 2) Concerning when the categories are AND'd together - the only way this can be done with a single query was to create a self-join for |
| * each category. If there are a lot of categories, the performance could suffer. |
| * TODO: Test performance with multiple AND'd categories. If too slow, look to process this query in multiple steps. |
| * 3) The "orLikeKeys" qualifier complicates matters. The "like" keys are OR'd together and these groups of "like" keys are AND'd together. |
| * As with "andAllKeys", self-joins are created but only one for each group of "like" keys. If none of the keyedReferences passed are alike then this |
| * will reduce to an "andAllKeys" query. If all are alike, then this will query will exhibit the behavior of OR'ing all keys. |
| * |
| * @author <a href="mailto:jfaath@apache.org">Jeff Faath</a> |
| */ |
| public class FindEntityByCategoryQuery extends EntityQuery { |
| |
| @SuppressWarnings("unused") |
| <span class="fc" id="L54"> private final static Log log = LogFactory.getLog(FindEntityByCategoryQuery.class);</span> |
| |
| private static final String ENTITY_KEYEDREFERENCE = "KeyedReference"; |
| <span class="fc" id="L57"> private static final String ALIAS_KEYEDREFERENCE = buildAlias(ENTITY_KEYEDREFERENCE);</span> |
| private static final String FIELD_CATEGORYBAG = "categoryBag"; |
| |
| protected String entityName; |
| protected String entityAlias; |
| protected String keyName; |
| protected String entityField; |
| protected String entityNameChild; |
| protected String entityAliasChild; |
| protected String selectSQL; |
| protected String signaturePresent; |
| |
| public FindEntityByCategoryQuery(String entityName, String entityAlias, String keyName, |
| <span class="fc" id="L70"> String entityField, String entityNameChild, String signaturePresent) {</span> |
| <span class="fc" id="L71"> this.entityName = entityName;</span> |
| <span class="fc" id="L72"> this.entityAlias = entityAlias;</span> |
| <span class="fc" id="L73"> this.keyName = keyName;</span> |
| <span class="fc" id="L74"> this.entityField = entityField;</span> |
| <span class="fc" id="L75"> this.entityNameChild = entityNameChild;</span> |
| <span class="fc" id="L76"> this.entityAliasChild = buildAlias(entityNameChild);</span> |
| <span class="fc" id="L77"> this.signaturePresent = signaturePresent;</span> |
| |
| <span class="fc" id="L79"> StringBuffer sql = new StringBuffer(200);</span> |
| <span class="fc" id="L80"> sql.append("select distinct " + entityAlias + "." + keyName + " from " + entityName + " " + entityAlias + " , " + entityNameChild + " " + entityAliasChild + " ");</span> |
| <span class="fc" id="L81"> selectSQL = sql.toString();</span> |
| <span class="fc" id="L82"> }</span> |
| |
| public String getEntityName() { |
| <span class="nc" id="L85"> return entityName;</span> |
| } |
| |
| public String getEntityAlias() { |
| <span class="nc" id="L89"> return entityAlias;</span> |
| } |
| |
| public String getKeyName() { |
| <span class="nc" id="L93"> return keyName;</span> |
| } |
| |
| public String getEntityField() { |
| <span class="nc" id="L97"> return entityField;</span> |
| } |
| |
| public String getEntityNameChild() { |
| <span class="nc" id="L101"> return entityNameChild;</span> |
| } |
| |
| public String getEntityAliasChild() { |
| <span class="nc" id="L105"> return entityAliasChild;</span> |
| } |
| |
| public String getSelectSQL() { |
| <span class="nc" id="L109"> return selectSQL;</span> |
| } |
| |
| public String getSignaturePresent() { |
| <span class="fc" id="L113"> return signaturePresent;</span> |
| } |
| |
| public void setSignaturePresent(String signaturePresent) { |
| <span class="nc" id="L117"> this.signaturePresent = signaturePresent;</span> |
| <span class="nc" id="L118"> }</span> |
| |
| public List<Object> select(EntityManager em, FindQualifiers fq, CategoryBag categoryBag, List<Object> keysIn, DynamicQuery.Parameter... restrictions) { |
| // If keysIn is not null and empty, then search is over. |
| <span class="fc bfc" id="L122" title="All 4 branches covered."> if ((keysIn != null) && (keysIn.size() == 0))</span> |
| <span class="fc" id="L123"> return keysIn;</span> |
| |
| <span class="fc bfc" id="L125" title="All 2 branches covered."> if (categoryBag == null)</span> |
| <span class="fc" id="L126"> return keysIn;</span> |
| |
| <span class="fc" id="L128"> List<KeyedReference> categories = categoryBag.getKeyedReference();</span> |
| <span class="pc bpc" id="L129" title="2 of 4 branches missed."> if (categories == null || categories.size() == 0)</span> |
| <span class="nc" id="L130"> return keysIn;</span> |
| |
| <span class="fc" id="L132"> List<KeyedReference> keyedRefs = new ArrayList<KeyedReference>(0);</span> |
| <span class="fc bfc" id="L133" title="All 2 branches covered."> for (KeyedReference elem : categories) {</span> |
| <span class="pc bpc" id="L134" title="1 of 2 branches missed."> if (elem instanceof KeyedReference)</span> |
| <span class="fc" id="L135"> keyedRefs.add((KeyedReference)elem);</span> |
| <span class="fc" id="L136"> }</span> |
| <span class="pc bpc" id="L137" title="1 of 2 branches missed."> if (keyedRefs.isEmpty())</span> |
| <span class="nc" id="L138"> return keysIn; </span> |
| |
| <span class="fc" id="L140"> DynamicQuery dynamicQry = new DynamicQuery(selectSQL);</span> |
| <span class="fc" id="L141"> appendConditions(dynamicQry, fq, keyedRefs);</span> |
| <span class="pc bpc" id="L142" title="1 of 4 branches missed."> if (restrictions != null && restrictions.length > 0)</span> |
| <span class="fc" id="L143"> dynamicQry.AND().pad().appendGroupedAnd(restrictions);</span> |
| |
| <span class="fc" id="L145"> return getQueryResult(em, dynamicQry, keysIn, entityAlias + "." + keyName);</span> |
| } |
| |
| |
| /* |
| * Appends the conditions to the query based on the keyedReference list. With the default or when "orAllKeys" is passed, the keyedReferences are autonomous and are |
| * all AND'd or OR'd respectively. However, "orLikeKeys" requires special treatment. The goal is to create the conditions in this format: |
| * |
| * (likeKey1 = X or likeKey1 = Y) and (likeKey2 = A or likeKey2 = B or likeKey2 = C) |
| * |
| * ie. the "like" KeyedReferences are OR'd and the groups of "like" KeyedReferences are AND'd with each other. |
| */ |
| public void appendConditions(DynamicQuery qry, FindQualifiers fq, List<KeyedReference> keyedRefs) { |
| |
| // Append the necessary tables (two will always be added connecting the entity to its category table and then the category table to the keyed references). |
| <span class="fc" id="L160"> appendJoinTables(qry, fq, keyedRefs);</span> |
| <span class="fc" id="L161"> qry.AND().pad().openParen().pad();</span> |
| |
| <span class="fc" id="L163"> String predicate = DynamicQuery.PREDICATE_EQUALS;</span> |
| <span class="fc bfc" id="L164" title="All 2 branches covered."> if (fq.isApproximateMatch()) {</span> |
| <span class="fc" id="L165"> predicate = DynamicQuery.PREDICATE_LIKE;</span> |
| } |
| |
| // Sorting the collection by tModel Key |
| <span class="fc" id="L169"> Collections.sort(keyedRefs, new KeyedRefTModelComparator());</span> |
| |
| <span class="fc" id="L171"> String prevTModelKey = null;</span> |
| <span class="fc" id="L172"> int count = 0;</span> |
| <span class="fc" id="L173"> int tblCount = -1;</span> |
| <span class="fc bfc" id="L174" title="All 2 branches covered."> for(KeyedReference keyedRef : keyedRefs) {</span> |
| <span class="fc" id="L175"> String tmodelKey = keyedRef.getTModelKey();</span> |
| <span class="fc" id="L176"> String keyValue = keyedRef.getKeyValue();</span> |
| <span class="fc" id="L177"> String keyName = keyedRef.getKeyName();</span> |
| |
| <span class="fc bfc" id="L179" title="All 2 branches covered."> if (fq.isApproximateMatch()) {</span> |
| // JUDDI-235: wildcards are provided by user (only commenting in case a new interpretation arises) |
| //keyValue = keyValue.endsWith(DynamicQuery.WILDCARD)?keyValue:keyValue + DynamicQuery.WILDCARD; |
| //keyName = keyName.endsWith(DynamicQuery.WILDCARD)?keyName:keyName + DynamicQuery.WILDCARD; |
| } |
| |
| // Either opening up (and AND'ing) a new "group" of like keys or simply appending an "or". If this is not "orLikeKeys", then just need to increment |
| // the table count. |
| <span class="fc bfc" id="L187" title="All 2 branches covered."> if (fq.isOrLikeKeys()) {</span> |
| <span class="fc bfc" id="L188" title="All 2 branches covered."> if (count == 0) {</span> |
| <span class="fc" id="L189"> qry.openParen().pad();</span> |
| <span class="fc" id="L190"> tblCount++;</span> |
| } |
| else { |
| <span class="pc bpc" id="L193" title="1 of 2 branches missed."> if (!tmodelKey.equals(prevTModelKey)) {</span> |
| <span class="nc" id="L194"> qry.closeParen().pad().AND().pad().openParen().pad();</span> |
| <span class="nc" id="L195"> tblCount++;</span> |
| } |
| else |
| <span class="fc" id="L198"> qry.OR().pad();</span> |
| } |
| } |
| else |
| <span class="fc" id="L202"> tblCount++;</span> |
| |
| <span class="fc bfc" id="L204" title="All 2 branches covered."> String keyValueTerm = (fq.isOrAllKeys()?ALIAS_KEYEDREFERENCE + "0":ALIAS_KEYEDREFERENCE + tblCount) + ".keyValue";</span> |
| <span class="fc bfc" id="L205" title="All 2 branches covered."> String keyNameTerm = (fq.isOrAllKeys()?ALIAS_KEYEDREFERENCE + "0":ALIAS_KEYEDREFERENCE + tblCount) + ".keyName";</span> |
| <span class="fc bfc" id="L206" title="All 2 branches covered."> String tmodelKeyTerm = (fq.isOrAllKeys()?ALIAS_KEYEDREFERENCE + "0":ALIAS_KEYEDREFERENCE + tblCount) + ".tmodelKeyRef";</span> |
| <span class="fc bfc" id="L207" title="All 2 branches covered."> if (fq.isCaseInsensitiveMatch()) {</span> |
| <span class="fc" id="L208"> keyValueTerm = "upper(" + keyValueTerm + ")";</span> |
| <span class="fc" id="L209"> keyValue = keyValue.toUpperCase();</span> |
| |
| <span class="fc" id="L211"> keyNameTerm = "upper(" + keyNameTerm + ")";</span> |
| <span class="fc" id="L212"> keyName = keyName.toUpperCase();</span> |
| } |
| |
| |
| // According to specification, if the "general keyword" tmodel is used, then the keyName must be part of the query. |
| <span class="pc bpc" id="L217" title="1 of 2 branches missed."> if (Constants.GENERAL_KEYWORD_TMODEL.equalsIgnoreCase(tmodelKey)) {</span> |
| <span class="nc" id="L218"> qry.appendGroupedAnd(new DynamicQuery.Parameter(tmodelKeyTerm, tmodelKey, DynamicQuery.PREDICATE_EQUALS),</span> |
| new DynamicQuery.Parameter(keyValueTerm, keyValue, predicate), |
| new DynamicQuery.Parameter(keyNameTerm, keyName, predicate)); |
| } |
| else { |
| <span class="fc" id="L223"> qry.appendGroupedAnd(new DynamicQuery.Parameter(tmodelKeyTerm, tmodelKey, DynamicQuery.PREDICATE_EQUALS),</span> |
| new DynamicQuery.Parameter(keyValueTerm, keyValue, predicate)); |
| |
| } |
| |
| <span class="fc bfc" id="L228" title="All 2 branches covered."> if (count + 1 < keyedRefs.size()) {</span> |
| <span class="pc bpc" id="L229" title="1 of 2 branches missed."> if (fq.isOrAllKeys())</span> |
| <span class="nc" id="L230"> qry.OR().pad();</span> |
| <span class="fc bfc" id="L231" title="All 2 branches covered."> else if (fq.isOrLikeKeys()) {</span> |
| } |
| else |
| <span class="fc" id="L234"> qry.AND().pad();</span> |
| } |
| |
| // The "orLikeKeys" will always leave an unclosed parenthesis. This will close it. |
| <span class="fc bfc" id="L238" title="All 4 branches covered."> if (fq.isOrLikeKeys() && (count + 1 == keyedRefs.size()))</span> |
| <span class="fc" id="L239"> qry.closeParen().pad();</span> |
| |
| <span class="fc" id="L241"> prevTModelKey = tmodelKey;</span> |
| <span class="fc" id="L242"> count++;</span> |
| <span class="fc" id="L243"> }</span> |
| <span class="fc" id="L244"> qry.closeParen().pad();</span> |
| |
| <span class="fc" id="L246"> }</span> |
| |
| |
| |
| /* |
| * Appends the necessary join table for the child entity and additional tables for when keys are AND'd. When "orLikeKeys" is used, |
| * we only need an extra table for each distinct tmodelKey. |
| */ |
| public void appendJoinTables(DynamicQuery qry, FindQualifiers fq, List<KeyedReference> keyedRefs) { |
| |
| <span class="pc bpc" id="L256" title="2 of 4 branches missed."> if (keyedRefs != null && keyedRefs.size() > 0) {</span> |
| // Sorting the collection by tModel Key |
| <span class="fc" id="L258"> Collections.sort(keyedRefs, new KeyedRefTModelComparator());</span> |
| |
| <span class="fc" id="L260"> StringBuffer thetaJoins = new StringBuffer(200);</span> |
| <span class="fc" id="L261"> int tblCount = 0;</span> |
| <span class="fc" id="L262"> int count = 0;</span> |
| <span class="fc" id="L263"> String curTModelKey = null;</span> |
| <span class="fc" id="L264"> String prevTModelKey = null;</span> |
| <span class="fc bfc" id="L265" title="All 2 branches covered."> for(KeyedReference kr : keyedRefs) {</span> |
| <span class="fc" id="L266"> curTModelKey = kr.getTModelKey();</span> |
| <span class="fc bfc" id="L267" title="All 2 branches covered."> if (count != 0) {</span> |
| <span class="pc bpc" id="L268" title="1 of 2 branches missed."> if (!fq.isOrAllKeys()) {</span> |
| <span class="pc bpc" id="L269" title="1 of 4 branches missed."> if (fq.isOrLikeKeys() && curTModelKey.equals(prevTModelKey)) {</span> |
| // Do nothing |
| } |
| else { |
| <span class="fc" id="L273"> tblCount++;</span> |
| <span class="fc" id="L274"> qry.comma().pad().append(ENTITY_KEYEDREFERENCE + " " + ALIAS_KEYEDREFERENCE + tblCount).pad();</span> |
| <span class="fc" id="L275"> thetaJoins.append(ALIAS_KEYEDREFERENCE + (tblCount - 1) + "." + FIELD_CATEGORYBAG + ".id = " + ALIAS_KEYEDREFERENCE + tblCount + "." + FIELD_CATEGORYBAG + ".id ");</span> |
| <span class="fc" id="L276"> thetaJoins.append(DynamicQuery.OPERATOR_AND + " ");</span> |
| } |
| } |
| |
| } |
| else { |
| <span class="fc" id="L282"> qry.comma().pad().append(ENTITY_KEYEDREFERENCE + " " + ALIAS_KEYEDREFERENCE + tblCount).pad();</span> |
| <span class="fc" id="L283"> thetaJoins.append(entityAliasChild + ".id = " + ALIAS_KEYEDREFERENCE + tblCount + "." + FIELD_CATEGORYBAG + ".id ");</span> |
| <span class="fc" id="L284"> thetaJoins.append(DynamicQuery.OPERATOR_AND + " ");</span> |
| } |
| <span class="fc" id="L286"> prevTModelKey = curTModelKey;</span> |
| <span class="fc" id="L287"> count++;</span> |
| <span class="fc" id="L288"> }</span> |
| |
| <span class="fc" id="L290"> qry.WHERE().pad().openParen().pad();</span> |
| |
| // Appending the middling entity-specific category table condition |
| <span class="fc" id="L293"> qry.append(entityAlias + "." + keyName + " = " + entityAliasChild + "." + entityField + "." + KEY_NAME).pad();</span> |
| <span class="fc" id="L294"> qry.AND().pad();</span> |
| |
| <span class="fc" id="L296"> String thetaJoinsStr = thetaJoins.toString();</span> |
| <span class="pc bpc" id="L297" title="1 of 2 branches missed."> if (thetaJoinsStr.endsWith(DynamicQuery.OPERATOR_AND + " "))</span> |
| <span class="fc" id="L298"> thetaJoinsStr = thetaJoinsStr.substring(0, thetaJoinsStr.length() - (DynamicQuery.OPERATOR_AND + " ").length());</span> |
| <span class="fc" id="L299"> qry.append(thetaJoinsStr);</span> |
| |
| <span class="fc" id="L301"> qry.closeParen().pad();</span> |
| <span class="pc bpc" id="L302" title="1 of 4 branches missed."> if (fq!=null && fq.isSignaturePresent()) {</span> |
| <span class="fc" id="L303"> qry.AND().pad().openParen().pad().append(getSignaturePresent()).pad().closeParen().pad();</span> |
| } |
| } |
| <span class="fc" id="L306"> }</span> |
| |
| } |
| </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.7.9.201702052155</span></div></body></html> |