Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2005-2013 The Kuali Foundation Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php 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.kuali.rice.kew.rule.dao.impl;
 
 
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
 
 
 public class RuleDAOJpaImpl implements RuleDAO {
 
 	private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RuleDAOJpaImpl.class);
 
 	@PersistenceContext(unitName="kew-unit")
 
 	private static final String OLD_DELEGATIONS_SQL =
 		"select oldDel.dlgn_rule_id "+
 		"from krew_rule_rsp_t oldRsp, krew_dlgn_rsp_t oldDel "+
 		"where oldRsp.rule_id=? and "+
 		"oldRsp.rule_rsp_id=oldDel.rule_rsp_id and "+
 		"oldDel.dlgn_rule_base_val_id not in "+
 		"(select newDel.dlgn_rule_base_val_id from krew_rule_rsp_t newRsp, krew_dlgn_rsp_t newDel "+
 		"where newRsp.rule_id=? and "+
 		"newRsp.rule_rsp_id=newDel.rule_rsp_id)";
 
 	public void save(RuleBaseValues ruleBaseValues) {
 		if(ruleBaseValues.getId()==null){
 			.persist(ruleBaseValues);
 		}else{
 			OrmUtils.merge(ruleBaseValues);
 		}
 	}
 
 	public List<RuleBaseValuesfetchAllCurrentRulesForTemplateDocCombination(String ruleTemplateIdList documentTypes) {
 		Criteria crit = new Criteria(RuleBaseValues.class.getName());
 		crit.in("docTypeName"documentTypes);
 		crit.eq("ruleTemplateId"ruleTemplateId);
 		crit.eq("currentInd".);
 		crit.eq("active".);
 		crit.eq("delegateRule".);
 		crit.eq("templateRuleInd".);
 
 		return (Listnew QueryByCriteria(crit).toQuery().getResultList();
 	}
 
 	public List<RuleBaseValuesfetchAllCurrentRulesForTemplateDocCombination(String ruleTemplateIdList documentTypesTimestamp effectiveDate) {
 		Criteria crit = new Criteria(RuleBaseValues.class.getName());
 		crit.in("docTypeName"documentTypes);
 		crit.eq("ruleTemplateId"ruleTemplateId);
 		crit.eq("active".);
 		crit.eq("delegateRule".);
 		crit.eq("templateRuleInd".);
 		if (effectiveDate != null) {
 			crit.lte("activationDate"effectiveDate);
 			crit.gte("deactivationDate"effectiveDate);
 		}
 
 		return (Listnew QueryByCriteria(crit).toQuery().getResultList();
 	}
 
 		Criteria crit = new Criteria(RuleBaseValues.class.getName());
		Criteria fromCrit = new Criteria(RuleBaseValues.class.getName());
		Criteria fromNullCrit = new Criteria(RuleBaseValues.class.getName());
		fromNullCrit.isNull("fromDateValue");
		Criteria fromLessOrEqualCrit = new Criteria(RuleBaseValues.class.getName());
		fromLessOrEqualCrit.lte("fromDateValue"new Timestamp(date.getTime()));
		fromCrit.or(fromNullCrit);
		fromCrit.or(fromLessOrEqualCrit);
		Criteria toCrit = new Criteria(RuleBaseValues.class.getName());
		Criteria toNullCrit = new Criteria(RuleBaseValues.class.getName());
		toNullCrit.isNull("toDateValue");
		Criteria toGreaterOrEqualCrit = new Criteria(RuleBaseValues.class.getName());
		toGreaterOrEqualCrit.gte("toDateValue"new Timestamp(date.getTime()));
		toCrit.or(toNullCrit);
		toCrit.or(toGreaterOrEqualCrit);
		crit.and(fromCrit);
		crit.and(toCrit);
		return crit;
	}
	public List<RuleBaseValuesfetchAllRules(boolean currentRules) {
		Criteria crit = new Criteria(RuleBaseValues.class.getName());
		crit.eq("currentInd"new Boolean(currentRules));
		crit.eq("templateRuleInd".);
		crit.orderBy("activationDate"false);
		return (Listquery.toQuery().getResultList();
	}
	public void delete(String ruleBaseValuesId) {
	}
	public List<RuleBaseValuesfindByDocumentId(String documentId) {
		Criteria crit = new Criteria(RuleBaseValues.class.getName());
		crit.eq("documentId"documentId);
	}
        Criteria crit = new Criteria(RuleBaseValues.class.getName());
        if (name == null) {
        	return null;
        }
        crit.eq("name"name);
        crit.eq("currentInd".);
       	return (RuleBaseValuesnew QueryByCriteria(crit).toQuery().getSingleResult();
    }
	public RuleBaseValues findRuleBaseValuesById(String ruleBaseValuesId) {
		if (ruleBaseValuesId == null) {
			return null;
		}
		Criteria crit = new Criteria(RuleBaseValues.class.getName());
		crit.eq("id"ruleBaseValuesId);
	}
		crit.eq("ruleResponsibilityName"reviewerName);
		crit.eq("ruleResponsibilityType"type);
		List responsibilities = (Listnew QueryByCriteria(crit).toQuery().getResultList();
		List rules = new ArrayList();
		for (Iterator iter = responsibilities.iterator(); iter.hasNext();) {
			RuleResponsibilityBo responsibility = (RuleResponsibilityBoiter.next();
			RuleBaseValues rule = responsibility.getRuleBaseValues();
			if (rule != null && rule.getCurrentInd() != null && rule.getCurrentInd().booleanValue()) {
				rules.add(rule);
			}
		}
		return rules;
	}
	public List<RuleBaseValuesfindRuleBaseValuesByResponsibilityReviewerTemplateDoc(String ruleTemplateNameString documentTypeString reviewerNameString type) {
	    Criteria crit = new Criteria(RuleResponsibilityBo.class.getName());
		crit.eq("ruleResponsibilityName"reviewerName);
		crit.eq("ruleResponsibilityType"type);
		crit.eq("ruleBaseValues.currentInd".);
		if (!StringUtils.isBlank(ruleTemplateName)) {
		    crit.like("ruleBaseValues.ruleTemplate.name"ruleTemplateName.replace("*""%").concat("%"));
		}
		if (!StringUtils.isBlank(documentType)) {
		    crit.like("ruleBaseValues.docTypeName"documentType.replace("*""%").concat("%"));
		}
		List responsibilities = (Listnew QueryByCriteria(crit).toQuery().getResultList();
		List rules = new ArrayList();
		for (Iterator iter = responsibilities.iterator(); iter.hasNext();) {
			RuleResponsibilityBo responsibility = (RuleResponsibilityBoiter.next();
			RuleBaseValues rule = responsibility.getRuleBaseValues();
			if (rule != null && rule.getCurrentInd() != null && rule.getCurrentInd().booleanValue()) {
				rules.add(rule);
			}
		}
		return rules;
	}
	//FIXME nothing uses this, it's not in ruleDAO interface
//	public List findRuleBaseValuesByObjectGraph(RuleBaseValues ruleBaseValues) {
//		ruleBaseValues.setCurrentInd(Boolean.TRUE);
//		ruleBaseValues.setTemplateRuleInd(Boolean.FALSE);
//		return (List) new QueryByObject(entityManager,ruleBaseValues).toQuery().getResultList();
//	}
	public RuleResponsibilityBo findRuleResponsibility(String responsibilityId) {
		crit.eq("responsibilityId"responsibilityId);
		Collection responsibilities = new QueryByCriteria(crit).toQuery().getResultList();
		for (Iterator iterator = responsibilities.iterator(); iterator.hasNext();) {
			RuleResponsibilityBo responsibility = (RuleResponsibilityBoiterator.next();
			if (responsibility.getRuleBaseValues().getCurrentInd().booleanValue()) {
				return responsibility;
			}
		}
		return null;
	}
	public List<RuleBaseValuessearch(String docTypeNameString ruleIdString ruleTemplateIdString ruleDescriptionString groupIdString principalIdBoolean delegateRuleBoolean activeIndMap extensionValuesString workflowIdDirective) {
        Criteria crit = getSearchCriteria(docTypeNameruleTemplateIdruleDescriptiondelegateRuleactiveIndextensionValues);
        if (ruleId != null) {
            crit.eq("id"ruleId);
        }
        if (groupId != null) {
            //crit.in("responsibilities.ruleBaseValuesId", getResponsibilitySubQuery(groupId), "ruleBaseValuesId");
        	addResponsibilityCriteria(critgroupId);
        }
        Collection<StringkimGroupIds = new HashSet<String>();
        Boolean searchUser = .;
        Boolean searchUserInWorkgroups = .;
        
        if ("group".equals(workflowIdDirective)) {
            searchUserInWorkgroups = .;
        } else if (StringUtils.isBlank(workflowIdDirective)) {
            searchUser = .;
            searchUserInWorkgroups = .;
        } else {
            searchUser = .;
        }
        
        if (!org.apache.commons.lang.StringUtils.isEmpty(principalId) && searchUserInWorkgroups) {
            Principal principal = null;
            principal = KimApiServiceLocator.getIdentityService().getPrincipal(principalId);
            if (principal == null)
            {
            	throw new RiceRuntimeException("Failed to locate user for the given principal id: " + principalId);
            }
            kimGroupIds = KimApiServiceLocator.getGroupService().getGroupIdsByPrincipalId(principalId);
        }
        addResponsibilityCriteria(critkimGroupIdsprincipalIdsearchUsersearchUserInWorkgroups);
        //if (responsibilityCrit != null) {
        	//crit.in("responsibilities.ruleBaseValuesId", responsibilityCrit,"ruleBaseValuesId");
        //}
        crit.distinct(true);
	}
    public List<RuleBaseValuessearch(String docTypeNameString ruleTemplateIdString ruleDescriptionCollection<StringworkgroupIdsString workflowIdBoolean delegateRuleBoolean activeIndMap extensionValuesCollection actionRequestCodes) {
        Criteria crit = getSearchCriteria(docTypeNameruleTemplateIdruleDescriptiondelegateRuleactiveIndextensionValues);
        addResponsibilityCriteria(critworkgroupIdsworkflowIdactionRequestCodes, (workflowId != null), ((workgroupIds != null) && !workgroupIds.isEmpty()));
        //if (responsibilityCrit != null) {
        	//crit.in("responsibilities.ruleBaseValuesId", responsibilityCrit, "ruleBaseValuesId");
        //}
        return (Listnew QueryByCriteria(crit).toQuery().getResultList();
    }
    private void addResponsibilityCriteria(Criteria parentCritCollection<StringkimGroupIdsString principalIdBoolean searchUserBoolean searchUserInWorkgroups) {
        Collection<StringworkgroupIdStrings = new ArrayList<String>();
        for (String workgroupId : kimGroupIds) {
            workgroupIdStrings.add(workgroupId.toString());
        }
        addResponsibilityCriteria(parentCritworkgroupIdStrings,principalId,new ArrayList<String>(), searchUsersearchUserInWorkgroups);
    }
    
    private void addResponsibilityCriteria(Criteria parentCritCollection<StringworkgroupIdsString workflowIdCollection actionRequestCodesBoolean searchUserBoolean searchUserInWorkgroups) {
        Criteria responsibilityCrit = null;
        Criteria ruleResponsibilityNameCrit = null;
        
        if ( (actionRequestCodes != null) && (!actionRequestCodes.isEmpty()) ) {
        	responsibilityCrit = new Criteria(RuleBaseValues.class.getName(), false);
            responsibilityCrit.in("__JPA_ALIAS[['rr']]__.actionRequestedCd"new ArrayList(actionRequestCodes));
        }
        
        if (!org.apache.commons.lang.StringUtils.isEmpty(workflowId)) {
            // workflow user id exists
            if (searchUser != null && searchUser) {
                // searching user wishes to search for rules specific to user
                ruleResponsibilityNameCrit = new Criteria(RuleBaseValues.class.getName(), false);
                ruleResponsibilityNameCrit.like("__JPA_ALIAS[['rr']]__.ruleResponsibilityName"workflowId);
                ruleResponsibilityNameCrit.eq("__JPA_ALIAS[['rr']]__.ruleResponsibilityType".);
            }
            if ( (searchUserInWorkgroups != null && searchUserInWorkgroups) && (workgroupIds != null) && (!workgroupIds.isEmpty()) ) {
                // at least one workgroup id exists and user wishes to search on workgroups
                if (ruleResponsibilityNameCrit == null) {
                    ruleResponsibilityNameCrit = new Criteria(RuleBaseValues.class.getName(), false);
                }
                Criteria workgroupCrit = new Criteria(RuleBaseValues.class.getName(), false);
                workgroupCrit.in("__JPA_ALIAS[['rr']]__.ruleResponsibilityName"new ArrayList<String>(workgroupIds));
                workgroupCrit.eq("__JPA_ALIAS[['rr']]__.ruleResponsibilityType".);
                ruleResponsibilityNameCrit.or(workgroupCrit);
            }
        } else if ( (workgroupIds != null) && (workgroupIds.size() == 1) ) {
            // no user and one workgroup id
            ruleResponsibilityNameCrit = new Criteria(RuleBaseValues.class.getName(), false);
            ruleResponsibilityNameCrit.like("__JPA_ALIAS[['rr']]__.ruleResponsibilityName"workgroupIds.iterator().next());
            ruleResponsibilityNameCrit.eq("__JPA_ALIAS[['rr']]__.ruleResponsibilityType".);
        } else if ( (workgroupIds != null) && (workgroupIds.size() > 1) ) {
            // no user and more than one workgroup id
            ruleResponsibilityNameCrit = new Criteria(RuleBaseValues.class.getName(), false);
            ruleResponsibilityNameCrit.in("__JPA_ALIAS[['rr']]__.ruleResponsibilityName",  new ArrayList<String>(workgroupIds));
            ruleResponsibilityNameCrit.eq("__JPA_ALIAS[['rr']]__.ruleResponsibilityType".);
        }
        if (ruleResponsibilityNameCrit != null) {
        	if (responsibilityCrit == null) {
        		responsibilityCrit = new Criteria(RuleBaseValues.class.getName(), false);
        	}
            responsibilityCrit.and(ruleResponsibilityNameCrit);
        }
        if (responsibilityCrit != null) {
        	if (parentCrit.getAliasIndex("rr") == -1) {
    			parentCrit.join("responsibilities""rr"falsetrue);
    		}
        	parentCrit.and(responsibilityCrit);
        }
        //return responsibilityCrit;
    }
    private Criteria getSearchCriteria(String docTypeNameString ruleTemplateIdString ruleDescriptionBoolean delegateRuleBoolean activeIndMap extensionValues) {
        Criteria crit = new Criteria(RuleBaseValues.class.getName());
        crit.eq("currentInd".);
        crit.eq("templateRuleInd".);
        if (activeInd != null) {
            crit.eq("active"activeInd);
        }
        if (docTypeName != null) {
            crit.like("UPPER(__JPA_ALIAS[[0]]__.docTypeName)"docTypeName.toUpperCase());
        }
        if (ruleDescription != null && !ruleDescription.trim().equals("")) {
            crit.like("UPPER(__JPA_ALIAS[[0]]__.description)"ruleDescription.toUpperCase());
        }
        if (ruleTemplateId != null) {
            crit.eq("ruleTemplateId"ruleTemplateId);
        }
        if (delegateRule != null) {
            crit.eq("delegateRule"delegateRule);
        }
        if (extensionValues != null && !extensionValues.isEmpty()) {
            for (Iterator iter2 = extensionValues.entrySet().iterator(); iter2.hasNext();) {
                Map.Entry entry = (Map.Entryiter2.next();
                if (!org.apache.commons.lang.StringUtils.isEmpty((Stringentry.getValue())) {
                    // Criteria extensionCrit = new Criteria();
                    // extensionCrit.addEqualTo("extensionValues.key",
                    // entry.getKey());
                    // extensionCrit.addLike("extensionValues.value",
                    // "%"+(String) entry.getValue()+"%");
                    Criteria extensionCrit2 = new Criteria(RuleExtensionBo.class.getName());
                    extensionCrit2.distinct(true);
                    extensionCrit2.join("extensionValues""extval"falsetrue);
                    extensionCrit2.eq("__JPA_ALIAS[['extval']]__.key"entry.getKey());
                    extensionCrit2.like("UPPER(__JPA_ALIAS[['extval']]__.value)", ("%" + (Stringentry.getValue() + "%").toUpperCase());
                    // Criteria extensionCrit3 = new Criteria();
                    // extensionCrit3.addEqualTo("extensionValues.key",
                    // entry.getKey());
                    // extensionCrit3.addLike("extensionValues.value",
                    // ("%"+(String) entry.getValue()+"%").toLowerCase());
                    // extensionCrit.addOrCriteria(extensionCrit2);
                    // extensionCrit.addOrCriteria(extensionCrit3);
                    extensionCrit2.memberOf("__JPA_ALIAS[[0]]__""__JPA_ALIAS[[-1]]__.ruleExtensions");
                    crit.exists(extensionCrit2);
                }
            }
        }
        return crit;
    }
//    private Criteria getWorkgroupOrCriteria(Collection workgroupIds) {
//        Criteria responsibilityCrit = new Criteria(RuleResponsibility.class.getName());
//        for (Iterator iter = workgroupIds.iterator(); iter.hasNext();) {
//            String workgroupIdFromList = (String) iter.next();
//            Criteria orCriteria = new Criteria(RuleResponsibility.class.getName());
//            orCriteria.like("ruleResponsibilityName", workgroupIdFromList);
//            responsibilityCrit.or(orCriteria);
//        }
//
//        Criteria crit = new Criteria();
//        crit.in("responsibilities.ruleBaseValuesId", responsibilityCrit,"ruleBaseValuesId");
//        return crit;
//    }
	private void addResponsibilityCriteria(Criteria parentCritString ruleResponsibilityName) {
		//Criteria responsibilityCrit = new Criteria(RuleResponsibility.class.getName());
		if (parentCrit.getAliasIndex("rr") == -1) {
			parentCrit.join("responsibilities""rr"falsetrue);
		}
		parentCrit.like("__JPA_ALIAS[['rr']]__.ruleResponsibilityName"ruleResponsibilityName);
		//ReportQueryByCriteria query = QueryFactory.newReportQuery(RuleResponsibility.class, responsibilityCrit);
		//query.setAttributes(new String[] { "ruleBaseValuesId" });
		//return responsibilityCrit;
	}
//	private Criteria getWorkgroupResponsibilitySubQuery(Set<Long> workgroupIds) {
//	    	Set<String> workgroupIdStrings = new HashSet<String>();
//	    	for (Long workgroupId : workgroupIds) {
//	    	    workgroupIdStrings.add(workgroupId.toString());
//	    	}
//		Criteria responsibilityCrit = new Criteria(RuleResponsibility.class.getName());
//		responsibilityCrit.in("ruleResponsibilityName", new ArrayList(workgroupIds));
//		responsibilityCrit.eq("ruleResponsibilityType", KewApiConstants.RULE_RESPONSIBILITY_GROUP_ID);
////		ReportQueryByCriteria query = QueryFactory.newReportQuery(RuleResponsibility.class, responsibilityCrit);
////		query.setAttributes(new String[] { "ruleBaseValuesId" });
//		return responsibilityCrit;
//	}
	public List<RuleBaseValuesfindByPreviousRuleId(String previousRuleId) {
		Criteria crit = new Criteria(RuleBaseValues.class.getName());
		crit.eq("previousRuleId"previousRuleId);
	}
		Criteria crit = new Criteria(RuleBaseValues.class.getName());
		crit.eq("ruleTemplateId"ruleTemplateId);
		crit.eq("templateRuleInd".);
		if (rules != null && !rules.isEmpty()) {
			return (RuleBaseValuesrules.get(0);
		}
		return null;
	}
	public void retrieveAllReferences(RuleBaseValues rule) {
		// getPersistenceBroker().retrieveAllReferences(rule);
	}
	public RuleBaseValues getParentRule(String ruleBaseValuesId) {
		Criteria criteria = new Criteria(RuleBaseValues.class.getName());
		criteria.eq("currentInd".);
		criteria.eq("responsibilities.delegationRules.delegateRuleId"ruleBaseValuesId);
		RuleBaseValues rule = null;
		for (Iterator iterator = rules.iterator(); iterator.hasNext();) {
			RuleBaseValues currentRule = (RuleBaseValuesiterator.next();
			if (rule == null || currentRule.getVersionNbr().intValue() > rule.getVersionNbr().intValue()) {
				rule = currentRule;
			}
		}
		return rule;
	}
	public List findOldDelegations(final RuleBaseValues oldRulefinal RuleBaseValues newRule) {
		q.setParameter(1, oldRule.getId());
		q.setParameter(2, newRule.getId());
		List oldDelegations = new ArrayList();
		for(Object l:q.getResultList()){
			// FIXME: KULRICE-5201 - This used to be a cast by new Long(l) -- assuming that the Object here in result list is actually a string or is castable to string by .toString()
			oldDelegations.add(findRuleBaseValuesById(String.valueOf(l)));
		}
		return oldDelegations;
	}
	public String findResponsibilityIdForRule(String ruleNameString ruleResponsibilityNameString ruleResponsibilityType) {
		crit.eq("ruleResponsibilityName"ruleResponsibilityName);
		crit.eq("ruleResponsibilityType"ruleResponsibilityType);
		crit.eq("ruleBaseValues.currentInd".);
		crit.eq("ruleBaseValues.name"ruleName);
		Collection responsibilities = new QueryByCriteria(crit).toQuery().getResultList();
		if (responsibilities != null) {
			for (Iterator iter = responsibilities.iterator(); iter.hasNext();) {
				RuleResponsibilityBo responsibility = (RuleResponsibilityBoiter.next();
				return responsibility.getResponsibilityId();
			}
		}
		return null;
	}
    public EntityManager getEntityManager() {
        return this.;
    }
    public void setEntityManager(EntityManager entityManager) {
        this. = entityManager;
    }
New to GrepCode? Check out our FAQ X