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.workgroup;
018:
019: import java.util.ArrayList;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Map;
024:
025: import javax.servlet.http.HttpServletRequest;
026:
027: import org.apache.commons.lang.StringUtils;
028: import org.kuali.workflow.attribute.Extension;
029: import org.kuali.workflow.attribute.ExtensionAttribute;
030: import org.kuali.workflow.attribute.ExtensionData;
031: import org.kuali.workflow.workgroup.WorkgroupType;
032: import org.kuali.workflow.workgroup.WorkgroupTypeAttribute;
033:
034: import edu.iu.uis.eden.EdenConstants;
035: import edu.iu.uis.eden.KEWServiceLocator;
036: import edu.iu.uis.eden.WorkflowServiceErrorException;
037: import edu.iu.uis.eden.export.ExportDataSet;
038: import edu.iu.uis.eden.export.ExportFormat;
039: import edu.iu.uis.eden.export.Exportable;
040: import edu.iu.uis.eden.lookupable.Column;
041: import edu.iu.uis.eden.lookupable.Field;
042: import edu.iu.uis.eden.lookupable.MyColumns;
043: import edu.iu.uis.eden.lookupable.Row;
044: import edu.iu.uis.eden.plugin.attributes.WorkflowLookupable;
045: import edu.iu.uis.eden.user.AuthenticationUserId;
046: import edu.iu.uis.eden.user.web.WebWorkflowUser;
047: import edu.iu.uis.eden.util.KeyLabelPair;
048: import edu.iu.uis.eden.util.Utilities;
049: import edu.iu.uis.eden.validation.ValidationContext;
050: import edu.iu.uis.eden.validation.ValidationResults;
051: import edu.iu.uis.eden.web.UrlResolver;
052: import edu.iu.uis.eden.workgroup.web.WebWorkgroup;
053:
054: /**
055: * Default implementation of the Workgroup Lookupable.
056: *
057: * @author ewestfal
058: */
059: public class WorkGroupLookupableImpl implements WorkflowLookupable,
060: Exportable {
061:
062: private List rows;
063: private List columns = establishColumns();
064: private static final String title = "Search for a WorkGroup";
065: private static final String returnLocation = "Lookup.do";
066:
067: private static final String WORKGROUP_NAME_FIELD_LABEL = "WorkGroup Name";
068: private static final String WORKGROUP_TYPE_FIELD_LABEL = "WorkGroup Type";
069: private static final String WORKGROUP_DESCRIPTION_FIELD_LABEL = "WorkGroup Description";
070: private static final String ACTIVE_IND_FIELD_LABEL = "Active Indicator";
071: private static final String WORKGROUP_MEMBER_FIELD_LABEL = "WorkGroup Member";
072: private static final String WORKGROUP_ID_FIELD_LABEL = "WorkGroup Id";
073:
074: private static final String WORKGROUP_NAME_FIELD_HELP = "";
075: private static final String WORKGROUP_TYPE_FIELD_HELP = "";
076: private static final String WORKGROUP_DESCRIPTION_FIELD_HELP = "";
077: private static final String ACTIVE_IND_FIELD_HELP = "";
078: private static final String WORKGROUP_MEMBER_FIELD_HELP = "";
079: private static final String WORKGROUP_ID_FIELD_HELP = "";
080:
081: private static final String WORKGROUP_NAME_PROPERTY_NAME = "workgroupName";
082: private static final String WORKGROUP_TYPE_PROPERTY_NAME = "workgroupType";
083: private static final String WORKGROUP_DESCRIPTION_PROPERTY_NAME = "workgroupDescription";
084: private static final String ACTIVE_IND_PROPERTY_NAME = "activeIndicator";
085: private static final String WORKGROUP_MEMBER_PROPERTY_NAME = "networkId";
086: private static final String WORKGROUP_ID_PROPERTY_NAME = "workgroupId";
087:
088: private static final String USER_LOOKUPABLE = "UserLookupableImplService";
089: private static final String BACK_LOCATION_KEY_NAME = "backLocation";
090: private static final String WORKGROUP_ID = "workgroupId";
091: private static final String DOC_FORM_KEY_NAME = "docFormKey";
092:
093: /**
094: * WorkGroupLookupableImpl - constructor that sets up the values of what the form on the jsp will look like.
095: */
096: public WorkGroupLookupableImpl() {
097: rows = new ArrayList();
098:
099: List fields = new ArrayList();
100: fields.add(new Field(WORKGROUP_NAME_FIELD_LABEL,
101: WORKGROUP_NAME_FIELD_HELP, Field.TEXT, false,
102: WORKGROUP_NAME_PROPERTY_NAME, "", null, null));
103: rows.add(new Row(fields));
104:
105: fields = new ArrayList();
106: fields.add(new Field(WORKGROUP_TYPE_FIELD_LABEL,
107: WORKGROUP_TYPE_FIELD_HELP, Field.DROPDOWN_REFRESH,
108: false, WORKGROUP_TYPE_PROPERTY_NAME, "",
109: getWorkgroupTypeOptions(), null));
110: rows.add(new Row(fields));
111:
112: fields = new ArrayList();
113: fields.add(new Field(WORKGROUP_DESCRIPTION_FIELD_LABEL,
114: WORKGROUP_DESCRIPTION_FIELD_HELP, Field.TEXT, false,
115: WORKGROUP_DESCRIPTION_PROPERTY_NAME, "", null, null));
116: rows.add(new Row(fields));
117:
118: fields = new ArrayList();
119: fields.add(new Field(WORKGROUP_MEMBER_FIELD_LABEL,
120: WORKGROUP_MEMBER_FIELD_HELP, Field.TEXT, true,
121: WORKGROUP_MEMBER_PROPERTY_NAME, "", null,
122: USER_LOOKUPABLE));
123: fields.add(new Field("user", "", Field.QUICKFINDER, false, "",
124: "", null, USER_LOOKUPABLE));
125: rows.add(new Row(fields));
126:
127: fields = new ArrayList();
128: fields.add(new Field(WORKGROUP_ID_FIELD_LABEL,
129: WORKGROUP_ID_FIELD_HELP, Field.TEXT, false,
130: WORKGROUP_ID_PROPERTY_NAME, "", null, null));
131: rows.add(new Row(fields));
132:
133: List options = new ArrayList();
134: options.add(new KeyLabelPair("Y", "Active"));
135: options.add(new KeyLabelPair("N", "Inactive"));
136: options.add(new KeyLabelPair("ALL", "Show All"));
137:
138: fields = new ArrayList();
139: fields.add(new Field(ACTIVE_IND_FIELD_LABEL,
140: ACTIVE_IND_FIELD_HELP, Field.RADIO, false,
141: ACTIVE_IND_PROPERTY_NAME, "ALL", options, null));
142: rows.add(new Row(fields));
143:
144: }
145:
146: private List getWorkgroupTypeOptions() {
147: List options = new ArrayList();
148: options.add(new KeyLabelPair("", ""));
149: options.add(new KeyLabelPair("Default", "Default"));
150: List<WorkgroupType> workgroupTypes = KEWServiceLocator
151: .getWorkgroupTypeService().findAll();
152: for (WorkgroupType workgroupType : workgroupTypes) {
153: options.add(new KeyLabelPair(workgroupType.getName(),
154: workgroupType.getLabel()));
155: }
156: return options;
157: }
158:
159: private static List establishColumns() {
160: List columns = new ArrayList();
161: Column column = new Column("WorkGroup Id",
162: Column.COLUMN_IS_SORTABLE_VALUE, "workgroupId");
163: column.setType(Column.INTEGER);
164: columns.add(column);
165: columns.add(new Column("WorkGroup Name",
166: Column.COLUMN_IS_SORTABLE_VALUE, "workgroupName"));
167: columns.add(new Column("Type", Column.COLUMN_IS_SORTABLE_VALUE,
168: "workgroupType"));
169: columns.add(new Column("Active",
170: Column.COLUMN_IS_SORTABLE_VALUE, "activeIndDisplay"));
171: columns.add(new Column("Actions",
172: Column.COLUMN_NOT_SORTABLE_VALUE, "destinationUrl"));
173: return columns;
174: }
175:
176: public List getDefaultReturnType() {
177: List returnTypes = new ArrayList();
178: returnTypes.add(WORKGROUP_ID);
179: return returnTypes;
180: }
181:
182: public void changeIdToName(Map fieldValues) {
183:
184: }
185:
186: /**
187: * getSearchResults - searches for work group information based on the criteria passed in by the map.
188: *
189: * @return Returns a list of Workgroup objects that match the result.
190: */
191: public List getSearchResults(Map fieldValues, Map fieldConversions)
192: throws Exception {
193: String activeIndicator = (String) fieldValues
194: .get(ACTIVE_IND_PROPERTY_NAME);
195: String workGroupName = (String) fieldValues
196: .get(WORKGROUP_NAME_PROPERTY_NAME);
197: String workGroupType = (String) fieldValues
198: .get(WORKGROUP_TYPE_PROPERTY_NAME);
199: String workGroupDescription = (String) fieldValues
200: .get(WORKGROUP_DESCRIPTION_PROPERTY_NAME);
201: String workGroupMember = (String) fieldValues
202: .get(WORKGROUP_MEMBER_PROPERTY_NAME);
203: String workGroupId = (String) fieldValues
204: .get(WORKGROUP_ID_PROPERTY_NAME);
205: String backLocation = (String) fieldValues
206: .get(BACK_LOCATION_KEY_NAME);
207: String docFormKey = (String) fieldValues.get(DOC_FORM_KEY_NAME);
208:
209: String workGroupReturn = (String) fieldConversions
210: .get(WORKGROUP_ID);
211: String workGroupNameReturn = (String) fieldConversions
212: .get(WORKGROUP_NAME_PROPERTY_NAME);
213:
214: WorkgroupService workgroupService = (WorkgroupService) KEWServiceLocator
215: .getWorkgroupService();
216: WebWorkgroup template = new WebWorkgroup(workgroupService
217: .getBlankWorkgroup());
218:
219: if (workGroupId != null && !"".equals(workGroupId.trim())) {
220: try {
221: template.setWorkflowGroupId(new WorkflowGroupId(
222: new Long(workGroupId.trim())));
223: } catch (NumberFormatException e) {
224: template.setWorkflowGroupId(new WorkflowGroupId(
225: new Long(-1)));
226: }
227: }
228:
229: if (workGroupName != null && !"".equals(workGroupName.trim())) {
230: workGroupName = workGroupName.replace('*', '%');
231: template.setGroupNameId(new GroupNameId("%"
232: + workGroupName.trim() + "%"));
233: }
234:
235: if (!StringUtils.isBlank(workGroupType)) {
236: template.setWorkgroupType(workGroupType);
237: }
238:
239: if (workGroupDescription != null
240: && !"".equals(workGroupDescription.trim())) {
241: workGroupDescription = workGroupDescription.replace('*',
242: '%');
243: template.setDescription("%" + workGroupDescription.trim()
244: + "%");
245: }
246:
247: if (activeIndicator == null || activeIndicator.equals("ALL")) {
248: template.setActiveInd(null);
249: } else if (activeIndicator.equals("Y")) {
250: template.setActiveInd(new Boolean(true));
251: } else if (activeIndicator.equals("N")) {
252: template.setActiveInd(new Boolean(false));
253: }
254:
255: MyColumns myColumns = new MyColumns();
256: Map attributes = handleAttributeSearchFields(myColumns,
257: fieldValues, workGroupType);
258:
259: Iterator workGroups = null;
260: if (workGroupMember != null
261: && !"".equals(workGroupMember.trim())) {
262: WebWorkflowUser userTemplate = new WebWorkflowUser(
263: KEWServiceLocator.getUserService().getBlankUser());
264: userTemplate
265: .setAuthenticationUserId(new AuthenticationUserId(
266: workGroupMember.trim()));
267: workGroups = workgroupService.search(template, attributes,
268: userTemplate).iterator();
269: } else {
270: workGroups = workgroupService.search(template, attributes,
271: true).iterator();
272: }
273:
274: List displayList = new ArrayList();
275: while (workGroups.hasNext()) {
276: Workgroup prototype = (Workgroup) workGroups.next();
277: WebWorkgroup workGroup = new WebWorkgroup(prototype);
278:
279: if (!StringUtils.isEmpty(workGroupType)
280: && !"Default".equals(workGroupType)) {
281: MyColumns myNewColumns = new MyColumns();
282: for (Iterator iter = myColumns.getColumns().iterator(); iter
283: .hasNext();) {
284: KeyLabelPair pair = (KeyLabelPair) iter.next();
285: KeyLabelPair newPair = new KeyLabelPair();
286: newPair.setKey(pair.getKey());
287: String extensionValue = getExtensionValue(
288: workGroup, pair.getKey().toString());
289: if (!StringUtils.isEmpty(extensionValue)) {
290: newPair.setLabel(extensionValue);
291: } else {
292: newPair
293: .setLabel(EdenConstants.HTML_NON_BREAKING_SPACE);
294: }
295: myNewColumns.getColumns().add(newPair);
296: }
297: workGroup.setMyColumns(myNewColumns);
298: }
299:
300: StringBuffer returnUrl = new StringBuffer("<a href=\"");
301: returnUrl.append(backLocation).append(
302: "?methodToCall=refresh&docFormKey=").append(
303: docFormKey).append("&");
304: if (!Utilities.isEmpty(workGroupReturn)) {
305: returnUrl.append(workGroupReturn);
306: } else {
307: returnUrl.append(WORKGROUP_ID);
308: }
309: returnUrl.append("=").append(
310: workGroup.getWorkflowGroupId().getGroupId())
311: .append("&");
312: if (!Utilities.isEmpty(workGroupNameReturn)) {
313: returnUrl.append(workGroupNameReturn);
314: } else {
315: returnUrl.append(WORKGROUP_NAME_PROPERTY_NAME);
316: }
317: returnUrl.append("=").append(workGroup.getWorkgroupName());
318:
319: returnUrl.append("\">return value</a>");
320: workGroup.setReturnUrl(returnUrl.toString());
321:
322: WorkgroupCapabilities caps = workgroupService
323: .getCapabilities();
324: int actionsAvailable = 0;
325: StringBuffer buffer = new StringBuffer();
326: if (caps.isReportSupported()) {
327: buffer.append("<a href=\"");
328: buffer.append(
329: UrlResolver.getInstance()
330: .getWorkgroupReportUrl()
331: + "?methodToCall=report&workgroupId=")
332: .append(
333: workGroup.getWorkflowGroupId()
334: .getGroupId());
335: buffer.append("&showEdit=yes").append("\" >").append(
336: "report").append("</a>");
337: actionsAvailable++;
338: }
339:
340: if (caps.isEditSupported()) {
341: if (actionsAvailable > 0) {
342: buffer.append(" | ");
343: }
344: buffer.append("<a href=\"");
345: buffer.append(
346: UrlResolver.getInstance().getWorkgroupUrl()
347: + "?methodToCall=edit&workgroupId=")
348: .append(
349: workGroup.getWorkflowGroupId()
350: .getGroupId());
351: buffer.append("&showEdit=yes").append("\" >").append(
352: "edit").append("</a>");
353: actionsAvailable++;
354: }
355:
356: if (actionsAvailable == 0) {
357: buffer.append("No actions available");
358: }
359:
360: workGroup.setDestinationUrl(buffer.toString());
361: displayList.add(workGroup);
362: }
363: return displayList;
364: }
365:
366: protected String getExtensionValue(Workgroup workgroup, String key) {
367: for (Extension extension : workgroup.getExtensions()) {
368: for (ExtensionData data : extension.getData()) {
369: if (key.equals(data.getKey())) {
370: return data.getValue();
371: }
372: }
373: }
374: return null;
375: }
376:
377: protected Map handleAttributeSearchFields(MyColumns myColumns,
378: Map fieldValues, String workgroupTypeName) {
379: Map<String, String> attributes = null;
380: if (!StringUtils.isBlank(workgroupTypeName)
381: && !"Default".equals(workgroupTypeName)) {
382: WorkgroupType workgroupType = KEWServiceLocator
383: .getWorkgroupTypeService().findByName(
384: workgroupTypeName);
385: List<Object> errors = new ArrayList<Object>();
386: for (WorkgroupTypeAttribute workgroupTypeAttribute : workgroupType
387: .getActiveAttributes()) {
388: attributes = new HashMap<String, String>();
389: Object attributeObject = workgroupTypeAttribute
390: .loadAttribute();
391: if (attributeObject instanceof ExtensionAttribute) {
392: ExtensionAttribute attribute = (ExtensionAttribute) attributeObject;
393: List<Row> searchRows = attribute.getRows();
394: for (Row row : searchRows) {
395: for (Field field : row.getFields()) {
396: if (fieldValues
397: .get(field.getPropertyName()) != null) {
398: String attributeParam = (String) fieldValues
399: .get(field.getPropertyName());
400: if (!attributeParam.equals("")) {
401: //if (!Utilities.isEmpty(field.getDefaultLookupableName())) {
402: // attributes.put(field.getDefaultLookupableName(), attributeParam.trim());
403: //} else {
404: attributes.put(field
405: .getPropertyName(),
406: attributeParam.trim());
407: //}
408: }
409: }
410: if (field.isInputField()
411: && !Field.HIDDEN.equals(field
412: .getFieldType())) {
413: //if (!Utilities.isEmpty(field.getDefaultLookupableName())) {
414: // myColumns.getColumns().add(new KeyLabelPair(field.getDefaultLookupableName(), workgroupTypeAttribute.getWorkgroupTypeAttributeId()+""));
415: //} else {
416: myColumns
417: .getColumns()
418: .add(
419: new KeyLabelPair(
420: field
421: .getPropertyName(),
422: workgroupTypeAttribute
423: .getWorkgroupTypeAttributeId()
424: + ""));
425: //}
426: }
427: }
428: }
429: ValidationContext validationContext = new ValidationContext();
430: validationContext.getParameters().put("extensions",
431: attributes);
432: validationContext.getParameters().put("operation",
433: "search");
434: ValidationResults results = attribute
435: .validate(validationContext);
436: if (results != null
437: && !results.getValidationResults()
438: .isEmpty()) {
439: errors.add(results);
440: }
441: }
442: }
443: if (!errors.isEmpty()) {
444: throw new WorkflowServiceErrorException(
445: "Workgroup search errors", errors);
446: }
447: }
448: return attributes;
449: }
450:
451: public boolean checkForAdditionalFields(Map fieldValues,
452: HttpServletRequest request) throws Exception {
453: String workgroupTypeName = (String) fieldValues
454: .get(WORKGROUP_TYPE_PROPERTY_NAME);
455:
456: // copy the field values into a Map for validation
457: // Map<String, String> extensions = new HashMap<String, String>();
458: // for (Object key : fieldValues.keySet()) {
459: // if (key instanceof String) {
460: // Object value = fieldValues.get(key);
461: // if (value instanceof String) {
462: // extensions.put((String)key, (String)value);
463: // }
464: // }
465: // }
466:
467: if (!StringUtils.isBlank(workgroupTypeName)
468: && !"Default".equals(workgroupTypeName)) {
469: WorkgroupType workgroupType = KEWServiceLocator
470: .getWorkgroupTypeService().findByName(
471: workgroupTypeName);
472:
473: int i = 0;
474: List<Object> errors = new ArrayList<Object>();
475: for (WorkgroupTypeAttribute workgroupTypeAttribute : workgroupType
476: .getActiveAttributes()) {
477: Object attributeObject = workgroupTypeAttribute
478: .loadAttribute();
479: if (attributeObject instanceof ExtensionAttribute) {
480: ExtensionAttribute attribute = (ExtensionAttribute) attributeObject;
481: List<Row> searchRows = attribute.getRows();
482: for (Row searchRow : searchRows) {
483: for (Field searchField : searchRow.getFields()) {
484: if (request.getParameter(searchField
485: .getPropertyName()) != null) {
486: searchField.setPropertyValue(request
487: .getParameter(searchField
488: .getPropertyName()));
489: }
490: fieldValues.put(searchField
491: .getPropertyName(), searchField
492: .getPropertyValue());
493: }
494: }
495: // ValidationContext validationContext = new ValidationContext();
496: // validationContext.getParameters().put("extensions", extensions);
497: // ValidationResults validationResults = attribute.validate(validationContext);
498: // if (validationResults != null) {
499: // errors.add(validationResults);
500: // }
501:
502: // new refetch the searchRows (not sure why, modeled after RuleBaseValuesLookupable)
503: searchRows = attribute.getRows();
504:
505: for (Row searchRow : searchRows) {
506: List<Field> fields = new ArrayList<Field>();
507: for (Field searchField : searchRow.getFields()) {
508: if (request.getParameter(searchField
509: .getPropertyName()) != null) {
510: searchField.setPropertyValue(request
511: .getParameter(searchField
512: .getPropertyName()));
513: }
514: fields.add(searchField);
515: fieldValues.put(searchField
516: .getPropertyName(), searchField
517: .getPropertyValue());
518: }
519: searchRow.setFields(fields);
520: rows.add(searchRow);
521: }
522:
523: if (attribute.getRows() != null) {
524: for (Row row : attribute.getRows()) {
525: for (Field field : row.getFields()) {
526: if (field.isInputField()
527: && !Field.HIDDEN.equals(field
528: .getFieldType())) {
529: getColumns()
530: .add(
531: i + 4,
532: new Column(
533: field
534: .getFieldLabel(),
535: Column.COLUMN_IS_SORTABLE_VALUE,
536: "myColumns.columns["
537: + i
538: + "].label"));
539: i++;
540: }
541: }
542: }
543: }
544:
545: }
546: }
547: if (!errors.isEmpty()) {
548: throw new WorkflowServiceErrorException(
549: "Workgroup search errors", errors);
550: }
551:
552: return true;
553: }
554: return false;
555: }
556:
557: public String getNoReturnParams(Map fieldConversions) {
558: String workGroupReturn = (String) fieldConversions
559: .get(WORKGROUP_ID);
560: String workGroupNameReturn = (String) fieldConversions
561: .get(WORKGROUP_NAME_PROPERTY_NAME);
562: StringBuffer noReturnParams = new StringBuffer("&");
563: if (!Utilities.isEmpty(workGroupReturn)) {
564: noReturnParams.append(workGroupReturn);
565: } else {
566: noReturnParams.append(WORKGROUP_ID);
567: }
568: noReturnParams.append("=&");
569: if (!Utilities.isEmpty(workGroupNameReturn)) {
570: noReturnParams.append(workGroupNameReturn);
571: } else {
572: noReturnParams.append(WORKGROUP_NAME_PROPERTY_NAME);
573: }
574: noReturnParams.append("=");
575: return noReturnParams.toString();
576: }
577:
578: /**
579: * @return Returns the instructions.
580: */
581: public String getLookupInstructions() {
582: return Utilities
583: .getApplicationConstant(EdenConstants.WORKGROUP_SEARCH_INSTRUCTION_KEY);
584: }
585:
586: /**
587: * @return Returns the title.
588: */
589: public String getTitle() {
590: return title;
591: }
592:
593: /**
594: * @return Returns the returnLocation.
595: */
596: public String getReturnLocation() {
597: return returnLocation;
598: }
599:
600: /**
601: * @return Returns the columns.
602: */
603: public List getColumns() {
604: return columns;
605: }
606:
607: public String getHtmlMenuBar() {
608: if (!KEWServiceLocator.getWorkgroupService().getCapabilities()
609: .isCreateSupported()) {
610: return "";
611: }
612: return "<a href=\""
613: + UrlResolver.getInstance().getWorkgroupUrl()
614: + "\" >Create new workgroup</a>";
615: }
616:
617: public List getRows() {
618: return rows;
619: }
620:
621: public ExportDataSet export(ExportFormat format,
622: Object exportCriteria) throws Exception {
623: List searchResults = (List) exportCriteria;
624: ExportDataSet dataSet = new ExportDataSet(format);
625: dataSet.getWorkgroups().addAll(searchResults);
626: return dataSet;
627: }
628:
629: public List getSupportedExportFormats() {
630: return EdenConstants.STANDARD_FORMATS;
631: }
632:
633: }
|