blob: 918a731f5fa0308d3aa75cf167893a5df370dd0b [file] [log] [blame]
<?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>EntityQuery.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> &gt; <a href="index.source.html" class="el_package">org.apache.juddi.query</a> &gt; <span class="el_source">EntityQuery.java</span></div><h1>EntityQuery.java</h1><pre class="source lang-java linenums">/*
* Copyright 2001-2008 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
* 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 &quot;AS IS&quot; 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.List;
import java.util.ArrayList;
import java.util.Collections;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.uddi.api_v3.ListDescription;
import org.apache.juddi.config.AppConfig;
import org.apache.juddi.config.Property;
import org.apache.juddi.model.TempKey;
import org.apache.juddi.query.util.DynamicQuery;
/**
* @author &lt;a href=&quot;mailto:jfaath@apache.org&quot;&gt;Jeff Faath&lt;/a&gt;
*/
<span class="fc" id="L39">public abstract class EntityQuery {</span>
<span class="fc" id="L40"> private static Log log = LogFactory.getLog(EntityQuery.class);</span>
public static final String KEY_NAME = &quot;entityKey&quot;;
public static final String TEMP_ENTITY_NAME = &quot;TempKey&quot;;
public static final String TEMP_ENTITY_ALIAS = &quot;tk&quot;;
public static final String TEMP_ENTITY_PK_TXID_NAME = TEMP_ENTITY_ALIAS + &quot;.pk.txId&quot;;
public static final String TEMP_ENTITY_PK_KEY_NAME = TEMP_ENTITY_ALIAS + &quot;.pk.entityKey&quot;;
public static final String SIGNATURE_FIELD = &quot;signatures&quot;;
public static final int DEFAULT_MAXROWS = 1000;
public static final int DEFAULT_MAXINCLAUSE = 1000;
// TODO: make this alias creator a little more unique
public static String buildAlias(String entityName) {
<span class="pc bpc" id="L54" title="2 of 4 branches missed."> if (entityName == null || entityName.length() == 0)</span>
<span class="nc" id="L55"> return &quot;x&quot;;</span>
<span class="fc" id="L57"> return entityName.substring(0, entityName.length() - 3) + &quot;_&quot;;</span>
}
/*
* Used to retrieve the final results of find operations. Handles paging as specified by user.
*
* TODO: This query will use an IN clause, however, it is not restricted per the global parameter. This is so the query can
* take advantage of sorting through SQL. The fix would be to apply the parameter, but only if the IN list is greater than the
* parameter. In this case, sorting would have to be done in java.
*
*/
public static List&lt;?&gt; getPagedResult(EntityManager em, DynamicQuery dynamicQry, Integer maxRowsUser, Integer listHead, ListDescription listDesc) {
<span class="fc" id="L70"> int maxRows = DEFAULT_MAXROWS;</span>
try {
<span class="fc" id="L72"> maxRows = AppConfig.getConfiguration().getInteger(Property.JUDDI_MAX_ROWS, DEFAULT_MAXROWS);</span>
}
<span class="nc" id="L74"> catch(ConfigurationException ce) {</span>
<span class="nc" id="L75"> log.error(&quot;Configuration exception occurred retrieving: &quot; + Property.JUDDI_MAX_ROWS);</span>
<span class="fc" id="L76"> }</span>
<span class="pc bpc" id="L78" title="1 of 4 branches missed."> if (maxRowsUser != null &amp;&amp; maxRowsUser &gt; 0) {</span>
<span class="pc bpc" id="L79" title="1 of 2 branches missed."> if (maxRowsUser &lt; maxRows)</span>
<span class="fc" id="L80"> maxRows = maxRowsUser;</span>
}
<span class="pc bpc" id="L83" title="1 of 4 branches missed."> if (listHead == null || listHead &lt;= 0)</span>
<span class="fc" id="L84"> listHead = 1;</span>
<span class="fc" id="L87"> Query qry = dynamicQry.buildJPAQuery(em);</span>
<span class="fc" id="L88"> List&lt;Object&gt; result = new ArrayList&lt;Object&gt;();</span>
//Filter out non-unique results
<span class="fc bfc" id="L90" title="All 2 branches covered."> for (Object object : qry.getResultList()) {</span>
<span class="fc bfc" id="L91" title="All 2 branches covered."> if (!result.contains(object)) {</span>
<span class="fc" id="L92"> result.add(object);</span>
}
<span class="fc" id="L94"> }</span>
<span class="fc" id="L96"> int resultSize = result.size();</span>
<span class="pc bpc" id="L98" title="1 of 2 branches missed."> if (listDesc != null) {</span>
<span class="fc" id="L99"> listDesc.setActualCount(resultSize);</span>
<span class="fc" id="L100"> listDesc.setListHead(listHead);</span>
}
<span class="fc" id="L103"> int startIndex = listHead - 1;</span>
<span class="pc bpc" id="L104" title="1 of 2 branches missed."> if (startIndex &gt;= resultSize) {</span>
<span class="nc bnc" id="L105" title="All 2 branches missed."> if (listDesc != null)</span>
<span class="nc" id="L106"> listDesc.setIncludeCount(0);</span>
<span class="nc" id="L108"> return Collections.emptyList();</span>
}
else {
<span class="fc" id="L111"> int endIndex = Math.min(startIndex + maxRows, resultSize);</span>
<span class="pc bpc" id="L112" title="1 of 2 branches missed."> if (listDesc != null)</span>
<span class="fc" id="L113"> listDesc.setIncludeCount(endIndex - startIndex);</span>
<span class="fc" id="L115"> List&lt;Object&gt; subList = new ArrayList&lt;Object&gt;(endIndex);</span>
<span class="fc bfc" id="L116" title="All 2 branches covered."> for (int i=startIndex; i&lt; endIndex; i++) {</span>
<span class="fc" id="L117"> subList.add(result.get(i));</span>
}
<span class="fc" id="L119"> return subList;</span>
}
}
/*
* Used for all the find operation sub-queries. Restricts size of the IN clause based on global parameter
*/
@SuppressWarnings(&quot;unchecked&quot;)
public static List&lt;Object&gt; getQueryResult(EntityManager em, DynamicQuery dynamicQry, List&lt;?&gt; keysIn, String inListTerm) {
<span class="fc" id="L129"> List&lt;Object&gt; result = new ArrayList&lt;Object&gt;(0);</span>
// If keysIn is null, then no IN list is applied to the query - we simply need to run the query. Otherwise, the IN list is chunked based on
// the application property.
<span class="fc bfc" id="L132" title="All 2 branches covered."> if (keysIn == null) {</span>
<span class="pc bpc" id="L133" title="1 of 2 branches missed."> if (log.isDebugEnabled()) log.debug(dynamicQry);</span>
<span class="fc" id="L134"> Query qry = dynamicQry.buildJPAQuery(em);</span>
<span class="fc" id="L135"> result = qry.getResultList();</span>
<span class="fc" id="L136"> }</span>
else {
<span class="fc" id="L138"> int maxInClause = DEFAULT_MAXINCLAUSE;</span>
try {
<span class="fc" id="L140"> maxInClause = AppConfig.getConfiguration().getInteger(Property.JUDDI_MAX_IN_CLAUSE, DEFAULT_MAXINCLAUSE);</span>
}
<span class="nc" id="L142"> catch(ConfigurationException ce) {</span>
<span class="nc" id="L143"> log.error(&quot;Configuration exception occurred retrieving: &quot; + Property.JUDDI_MAX_IN_CLAUSE);</span>
<span class="fc" id="L144"> }</span>
<span class="fc bfc" id="L145" title="All 2 branches covered."> if (keysIn.isEmpty()) {</span>
<span class="fc" id="L146"> Query qry = dynamicQry.buildJPAQuery(em);</span>
<span class="fc" id="L147"> List&lt;Object&gt; resultChunk = qry.getResultList();</span>
<span class="fc" id="L148"> result.addAll(resultChunk);</span>
<span class="fc" id="L149"> } else {</span>
<span class="fc" id="L150"> int inParamsLeft = keysIn.size();</span>
<span class="fc" id="L151"> int startIndex = 0;</span>
<span class="fc bfc" id="L152" title="All 2 branches covered."> while(inParamsLeft &gt; 0) {</span>
<span class="fc" id="L153"> int endIndex = startIndex + Math.min(inParamsLeft, maxInClause);</span>
<span class="fc" id="L155"> List&lt;Object&gt; subKeysIn = new ArrayList&lt;Object&gt;(endIndex);</span>
<span class="fc bfc" id="L156" title="All 2 branches covered."> for (int i=startIndex; i&lt; endIndex; i++) {</span>
<span class="fc" id="L157"> subKeysIn.add(keysIn.get(i));</span>
}
<span class="fc" id="L159"> dynamicQry.appendInListWithAnd(inListTerm, subKeysIn);</span>
<span class="fc" id="L160"> log.debug(dynamicQry);</span>
<span class="fc" id="L162"> Query qry = dynamicQry.buildJPAQuery(em);</span>
<span class="fc" id="L163"> List&lt;Object&gt; resultChunk = qry.getResultList();</span>
<span class="fc" id="L164"> result.addAll(resultChunk);</span>
<span class="fc" id="L166"> inParamsLeft = inParamsLeft - (endIndex - startIndex);</span>
<span class="fc" id="L167"> startIndex = endIndex;</span>
<span class="fc" id="L168"> }</span>
}
}
<span class="fc" id="L172"> return result;</span>
}
public static void storeIntermediateKeySetResults (EntityManager em, String txId, List&lt;?&gt; keysIn) {
<span class="nc bnc" id="L179" title="All 2 branches missed."> for (Object key : keysIn) {</span>
<span class="nc" id="L180"> TempKey tempKey = new TempKey();</span>
<span class="nc" id="L181"> tempKey.setPk(txId,key.toString());</span>
<span class="nc" id="L182"> em.persist(tempKey);</span>
<span class="nc" id="L183"> }</span>
<span class="nc" id="L184"> }</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>