Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.kie.remote.services.rest.query;
  
  import static org.jbpm.process.audit.JPAAuditLogService.*;
  import static org.kie.internal.query.QueryParameterIdentifiers.*;
  
  import java.util.HashSet;
  import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 import  org.jbpm.process.audit.JPAAuditLogService;
 import  org.jbpm.services.task.impl.TaskQueryServiceImpl;
 import  org.kie.internal.query.QueryAndParameterAppender;
 import  org.kie.internal.query.QueryModificationService;
 import  org.kie.internal.query.data.QueryData;

This is the QueryModificationService implementation for the REST remote services, which allows us to do complicated queries that join
  • The process instance tables to the variable instance log tables
  • Or the task tables to the variable instance log tables
 
 public class RemoteServicesQueryModificationService implements QueryModificationService {
 
         // default constructor
     }
 
     private static Map<StringClass<?>> taskCriteriaFieldClasses = TaskQueryServiceImpl.criteriaFieldClasses;
     private static Map<StringStringtaskCriteriaFields = TaskQueryServiceImpl.criteriaFields;
     private static Map<StringStringtaskCriteriaFieldJoinClauses = TaskQueryServiceImpl.criteriaFieldJoinClauses;
    
     private static Map<StringClass<?>> auditCriteriaFieldClasses = JPAAuditLogService.criteriaFieldClasses;
     private static Map<StringStringauditCriteriaFields = JPAAuditLogService.criteriaFields;
   
     // add tables lookup table logic
     private static final String processInstanceLogTable = "ProcessInstanceLog p";
     private static final String variableInstanceLogTable = "VariableInstanceLog v";
     private static final String taskTable = "TaskImpl t";
         
     private static Set<StringprocInstLogNeededCriterias = new CopyOnWriteArraySet<String>();
     private static Set<StringvarInstLogNeededCriterias = new CopyOnWriteArraySet<String>();
     private static Set<StringtaskNeededCriterias = new CopyOnWriteArraySet<String>();
     
     static { 
       
         // when doing a task or proc inst log query, add var inst log if these criteria are used
         .add(VARIABLE_ID_LIST);
         .add(VALUE_LIST);
         .add(OLD_VALUE_LIST);
         .add(EXTERNAL_ID_LIST);
         .add(VARIABLE_INSTANCE_ID_LIST);
         .add(VAR_VALUE_ID_LIST);
         
         // when doing a var inst log or proc inst log query, add task if these criteria are used
         .add(TASK_ID_LIST);
         .add(TASK_STATUS_LIST);
         .add(CREATED_BY_LIST);
         .add(STAKEHOLDER_ID_LIST);
         .add(POTENTIAL_OWNER_ID_LIST);
         .add(ACTUAL_OWNER_ID_LIST);
         .add(BUSINESS_ADMIN_ID_LIST);
         
         // when doing a task or var inst log query, add task if these criteria are used
         .add(START_DATE_LIST);
         .add(END_DATE_LIST);
         .add(PROCESS_INSTANCE_STATUS_LIST);
         
         // when doing a task or var inst log query, add task if these criteria are used
         .add(START_DATE_LIST);
         .add(END_DATE_LIST);
         .add(PROCESS_INSTANCE_STATUS_LIST);
         .add(PROCESS_VERSION_LIST);
     }
     
     private static final int TASK_SUMMARY_QUERY_TYPE;
     private static final int VARIABLE_INSTANCE_LOG_QUERY_TYPE;
     private static final int PROCESS_INSTANCE_LOG_QUERY_TYPE;
     private static final int OTHER_QUERY_TYPE;
 
     static { 
        int idGen = 0; 
         = idGen++;
         = idGen++;
         = idGen++;
         = idGen++;
     }
 
     private int determineQueryType(StringBuilder queryBuilder) { 
         String taskSumQueryBegin = TaskQueryServiceImpl.TASKSUMMARY_SELECT;
         String varInstLogQueryBegin = JPAAuditLogService.VARIABLE_INSTANCE_LOG_QUERY;
         String procInstLogQueryBegin = JPAAuditLogService.PROCESS_INSTANCE_LOG_QUERY;
         
        int queryLength = queryBuilder.length();
        int taskQueryLength = taskSumQueryBegin.length() > queryLength ? queryLength : taskSumQueryBegin.length();
        int varLogLength = varInstLogQueryBegin.length() > queryLength ? queryLength : varInstLogQueryBegin.length();
        int procLogLength = procInstLogQueryBegin.length() > queryLength ? queryLength : procInstLogQueryBegin.length();
        
        ifqueryBuilder.substring(0, taskQueryLength).equals(taskSumQueryBegin.substring(0, taskQueryLength)) ) { 
            return ;
        } else ifqueryBuilder.substring(0, varLogLength).equals(varInstLogQueryBegin.substring(0, varLogLength)) ) { 
            return ;
        } else ifqueryBuilder.substring(0, procLogLength).equals(procInstLogQueryBegin.substring(0, procLogLength)) ) { 
            return ;
        } else  {
            return ;
        }
    }
    @Override
    public void addTablesToQueryStringBuilder queryBuilder, QueryData queryData ) {
        int type = determineQueryType(queryBuilder);
        Set<StringadditionalTables = new HashSet<String>();
      
        // make a list with all of the parameter list ids
        Set<StringparameterListIdsUsed = new HashSet<String>();
        if( ! queryData.intersectParametersAreEmpty() ) { 
           parameterListIdsUsed.addAll(queryData.getIntersectParameters().keySet());
        }
        if( ! queryData.intersectRangeParametersAreEmpty() ) { 
           parameterListIdsUsed.addAll(queryData.getIntersectRangeParameters().keySet()); 
        }
        if( ! queryData.intersectRegexParametersAreEmpty() ) { 
           parameterListIdsUsed.addAll(queryData.getIntersectRegexParameters().keySet()); 
        }
       
        // go through parameter list ids to see which tables need to be added
        forString listId : parameterListIdsUsed ) { 
            iftype ==  ) { 
                if.contains(listId) ) { 
                    additionalTables.add();
                } else if.contains(listId) ) { 
                    additionalTables.add();
                }
            } else iftype ==  ) { 
                if.contains(listId) ) { 
                    additionalTables.add();
                } else if.contains(listId) ) { 
                    additionalTables.add();
                }
            } else iftype ==  ) { 
                if.contains(listId) ) { 
                    additionalTables.add();
                } else if.contains(listId) ) { 
                    additionalTables.add();
                }
            }
            ifadditionalTables.size() == 2 ) { 
                break;
            }
        }
       
        // Add the extra tables
        forString table : additionalTables ) { 
           queryBuilder.append(", " + table + "\n");
        }
    }
    @Override
    public void addCriteriaToQueryStringBuilder queryBuilder, QueryData queryData, QueryAndParameterAppender queryAppender ) {
        
       int type = determineQueryType(queryBuilder);
    
       boolean addLastVariableQueryClause = false;
       boolean addVariableValueQueryClause = false;
       String varInstLogTableId = "l";
       iftype !=  ) { 
           Set<StringqueryDataParms = new HashSet<String>();
           if( ! queryData.intersectParametersAreEmpty() ) { 
               queryDataParms.addAll(queryData.getIntersectParameters().keySet());
           }
           if( ! queryData.intersectRangeParametersAreEmpty() ) { 
               queryDataParms.addAll(queryData.getIntersectRangeParameters().keySet());
           }
           if( ! queryData.intersectRegexParametersAreEmpty() ) { 
               queryDataParms.addAll(queryData.getIntersectRegexParameters().keySet());
           }
           forString listId :  ) { 
               ifqueryDataParms.contains(listId) ) { 
                   addLastVariableQueryClause = true;
                   break;
               }
           }
           forString listId : queryDataParms ) { 
              if( VAR_VALUE_ID_LIST.equals(listId) )  { 
                  addVariableValueQueryClause = true;
                  break;
              }
           }
       }
       
       Map<StringStringemptyJoinClauses = Collections.emptyMap();
       // task queries
       iftype ==  ) { 
           varInstLogTableId = "v";
           internalAddCriteriaToQuery(
                   queryBuilderqueryDataqueryAppender
                   
                   "p"
                   
                   ".processInstanceId = t.taskData.processInstanceId",
                   
                   varInstLogTableId,
                   emptyJoinClauses
                   ".processInstanceId = t.taskData.processInstanceId");
       } 
      
       // variable log queries
       else iftype ==  ) { 
           internalAddCriteriaToQuery(
                   queryBuilderqueryDataqueryAppender
                   
                   "p"
                   
                   ".processInstanceId = l.processInstanceId",
                   
                   "t",
                   ".taskData.processInstanceId = l.processInstanceId");
       }
       
       // process log queries
       else iftype ==  ) { 
           varInstLogTableId = "v";
           internalAddCriteriaToQuery(
                   queryBuilderqueryDataqueryAppender
                   
                   varInstLogTableId,
                   
                   ".processInstanceId = l.processInstanceId",
                   
                   "t",
                   ".taskData.processInstanceId = l.processInstanceId");
       }
       iftype !=  ) { 
           ifaddLastVariableQueryClause ) { 
               queryData.getIntersectParameters().remove(LAST_VARIABLE_LIST);
               boolean addWhereClause = ! queryAppender.hasBeenUsed() && type != ;
               queryBuilder.append("\n").append( (addWhereClause ? "WHERE" : "AND" ) )
               .append(" (").append(varInstLogTableId).append(".id IN (SELECT MAX(ll.id) FROM VariableInstanceLog ll ")
               .append("GROUP BY ll.variableId, ll.processInstanceId))"); 
               queryAppender.markAsUsed();
               queryAppender.queryBuilderModificationCleanup();
           }
           ifaddVariableValueQueryClause ) { 
               if( ! queryData.intersectParametersAreEmpty() ) { 
                   List<StringvarValParameters = (List<String>) queryData.getIntersectParameters().remove(VAR_VALUE_ID_LIST);
                   ifvarValParameters != null && ! varValParameters.isEmpty() ) { 
                      List<Object[]> varValCriteria = new ArrayList<Object[]>();
                      checkVarValCriteria(varValParametersfalsefalsevarValCriteria);
                      addVarValCriteria(! queryAppender.hasBeenUsed(), queryBuilderqueryAppendervarInstLogTableIdvarValCriteria);
                      queryAppender.markAsUsed();
                      queryAppender.queryBuilderModificationCleanup();
                   }
               } 
               if( ! queryData.intersectRegexParametersAreEmpty() ) { 
                   List<StringvarValRegexParameters = queryData.getIntersectRegexParameters().remove(VAR_VALUE_ID_LIST);
                   ifvarValRegexParameters != null && ! varValRegexParameters.isEmpty() ) { 
                      List<Object[]> varValCriteria = new ArrayList<Object[]>();
                      checkVarValCriteria(varValRegexParametersfalsetruevarValCriteria);
                      addVarValCriteria(! queryAppender.hasBeenUsed(), queryBuilderqueryAppendervarInstLogTableIdvarValCriteria);
                      queryAppender.markAsUsed();
                      queryAppender.queryBuilderModificationCleanup();
                   }
               }
           }
       }
    } 
    private static void internalAddCriteriaToQuery(StringBuilder queryBuilder, QueryData queryData, QueryAndParameterAppender queryAppender,
           Set<StringfirstNeededCriteriasString firstTableId
           Map<StringClass<?>> firstCriteriaFieldClassesMap<StringStringfirstCriteriaFields
           String firstTableIdJoinClause,
           Set<StringotherNeededCriteriasString otherTableId
           Map<StringClass<?>> otherCriteriaFieldClassesMap<StringStringotherCriteriaFields,
           Map<StringStringotherCriteriaFieldJoinClauses
           String otherTableJoinClause ) { 
        
        boolean addFirstTableJoinClause = false;
        boolean addOtherTableJoinClause = false;
        
        Set<StringprocessedListIds = new HashSet<String>();
        // regular parameters
        if( ! queryData.intersectParametersAreEmpty() ) { 
            forEntry<StringList<? extends Object>> entry : queryData.getIntersectParameters().entrySet() ) { 
                String listId = entry.getKey();
                if( VAR_VALUE_ID_LIST.equals(listId) ) { 
                    continue;
                }
                iffirstNeededCriterias.contains(listId) )   { 
                    addFirstTableJoinClause = true;
                    processedListIds.add(listId);
                    String fieldName = firstTableId + firstCriteriaFields.get(listId).substring(1);
                    queryAppender.addQueryParameters(
                            entry.getValue(), listId
                            firstCriteriaFieldClasses.get(listId), fieldName
                            false);
                } else ifotherNeededCriterias.contains(listId) ) { 
                    addOtherTableJoinClause = true;
                    processedListIds.add(listId);
                    String fieldName = otherTableId + otherCriteriaFields.get(listId).substring(1);
                    queryAppender.addQueryParameters(
                            entry.getValue(), listId
                            otherCriteriaFieldClasses.get(listId), fieldName,
                            otherCriteriaFieldJoinClauses.get(listId),
                            false);
                }
            }
            forString processedListId : processedListIds ) { 
                queryData.getIntersectParameters().remove(processedListId);
            }
            processedListIds.clear();
        }
        
        // range parameters
        if( ! queryData.intersectRangeParametersAreEmpty() ) { 
            forEntry<StringList<? extends Object>> entry : queryData.getIntersectRangeParameters().entrySet() ) { 
                String listId = entry.getKey();
                iffirstNeededCriterias.contains(listId) )   { 
                    addFirstTableJoinClause = true;
                    processedListIds.add(listId);
                    String fieldName = firstTableId + firstCriteriaFields.get(listId).substring(1);
                    queryAppender.addRangeQueryParameters(
                            entry.getValue(), listId
                            firstCriteriaFieldClasses.get(listId), fieldName
                            false);
                } else ifotherNeededCriterias.contains(listId) ) { 
                    addOtherTableJoinClause = true;
                    processedListIds.add(listId);
                    String fieldName = otherTableId + otherCriteriaFields.get(listId).substring(1);
                    queryAppender.addRangeQueryParameters(
                            entry.getValue(), listId
                            otherCriteriaFieldClasses.get(listId), fieldName,
                            otherCriteriaFieldJoinClauses.get(listId),
                            false);
                }
            }
            forString processedListId : processedListIds ) { 
                queryData.getIntersectRangeParameters().remove(processedListId);
            }
            processedListIds.clear();
        }
        
        // regex parameters
        if( ! queryData.intersectRegexParametersAreEmpty() ) { 
            forEntry<StringList<String>> entry : queryData.getIntersectRegexParameters().entrySet() ) { 
                String listId = entry.getKey();
                if( VAR_VALUE_ID_LIST.equals(listId) ) { 
                    continue;
                }
                iffirstNeededCriterias.contains(listId) )   { 
                    addFirstTableJoinClause = true;
                    processedListIds.add(listId);
                    String fieldName = firstTableId + firstCriteriaFields.get(listId).substring(1);
                    queryAppender.addRegexQueryParameters(
                            entry.getValue(), listId
                            fieldName
                            false);
                } else ifotherNeededCriterias.contains(listId) ) { 
                    addOtherTableJoinClause = true;
                    processedListIds.add(listId);
                    String fieldName = otherTableId + otherCriteriaFields.get(listId).substring(1);
                    queryAppender.addRegexQueryParameters(
                            entry.getValue(), listId
                            fieldName,
                            otherCriteriaFieldJoinClauses.get(listId),
                            false);
                }
            }
            forString processedListId : processedListIds ) { 
                queryData.getIntersectRegexParameters().remove(processedListId);
            }
        }
        
        ifaddFirstTableJoinClause ) { 
           queryBuilder.append("\nAND " + firstTableId + firstTableIdJoinClause );
        }
        ifaddOtherTableJoinClause ) { 
           queryBuilder.append("\nAND " + otherTableId + otherTableJoinClause );
        }
    }
    
New to GrepCode? Check out our FAQ X