001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.workgroup.dao;
018:
019: import java.math.BigDecimal;
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Map;
025:
026: import org.apache.commons.lang.StringUtils;
027: import org.apache.ojb.broker.query.Criteria;
028: import org.apache.ojb.broker.query.QueryByCriteria;
029: import org.apache.ojb.broker.query.QueryFactory;
030: import org.apache.ojb.broker.query.ReportQueryByCriteria;
031: import org.kuali.workflow.workgroup.BaseWorkgroupExtension;
032: import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
033:
034: import edu.iu.uis.eden.EdenConstants;
035: import edu.iu.uis.eden.KEWServiceLocator;
036: import edu.iu.uis.eden.actionrequests.ActionRequestValue;
037: import edu.iu.uis.eden.routetemplate.RuleExtension;
038: import edu.iu.uis.eden.user.WorkflowUser;
039: import edu.iu.uis.eden.util.Utilities;
040: import edu.iu.uis.eden.workgroup.BaseWorkgroup;
041: import edu.iu.uis.eden.workgroup.WorkflowGroupId;
042: import edu.iu.uis.eden.workgroup.Workgroup;
043:
044: public class BaseWorkgroupDAOOjbImpl extends
045: PersistenceBrokerDaoSupport implements BaseWorkgroupDAO {
046:
047: public void save(Workgroup workgroup) {
048: // the OJB mapping does not automatically grab the id from the sequence, so if we attempt to save this document without
049: // an id, we'll generate one before saving it
050: if (workgroup.getWorkflowGroupId() == null
051: || workgroup.getWorkflowGroupId().getGroupId() == null) {
052: workgroup.setWorkflowGroupId(new WorkflowGroupId(
053: KEWServiceLocator.getRouteHeaderService()
054: .getNextRouteHeaderId()));
055: }
056: getPersistenceBrokerTemplate().store(workgroup);
057: }
058:
059: public List search(Workgroup workgroup,
060: Map<String, String> extensionValues) {
061: Criteria crit = buildStandardCriteria(workgroup,
062: extensionValues);
063: return (List) getPersistenceBrokerTemplate()
064: .getCollectionByQuery(
065: new QueryByCriteria(BaseWorkgroup.class, crit));
066: }
067:
068: public List find(Workgroup workgroup,
069: Map<String, String> extensionValues, WorkflowUser user) {
070: Criteria crit = buildStandardCriteria(workgroup,
071: extensionValues);
072:
073: if (user != null) {
074: // Criteria userCriteria = new Criteria();
075: // if (!user.getAuthenticationUserId().isEmpty()) {
076: // userCriteria.addLike("authenticationUserId", user.getAuthenticationUserId().getAuthenticationId().replace('*', '%'));
077: // }
078: // if (!user.getWorkflowUserId().isEmpty()) {
079: // userCriteria.addEqualTo("workflowUserId", user.getWorkflowUserId().getWorkflowId());
080: // }
081: // if (!user.getEmplId().isEmpty()) {
082: // userCriteria.addEqualTo("emplId", user.getEmplId().getEmplId());
083: // }
084: // if (!user.getUuId().isEmpty()) {
085: // userCriteria.addEqualTo("uuId", user.getUuId().getUuId());
086: // }
087: // Collection users = getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(SpringServiceLocator.getUserService().getBlankUser().getClass(), userCriteria));
088: Collection users = KEWServiceLocator.getUserService()
089: .search(user, true);
090: if (users == null || users.size() == 0) {
091: // this means the username they entered couldn't be found
092: return new ArrayList();
093: }
094: Collection workflowIds = new ArrayList();
095: for (Iterator iter = users.iterator(); iter.hasNext();) {
096: WorkflowUser wfUser = (WorkflowUser) iter.next();
097: workflowIds.add(wfUser.getWorkflowUserId()
098: .getWorkflowId());
099: }
100: crit.addIn("workgroupMembers.workflowId", workflowIds);
101: }
102: return (List) getPersistenceBrokerTemplate()
103: .getCollectionByQuery(
104: new QueryByCriteria(BaseWorkgroup.class, crit));
105: }
106:
107: protected Criteria buildStandardCriteria(Workgroup workgroup,
108: Map<String, String> extensionValues) {
109: Criteria crit = new Criteria();
110: crit.addEqualTo("currentInd", new Boolean(true));
111: if (workgroup.getActiveInd() != null) {
112: crit.addEqualTo("activeInd", workgroup.getActiveInd());
113: }
114: if (!StringUtils.isEmpty(workgroup.getDescription())) {
115: crit.addLike("UPPER(description)", "%"
116: + workgroup.getDescription().toUpperCase() + "%");
117: }
118: if (workgroup.getGroupNameId() != null
119: && !StringUtils.isEmpty(workgroup.getGroupNameId()
120: .getNameId())) {
121: crit.addLike("UPPER(workgroupName)", "%"
122: + workgroup.getGroupNameId().getNameId()
123: .toUpperCase() + "%");
124: }
125: if (!StringUtils.isBlank(workgroup.getWorkgroupType())) {
126: if (workgroup.getWorkgroupType().equals("Default")) {
127: Criteria orCrit = new Criteria();
128: Criteria nullType = new Criteria();
129: nullType.addIsNull("workgroupType");
130: Criteria legType = new Criteria();
131: legType.addEqualTo("workgroupType",
132: EdenConstants.LEGACY_DEFAULT_WORKGROUP_TYPE);
133: orCrit.addOrCriteria(nullType);
134: orCrit.addOrCriteria(legType);
135: crit.addAndCriteria(orCrit);
136: } else {
137: crit.addEqualTo("workgroupType", workgroup
138: .getWorkgroupType());
139: }
140: }
141: if (workgroup.getWorkflowGroupId() != null
142: && workgroup.getWorkflowGroupId().getGroupId() != null) {
143: crit.addEqualTo("workgroupId", workgroup
144: .getWorkflowGroupId().getGroupId());
145: }
146: if (extensionValues != null && !extensionValues.isEmpty()) {
147: int index = 0;
148: for (String key : extensionValues.keySet()) {
149: String value = extensionValues.get(key);
150: if (!StringUtils.isBlank(value)) {
151: Criteria extCrit = new Criteria();
152: extCrit.setAlias("EXT" + (index++));
153: extCrit.addEqualTo("extensions.data.key", key);
154: extCrit.addLike("UPPER(extensions.data.value)",
155: ("%" + value.replace("*", "%") + "%")
156: .toUpperCase());
157: crit.addAndCriteria(extCrit);
158: }
159: }
160: }
161: return crit;
162: }
163:
164: public BaseWorkgroup findByWorkgroupId(Long workgroupId) {
165: Criteria crit = new Criteria();
166: crit.addEqualTo("workgroupId", workgroupId);
167: crit.addEqualTo("currentInd", Boolean.TRUE);
168: return (BaseWorkgroup) getPersistenceBrokerTemplate()
169: .getObjectByQuery(
170: new QueryByCriteria(BaseWorkgroup.class, crit));
171: }
172:
173: public List findByName(String workgroupName, boolean workgroupCurInd) {
174: Criteria crit = new Criteria();
175: crit.addEqualTo("workgroupName", workgroupName);
176: crit.addEqualTo("currentInd", new Boolean(workgroupCurInd));
177: return (List) getPersistenceBrokerTemplate()
178: .getCollectionByQuery(
179: new QueryByCriteria(BaseWorkgroup.class, crit));
180: }
181:
182: public BaseWorkgroup findByName(String workgroupName) {
183: List workgroups = findByName(workgroupName, true);
184: if (workgroups.size() > 0) {
185: return (BaseWorkgroup) findByName(workgroupName, true).get(
186: 0);
187: }
188: return null;
189: }
190:
191: public BaseWorkgroup findEnrouteWorkgroupByName(String workgroupName) {
192: Criteria crit = new Criteria();
193: crit.addEqualTo("workgroupName", workgroupName);
194: QueryByCriteria query = new QueryByCriteria(
195: BaseWorkgroup.class, crit);
196: query.addOrderByDescending("versionNumber");
197: Iterator workgroups = getPersistenceBrokerTemplate()
198: .getCollectionByQuery(query).iterator();
199: while (workgroups.hasNext()) {
200: return (BaseWorkgroup) workgroups.next();
201: }
202: return null;
203: }
204:
205: public BaseWorkgroup findEnrouteWorkgroupById(Long workgroupId) {
206: Criteria crit = new Criteria();
207: crit.addEqualTo("workgroupId", workgroupId);
208: QueryByCriteria query = new QueryByCriteria(
209: BaseWorkgroup.class, crit);
210: query.addOrderByDescending("versionNumber");
211: Iterator workgroups = getPersistenceBrokerTemplate()
212: .getCollectionByQuery(query).iterator();
213: while (workgroups.hasNext()) {
214: return (BaseWorkgroup) workgroups.next();
215: }
216: return null;
217: }
218:
219: public BaseWorkgroup findByDocumentId(Long documentId) {
220: Criteria crit = new Criteria();
221: crit.addEqualTo("documentId", documentId);
222: return (BaseWorkgroup) getPersistenceBrokerTemplate()
223: .getObjectByQuery(
224: new QueryByCriteria(BaseWorkgroup.class, crit));
225: }
226:
227: public List<Long> getImmediateWorkgroupsGroupIds(Long workgroupId) {
228: Criteria crit = new Criteria();
229: crit.addEqualTo("currentInd", Boolean.TRUE);
230: crit.addEqualTo("workgroupMembers.workflowId", workgroupId
231: .toString());
232: crit.addEqualTo("workgroupMembers.memberType",
233: EdenConstants.ACTION_REQUEST_WORKGROUP_RECIPIENT_CD);
234: ReportQueryByCriteria query = QueryFactory.newReportQuery(
235: BaseWorkgroup.class, crit);
236: query.setAttributes(new String[] { "workgroupId" });
237: List<Long> workgroupIds = new ArrayList<Long>(10);
238: Iterator iter = getPersistenceBrokerTemplate()
239: .getReportQueryIteratorByQuery(query);
240: while (iter.hasNext()) {
241: Object[] row = (Object[]) iter.next();
242: BigDecimal id = (BigDecimal) row[0];
243: workgroupIds.add((Long) id.longValue());
244: }
245: return workgroupIds;
246: }
247:
248: }
|