001: /*
002: * Copyright 2005-2006 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.actionlist.dao;
018:
019: import java.sql.Timestamp;
020: import java.util.Collection;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.Map;
024:
025: import org.apache.ojb.broker.query.Criteria;
026: import org.apache.ojb.broker.query.QueryByCriteria;
027: import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
028:
029: import edu.iu.uis.eden.EdenConstants;
030: import edu.iu.uis.eden.actionitem.ActionItem;
031: import edu.iu.uis.eden.actionitem.ActionItemActionListExtension;
032: import edu.iu.uis.eden.actionlist.ActionListFilter;
033: import edu.iu.uis.eden.user.WorkflowUser;
034:
035: /**
036: * OJB implementation of the {@link ActionListDAO}.
037: *
038: * @author rkirkend
039: * @author temay
040: */
041: public class ActionListDAOOjbImpl extends PersistenceBrokerDaoSupport
042: implements ActionListDAO {
043:
044: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
045: .getLogger(ActionListDAOOjbImpl.class);
046:
047: public Collection getActionList(WorkflowUser workflowUser,
048: ActionListFilter filter) {
049: LOG.info("getting action list for user "
050: + workflowUser.getWorkflowUserId().getWorkflowId());
051: Criteria crit = new Criteria();
052: crit.addEqualTo("workflowId", workflowUser.getWorkflowUserId()
053: .getWorkflowId());
054: if (filter != null) {
055: setUpActionListCriteria(crit, filter);
056: }
057: LOG.info("running query to get action list for user "
058: + workflowUser.getWorkflowUserId().getWorkflowId());
059: Collection collection = this .getPersistenceBrokerTemplate()
060: .getCollectionByQuery(
061: new QueryByCriteria(
062: ActionItemActionListExtension.class,
063: crit));
064: LOG.info("finished running query to get action list for user "
065: + workflowUser.getWorkflowUserId().getWorkflowId());
066: return createActionList(collection);
067: }
068:
069: private void setUpActionListCriteria(Criteria crit,
070: ActionListFilter filter) {
071: LOG.info("setting up Action List criteria");
072: boolean filterOn = false;
073: String filteredByItems = "";
074: if (filter.getActionRequestCd() != null
075: && !"".equals(filter.getActionRequestCd().trim())
076: && !filter.getActionRequestCd().equals(
077: EdenConstants.ALL_CODE)) {
078: if (filter.isExcludeActionRequestCd()) {
079: crit.addNotEqualTo("actionRequestCd", filter
080: .getActionRequestCd());
081: } else {
082: crit.addEqualTo("actionRequestCd", filter
083: .getActionRequestCd());
084: }
085: filteredByItems += filteredByItems.length() > 0 ? ", " : "";
086: filteredByItems += "Action Requested";
087: }
088:
089: if (filter.getCreateDateFrom() != null
090: || filter.getCreateDateTo() != null) {
091: if (filter.isExcludeCreateDate()) {
092: if (filter.getCreateDateFrom() != null
093: && filter.getCreateDateTo() != null) {
094: crit.addNotBetween("routeHeader.createDate",
095: new Timestamp(filter.getCreateDateFrom()
096: .getTime()), new Timestamp(filter
097: .getCreateDateTo().getTime()));
098: } else if (filter.getCreateDateFrom() != null
099: && filter.getCreateDateTo() == null) {
100: crit.addLessOrEqualThan("routeHeader.createDate",
101: new Timestamp(filter.getCreateDateFrom()
102: .getTime()));
103: } else if (filter.getCreateDateFrom() == null
104: && filter.getCreateDateTo() != null) {
105: crit
106: .addGreaterOrEqualThan(
107: "routeHeader.createDate",
108: new Timestamp(filter
109: .getCreateDateTo()
110: .getTime()));
111: }
112: } else {
113: if (filter.getCreateDateFrom() != null
114: && filter.getCreateDateTo() != null) {
115: crit.addBetween("routeHeader.createDate",
116: new Timestamp(filter.getCreateDateFrom()
117: .getTime()), new Timestamp(filter
118: .getCreateDateTo().getTime()));
119: } else if (filter.getCreateDateFrom() != null
120: && filter.getCreateDateTo() == null) {
121: crit.addGreaterOrEqualThan(
122: "routeHeader.createDate", new Timestamp(
123: filter.getCreateDateFrom()
124: .getTime()));
125: } else if (filter.getCreateDateFrom() == null
126: && filter.getCreateDateTo() != null) {
127: crit.addLessOrEqualThan("routeHeader.createDate",
128: new Timestamp(filter.getCreateDateTo()
129: .getTime()));
130: }
131: }
132: filteredByItems += filteredByItems.length() > 0 ? ", " : "";
133: filteredByItems += "Date Created";
134: }
135:
136: if (filter.getDocRouteStatus() != null
137: && !"".equals(filter.getDocRouteStatus().trim())
138: && !filter.getDocRouteStatus().equals(
139: EdenConstants.ALL_CODE)) {
140: if (filter.isExcludeRouteStatus()) {
141: crit.addNotEqualTo("routeHeader.docRouteStatus", filter
142: .getDocRouteStatus());
143: } else {
144: crit.addEqualTo("routeHeader.docRouteStatus", filter
145: .getDocRouteStatus());
146: }
147: filteredByItems += filteredByItems.length() > 0 ? ", " : "";
148: filteredByItems += "Document Route Status";
149: }
150:
151: if (filter.getDocumentTitle() != null
152: && !"".equals(filter.getDocumentTitle().trim())) {
153: String docTitle = filter.getDocumentTitle();
154: if (docTitle.trim().endsWith("*")) {
155: docTitle = docTitle.substring(0, docTitle.length() - 1);
156: }
157:
158: if (filter.isExcludeDocumentTitle()) {
159: crit.addNotLike("docTitle", "%" + docTitle + "%");
160: } else {
161: crit.addLike("docTitle", "%" + docTitle + "%");
162: }
163: filteredByItems += filteredByItems.length() > 0 ? ", " : "";
164: filteredByItems += "Document Title";
165: }
166:
167: if (filter.getDocumentType() != null
168: && !"".equals(filter.getDocumentType().trim())) {
169: if (filter.isExcludeDocumentType()) {
170: crit.addNotLike("docName", "%"
171: + filter.getDocumentType() + "%");
172: } else {
173: crit.addLike("docName", "%" + filter.getDocumentType()
174: + "%");
175: }
176: filteredByItems += filteredByItems.length() > 0 ? ", " : "";
177: filteredByItems += "Document Type";
178: }
179:
180: if (filter.getLastAssignedDateFrom() != null
181: || filter.getLastAssignedDateTo() != null) {
182: if (filter.isExcludeLastAssignedDate()) {
183: if (filter.getLastAssignedDateFrom() != null
184: && filter.getLastAssignedDateTo() != null) {
185: crit
186: .addNotBetween("dateAssigned",
187: new Timestamp(filter
188: .getLastAssignedDateFrom()
189: .getTime()),
190: new Timestamp(filter
191: .getLastAssignedDateTo()
192: .getTime()));
193: } else if (filter.getLastAssignedDateFrom() != null
194: && filter.getLastAssignedDateTo() == null) {
195: crit.addLessOrEqualThan("dateAssigned",
196: new Timestamp(filter
197: .getLastAssignedDateFrom()
198: .getTime()));
199: } else if (filter.getLastAssignedDateFrom() == null
200: && filter.getLastAssignedDateTo() != null) {
201: crit
202: .addGreaterOrEqualThan("dateAssigned",
203: new Timestamp(filter
204: .getLastAssignedDateTo()
205: .getTime()));
206: }
207: } else {
208: if (filter.getLastAssignedDateFrom() != null
209: && filter.getLastAssignedDateTo() != null) {
210: crit
211: .addBetween("dateAssigned", new Timestamp(
212: filter.getLastAssignedDateFrom()
213: .getTime()), new Timestamp(
214: filter.getLastAssignedDateTo()
215: .getTime()));
216: } else if (filter.getLastAssignedDateFrom() != null
217: && filter.getLastAssignedDateTo() == null) {
218: crit.addGreaterOrEqualThan("dateAssigned",
219: new Timestamp(filter
220: .getLastAssignedDateFrom()
221: .getTime()));
222: } else if (filter.getLastAssignedDateFrom() == null
223: && filter.getLastAssignedDateTo() != null) {
224: crit
225: .addLessOrEqualThan("dateAssigned",
226: new Timestamp(filter
227: .getLastAssignedDateTo()
228: .getTime()));
229: }
230: }
231: filteredByItems += filteredByItems.length() > 0 ? ", " : "";
232: filteredByItems += "Date Last Assigned";
233: }
234:
235: filter.setWorkgroupId(null);
236: if (filter.getWorkgroupIdString() != null
237: && !"".equals(filter.getWorkgroupIdString().trim())
238: && !filter.getWorkgroupIdString().trim().equals(
239: EdenConstants.NO_FILTERING)) {
240: filter.setWorkgroupId(new Long(filter
241: .getWorkgroupIdString().trim()));
242: if (filter.isExcludeWorkgroupId()) {
243: Criteria critNotEqual = new Criteria();
244: critNotEqual.addNotEqualTo("workgroupId", filter
245: .getWorkgroupId());
246: Criteria critNull = new Criteria();
247: critNull.addIsNull("workgroupId");
248: critNotEqual.addOrCriteria(critNull);
249: crit.addAndCriteria(critNotEqual);
250: } else {
251: crit.addEqualTo("workgroupId", filter.getWorkgroupId());
252: }
253: filteredByItems += filteredByItems.length() > 0 ? ", " : "";
254: filteredByItems += "Action Request Workgroup";
255: }
256:
257: if (filteredByItems.length() > 0) {
258: filterOn = true;
259: }
260:
261: if (filter.getDelegatorId() != null
262: && !"".equals(filter.getDelegatorId().trim())
263: && !filter.getDelegatorId().trim().equals(
264: EdenConstants.DELEGATION_DEFAULT)
265: && !filter.getDelegatorId().trim().equals(
266: EdenConstants.ALL_CODE)) {
267: filter
268: .setDelegationType(EdenConstants.DELEGATION_SECONDARY);
269: filter.setExcludeDelegationType(false);
270: Criteria userCrit = new Criteria();
271: Criteria groupCrit = new Criteria();
272: if (filter.isExcludeDelegatorId()) {
273: Criteria userNull = new Criteria();
274: userCrit.addNotEqualTo("delegatorWorkflowId", filter
275: .getDelegatorId());
276: userNull.addIsNull("delegatorWorkflowId");
277: userCrit.addOrCriteria(userNull);
278: Criteria groupNull = new Criteria();
279: groupCrit.addNotEqualTo("delegatorWorkgroupId", filter
280: .getDelegatorId());
281: groupNull.addIsNull("delegatorWorkgroupId");
282: groupCrit.addOrCriteria(groupNull);
283: crit.addAndCriteria(userCrit);
284: crit.addAndCriteria(groupCrit);
285: } else {
286: Criteria orCrit = new Criteria();
287: userCrit.addEqualTo("delegatorWorkflowId", filter
288: .getDelegatorId());
289: groupCrit.addEqualTo("delegatorWorkgroupId", filter
290: .getDelegatorId());
291: orCrit.addOrCriteria(userCrit);
292: orCrit.addOrCriteria(groupCrit);
293: crit.addAndCriteria(orCrit);
294: }
295: filteredByItems += filteredByItems.length() > 0 ? ", " : "";
296: filteredByItems += "Delegator Id";
297: filterOn = true;
298: } else if (filter.getDelegatorId().trim().equals(
299: EdenConstants.DELEGATION_DEFAULT)) {
300: filter
301: .setDelegationType(EdenConstants.DELEGATION_SECONDARY);
302: filter.setExcludeDelegationType(true);
303: } else if (filter.getDelegatorId().trim().equals(
304: EdenConstants.ALL_CODE)) {
305: filter
306: .setDelegationType(EdenConstants.DELEGATION_SECONDARY);
307: filter.setExcludeDelegationType(false);
308: filteredByItems += filteredByItems.length() > 0 ? ", " : "";
309: filteredByItems += "Delegator Id";
310: filterOn = true;
311: }
312:
313: //must come after delegation id since the delegation choices are all secondary delegations
314: if (filter.getDelegationType() != null
315: && !"".equals(filter.getDelegationType().trim())) {
316: if (filter.isExcludeDelegationType()) {
317: Criteria critNotEqual = new Criteria();
318: Criteria critNull = new Criteria();
319: critNotEqual.addNotEqualTo("delegationType", filter
320: .getDelegationType());
321: critNull.addIsNull("delegationType");
322: critNotEqual.addOrCriteria(critNull);
323: crit.addAndCriteria(critNotEqual);
324: } else {
325: crit.addEqualTo("delegationType", filter
326: .getDelegationType());
327: }
328: }
329:
330: if (!"".equals(filteredByItems)) {
331: filteredByItems = "Filtered by " + filteredByItems;
332: }
333: filter.setFilterLegend(filteredByItems);
334: filter.setFilterOn(filterOn);
335:
336: LOG.info("returning from Action List criteria");
337: }
338:
339: /**
340: * Creates an Action List from the given collection of Action Items. The Action List should
341: * contain only one action item per document. The action item chosen should be the most "critical"
342: * or "important" one on the document.
343: *
344: * @return the Action List as a Collection of ActionItems
345: */
346: private Collection createActionList(Collection actionItems) {
347: Map actionItemMap = new HashMap();
348: ActionListPriorityComparator comparator = new ActionListPriorityComparator();
349: for (Iterator iterator = actionItems.iterator(); iterator
350: .hasNext();) {
351: ActionItem potentialActionItem = (ActionItem) iterator
352: .next();
353: ActionItem existingActionItem = (ActionItem) actionItemMap
354: .get(potentialActionItem.getRouteHeaderId());
355: if (existingActionItem == null
356: || comparator.compare(potentialActionItem,
357: existingActionItem) > 0) {
358: actionItemMap.put(potentialActionItem
359: .getRouteHeaderId(), potentialActionItem);
360: }
361: }
362: return actionItemMap.values();
363: }
364: }
|