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.lookupable;
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.util.ArrayList;
022: import java.util.Collection;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026: import java.util.Properties;
027:
028: import javax.servlet.http.HttpServletRequest;
029:
030: import org.apache.log4j.Logger;
031:
032: import edu.iu.uis.eden.EdenConstants;
033: import edu.iu.uis.eden.KEWServiceLocator;
034: import edu.iu.uis.eden.doctype.DocumentType;
035: import edu.iu.uis.eden.doctype.DocumentTypeService;
036: import edu.iu.uis.eden.export.ExportDataSet;
037: import edu.iu.uis.eden.export.ExportFormat;
038: import edu.iu.uis.eden.export.Exportable;
039: import edu.iu.uis.eden.plugin.attributes.WorkflowLookupable;
040: import edu.iu.uis.eden.util.ClassLoaderUtils;
041: import edu.iu.uis.eden.util.KeyLabelPair;
042: import edu.iu.uis.eden.util.Utilities;
043:
044: /**
045: * A {@link WorkflowLookupable} implementation for lookup of DocumentTypes.
046: *
047: * @see DocumentType
048: * @see DocumentTypeService
049: *
050: * @author jhopf
051: * @author rkirkend
052: * @author ewestfal
053: */
054: public class DocumentTypeLookupableImpl implements WorkflowLookupable,
055: Exportable {
056: private static final Logger LOG = Logger
057: .getLogger(DocumentTypeLookupableImpl.class);
058:
059: private static final String title = "Lookup a Document type";
060: private static final String returnLocation = "Lookup.do";
061:
062: private static final String PARENT_DOC_TYP_FIELD_LABEL_KEY = "documentTypeLookupable.field.label.parentDocumentType";
063: private static final String CLIMB_HIERARCHY_FIELD_LABEL_KEY = "documentTypeLookupable.field.label.climbHierarchy";
064: private static final String DOC_TYP_NAME_FIELD_LABEL_KEY = "documentTypeLookupable.field.label.documentTypeName";
065: private static final String DOC_TYP_FIELD_LABEL_KEY = "documentTypeLookupable.field.label.documentTypeLabel";
066: private static final String ACTIVE_IND_FIELD_LABEL_KEY = "documentTypeLookupable.field.label.activeIndicator";
067: private static final String DOCUMENT_TYPE_ID_FIELD_LABEL_KEY = "documentTypeLookupable.field.label.documentTypeId";
068:
069: private static final String DOC_TYP_ID_COLUMN_LABEL_KEY = "documentTypeLookupable.column.label.documentTypeId";
070: private static final String DOC_TYP_NAME_COLUMN_LABEL_KEY = "documentTypeLookupable.column.label.documentTypeName";
071: private static final String DOC_TYP_PARENT_COLUMN_LABEL_KEY = "documentTypeLookupable.column.label.documentTypeParent";
072: private static final String DOC_TYP_COLUMN_LABEL_KEY = "documentTypeLookupable.column.label.documentTypeLabel";
073: private static final String ACTIVE_IND_COLUMN_LABEL_KEY = "documentTypeLookupable.column.label.activeIndicator";
074: private static final String ACTION_COLUMN_LABEL_KEY = "documentTypeLookupable.column.label.action";
075:
076: private static final String PARENT_DOC_TYP_FIELD_HELP = "";
077: private static final String CLIMB_HIERARCHY_FIELD_HELP = "";
078: private static final String DOC_TYP_NAME_FIELD_HELP = "";
079: private static final String DOC_TYP_FIELD_HELP = "";
080: private static final String ACTIVE_IND_FIELD_HELP = "";
081: private static final String DOCUMENT_TYPE_ID_FIELD_HELP = "";
082:
083: public static final String CLIMB_HIERARCHY_PROPERTY_NAME = "climbHierarchy";
084: public static final String DOC_TYP_PROPERTY_NAME = "docTypeLabel";
085: public static final String DOC_TYP_NAME_PROPERTY_NAME = "docTypeName";
086: public static final String ACTIVE_IND_PROPERTY_NAME = "activeIndicator";
087: public static final String DOCUMENT_TYPE_ID_PROPERTY_NAME = "documentTypeId";
088:
089: public static final String DOC_TYP_LOOKUPABLE = "DocumentTypeLookupableImplService";
090: public static final String DOC_TYP_FULL_NAME = "docTypeFullName";
091: public static final String BACK_LOCATION_KEY_NAME = "backLocation";
092: public static final String DOC_FORM_KEY_NAME = "docFormKey";
093:
094: private static String PARENT_DOC_TYP_FIELD_LABEL = "Parent Document Type";
095: private static String CLIMB_HIERARCHY_FIELD_LABEL = "Traverse Down Hierarchy";
096: private static String DOC_TYP_NAME_FIELD_LABEL = "Document Type Name";
097: private static String DOC_TYP_FIELD_LABEL = "Document Type Label";
098: private static String ACTIVE_IND_FIELD_LABEL = "Active Indicator";
099: private static String DOCUMENT_TYPE_ID_FIELD_LABEL = "Document Type Id";
100:
101: private static String DOC_TYP_ID_COLUMN_LABEL = "Document Type Id";
102: private static String DOC_TYP_NAME_COLUMN_LABEL = "Document Type Name";
103: private static String DOC_TYP_PARENT_COLUMN_LABEL = "Document Type Parent";
104: private static String DOC_TYP_COLUMN_LABEL = "Document Type Label";
105: private static String ACTIVE_IND_COLUMN_LABEL = "Active";
106: private static String ACTION_COLUMN_LABEL = "Action";
107:
108: // load field/column labels from properties file
109: // needs to be done prior to establishing columns
110: static {
111: loadProperties();
112: }
113:
114: private List rows;
115: private static List columns = establishColumns();
116:
117: /**
118: * DocumentTypeLookupableImpl - constructor that sets up the values of what the form on the jsp will look like.
119: */
120: public DocumentTypeLookupableImpl() {
121: this .rows = establishRows();
122: }
123:
124: /**
125: * This must be done non-statically (see {@link WorkflowLookupable}).
126: *
127: * @return
128: */
129: private List establishRows() {
130: List rows = new ArrayList();
131:
132: List fields = new ArrayList();
133: fields.add(new Field("", "", Field.HIDDEN, true,
134: DOC_TYP_FULL_NAME, "", null, DOC_TYP_LOOKUPABLE));
135: fields.add(new Field(PARENT_DOC_TYP_FIELD_LABEL,
136: PARENT_DOC_TYP_FIELD_HELP, Field.QUICKFINDER, false,
137: DOC_TYP_FULL_NAME, "", null, DOC_TYP_LOOKUPABLE));
138: rows.add(new Row(fields));
139:
140: List options = new ArrayList();
141: options.add(new KeyLabelPair("true", "Yes"));
142: options.add(new KeyLabelPair("false", "No"));
143:
144: fields = new ArrayList();
145: fields.add(new Field(CLIMB_HIERARCHY_FIELD_LABEL,
146: CLIMB_HIERARCHY_FIELD_HELP, Field.RADIO, false,
147: CLIMB_HIERARCHY_PROPERTY_NAME, "false", options, null));
148: rows.add(new Row(fields));
149:
150: fields = new ArrayList();
151: fields.add(new Field(DOC_TYP_NAME_FIELD_LABEL,
152: DOC_TYP_NAME_FIELD_HELP, Field.TEXT, false,
153: DOC_TYP_NAME_PROPERTY_NAME, "", null, null));
154: rows.add(new Row(fields));
155:
156: fields = new ArrayList();
157: fields.add(new Field(DOC_TYP_FIELD_LABEL, DOC_TYP_FIELD_HELP,
158: Field.TEXT, false, DOC_TYP_PROPERTY_NAME, "", null,
159: null));
160: rows.add(new Row(fields));
161:
162: fields = new ArrayList();
163: fields.add(new Field(DOCUMENT_TYPE_ID_FIELD_LABEL,
164: DOCUMENT_TYPE_ID_FIELD_HELP, Field.TEXT, false,
165: DOCUMENT_TYPE_ID_PROPERTY_NAME, "", null, null));
166: rows.add(new Row(fields));
167:
168: options = new ArrayList();
169: options.add(new KeyLabelPair("Y", "Active"));
170: options.add(new KeyLabelPair("N", "Inactive"));
171: options.add(new KeyLabelPair("ALL", "Show All"));
172:
173: fields = new ArrayList();
174: fields.add(new Field(ACTIVE_IND_FIELD_LABEL,
175: ACTIVE_IND_FIELD_HELP, Field.RADIO, false,
176: ACTIVE_IND_PROPERTY_NAME, "ALL", options, null));
177: rows.add(new Row(fields));
178:
179: return rows;
180: }
181:
182: /**
183: * Load the properties from struts ApplicationResources.properties.
184: */
185: private static void loadProperties() {
186: Properties properties = new Properties();
187:
188: InputStream in = ClassLoaderUtils
189: .getDefaultClassLoader()
190: .getResourceAsStream(
191: "edu/iu/uis/eden/ApplicationResources.properties");
192: try {
193: if (in == null) {
194: LOG
195: .warn("edu.iu.uis.eden.ApplicationResources.properties not found in CLASSPATH, using defaults.");
196: } else {
197: properties.load(in);
198: if (properties
199: .getProperty(PARENT_DOC_TYP_FIELD_LABEL_KEY) != null) {
200: PARENT_DOC_TYP_FIELD_LABEL = properties
201: .getProperty(PARENT_DOC_TYP_FIELD_LABEL_KEY);
202: }
203: if (properties
204: .getProperty(CLIMB_HIERARCHY_FIELD_LABEL_KEY) != null) {
205: CLIMB_HIERARCHY_FIELD_LABEL = properties
206: .getProperty(CLIMB_HIERARCHY_FIELD_LABEL_KEY);
207: }
208: if (properties
209: .getProperty(DOC_TYP_NAME_FIELD_LABEL_KEY) != null) {
210: DOC_TYP_NAME_FIELD_LABEL = properties
211: .getProperty(DOC_TYP_NAME_FIELD_LABEL_KEY);
212: }
213: if (properties.getProperty(DOC_TYP_FIELD_LABEL_KEY) != null) {
214: DOC_TYP_FIELD_LABEL = properties
215: .getProperty(DOC_TYP_FIELD_LABEL_KEY);
216: }
217: if (properties.getProperty(ACTIVE_IND_FIELD_LABEL_KEY) != null) {
218: ACTIVE_IND_FIELD_LABEL = properties
219: .getProperty(ACTIVE_IND_FIELD_LABEL_KEY);
220: }
221: if (properties
222: .getProperty(DOCUMENT_TYPE_ID_FIELD_LABEL_KEY) != null) {
223: DOCUMENT_TYPE_ID_FIELD_LABEL = properties
224: .getProperty(DOCUMENT_TYPE_ID_FIELD_LABEL_KEY);
225: }
226:
227: if (properties.getProperty(DOC_TYP_ID_COLUMN_LABEL_KEY) != null) {
228: DOC_TYP_ID_COLUMN_LABEL = properties
229: .getProperty(DOC_TYP_ID_COLUMN_LABEL_KEY);
230: }
231: if (properties
232: .getProperty(DOC_TYP_NAME_COLUMN_LABEL_KEY) != null) {
233: DOC_TYP_NAME_COLUMN_LABEL = properties
234: .getProperty(DOC_TYP_NAME_COLUMN_LABEL_KEY);
235: }
236: if (properties
237: .getProperty(DOC_TYP_PARENT_COLUMN_LABEL_KEY) != null) {
238: DOC_TYP_PARENT_COLUMN_LABEL = properties
239: .getProperty(DOC_TYP_PARENT_COLUMN_LABEL_KEY);
240: }
241: if (properties.getProperty(DOC_TYP_COLUMN_LABEL_KEY) != null) {
242: DOC_TYP_COLUMN_LABEL = properties
243: .getProperty(DOC_TYP_COLUMN_LABEL_KEY);
244: }
245: if (properties.getProperty(ACTIVE_IND_COLUMN_LABEL_KEY) != null) {
246: ACTIVE_IND_COLUMN_LABEL = properties
247: .getProperty(ACTIVE_IND_COLUMN_LABEL_KEY);
248: }
249: if (properties.getProperty(ACTION_COLUMN_LABEL_KEY) != null) {
250: ACTION_COLUMN_LABEL = properties
251: .getProperty(ACTION_COLUMN_LABEL_KEY);
252: }
253:
254: }
255: } catch (IOException e) {
256: LOG
257: .error(
258: "Error loading edu.iu.uis.eden.ApplicationResources.properties, using defaults.",
259: e);
260: } finally {
261: if (in != null) {
262: try {
263: in.close();
264: } catch (IOException e) {
265: // Ignore IOException at this point
266: }
267: }
268: }
269:
270: }
271:
272: private static List establishColumns() {
273: List columns = new ArrayList();
274:
275: Column column = new Column(DOC_TYP_ID_COLUMN_LABEL, "true",
276: "documentTypeId");
277: column.setType(Column.INTEGER);
278: columns.add(column);
279: columns.add(new Column(DOC_TYP_NAME_COLUMN_LABEL,
280: Column.COLUMN_IS_SORTABLE_VALUE, "name"));
281: columns.add(new Column(DOC_TYP_PARENT_COLUMN_LABEL,
282: Column.COLUMN_IS_SORTABLE_VALUE, "lookupParentName"));
283: columns.add(new Column(DOC_TYP_COLUMN_LABEL,
284: Column.COLUMN_IS_SORTABLE_VALUE, "label"));
285: columns.add(new Column(ACTIVE_IND_COLUMN_LABEL,
286: Column.COLUMN_IS_SORTABLE_VALUE,
287: "docTypeActiveIndicatorDisplayValue"));
288: columns.add(new Column(ACTION_COLUMN_LABEL,
289: Column.COLUMN_NOT_SORTABLE_VALUE, "actionsUrl"));
290: return columns;
291: }
292:
293: public void changeIdToName(Map fieldValues) {
294:
295: }
296:
297: /**
298: * getSearchResults - searches for document type information based on the criteria passed in by the map.
299: *
300: * @return Returns a list of DocumentType objects that match the result.
301: */
302: public List getSearchResults(Map fieldValues, Map fieldConversions)
303: throws Exception {
304: DocumentType documentType = new DocumentType();
305: LOG.debug("fieldValues: " + fieldValues);
306: LOG.debug("fieldConversions: " + fieldConversions);
307: String activeIndicator = (String) fieldValues
308: .get(ACTIVE_IND_PROPERTY_NAME);
309: String docTypeLabel = (String) fieldValues
310: .get(DOC_TYP_PROPERTY_NAME);
311: String parentDocTypeName = (String) fieldValues
312: .get(DOC_TYP_FULL_NAME);
313: Boolean climbHierarchy = Boolean.valueOf((String) fieldValues
314: .get(CLIMB_HIERARCHY_PROPERTY_NAME));
315: String documentTypeId = (String) fieldValues
316: .get(DOCUMENT_TYPE_ID_PROPERTY_NAME);
317: String docTypeName = (String) fieldValues
318: .get(DOC_TYP_NAME_PROPERTY_NAME);
319: String backLocation = (String) fieldValues
320: .get(BACK_LOCATION_KEY_NAME);
321: String docFormKey = (String) fieldValues.get(DOC_FORM_KEY_NAME);
322:
323: String docTypReturn = (String) fieldConversions
324: .get(DOC_TYP_FULL_NAME);
325:
326: if (activeIndicator == null) {
327: activeIndicator = "ALL";
328: } else if (activeIndicator.equals("Y")) {
329: documentType.setActiveInd(new Boolean(true));
330: } else if (activeIndicator.equals("N")) {
331: documentType.setActiveInd(new Boolean(false));
332: }
333: if (docTypeLabel != null && !"".equals(docTypeLabel.trim())) {
334: docTypeLabel = docTypeLabel.replace('*', '%');
335: documentType.setLabel("%" + docTypeLabel.trim() + "%");
336: }
337: if (docTypeName != null && !"".equals(docTypeName.trim())) {
338:
339: documentType.setName(docTypeName.trim());
340: }
341:
342: if (documentTypeId != null && !"".equals(documentTypeId.trim())) {
343: try {
344: documentType.setDocumentTypeId(new Long(documentTypeId
345: .trim()));
346: } catch (NumberFormatException e) {
347: documentType.setDocumentTypeId(new Long(-1));
348: }
349: }
350:
351: LOG.debug("finding...: " + documentType + " "
352: + parentDocTypeName);
353: Collection docTypesFound = KEWServiceLocator
354: .getDocumentTypeService().find(documentType,
355: parentDocTypeName,
356: climbHierarchy.booleanValue());
357: Iterator docTypes = docTypesFound.iterator();
358: List displayList = new ArrayList();
359: while (docTypes.hasNext()) {
360: DocumentType type = (DocumentType) docTypes.next();
361: StringBuffer returnUrl = new StringBuffer("<a href=\"");
362: returnUrl.append(backLocation).append(
363: "?methodToCall=refresh&docFormKey=").append(
364: docFormKey).append("&");
365: if (!Utilities.isEmpty(docTypReturn)) {
366: returnUrl.append(docTypReturn);
367: } else {
368: returnUrl.append(DOC_TYP_FULL_NAME);
369: }
370: returnUrl.append("=").append(type.getName()).append(
371: "\">return value</a>");
372: type.setReturnUrl(returnUrl.toString());
373:
374: StringBuffer actions = new StringBuffer("<a href=\"");
375: actions.append("DocumentType.do?methodToCall=report");
376: actions.append("&docTypeId=").append(
377: type.getDocumentTypeId()).append("\" >report</a>");
378: //actions.append(" | <a href=\"").append("DocumentType.do?methodToCall=edit").append("&documentType.documentTypeId=");
379: //actions.append(type.getDocumentTypeId()).append("\" >edit</a>");
380: type.setActionsUrl(actions.toString());
381: displayList.add(type);
382: }
383: return displayList;
384: }
385:
386: public boolean checkForAdditionalFields(Map fieldValues,
387: HttpServletRequest request) throws Exception {
388: return false;
389: }
390:
391: public List getDefaultReturnType() {
392: List returnTypes = new ArrayList();
393: returnTypes.add(DOC_TYP_FULL_NAME);
394: return returnTypes;
395: }
396:
397: public String getNoReturnParams(Map fieldConversions) {
398: String docTypReturn = (String) fieldConversions
399: .get(DOC_TYP_FULL_NAME);
400:
401: StringBuffer noReturnParams = new StringBuffer("&");
402: if (!Utilities.isEmpty(docTypReturn)) {
403: noReturnParams.append(docTypReturn);
404: } else {
405: noReturnParams.append(DOC_TYP_FULL_NAME);
406: }
407: noReturnParams.append("=");
408: return noReturnParams.toString();
409: }
410:
411: /**
412: * @return Returns the title.
413: */
414: public String getTitle() {
415: return title;
416: }
417:
418: /**
419: * @return Returns the instructions.
420: */
421: public String getLookupInstructions() {
422: return Utilities
423: .getApplicationConstant(EdenConstants.DOCUMENT_TYPE_SEARCH_INSTRUCTION_KEY);
424: }
425:
426: /**
427: * @return Returns the returnLocation.
428: */
429: public String getReturnLocation() {
430: return returnLocation;
431: }
432:
433: /**
434: * @return Returns the columns.
435: */
436: public List getColumns() {
437: return columns;
438: }
439:
440: public String getHtmlMenuBar() {
441: return "";//"<a href=\"DocumentType.do\" >Create new document type</a>";
442: }
443:
444: public List getRows() {
445: return rows;
446: }
447:
448: public ExportDataSet export(ExportFormat format,
449: Object exportCriteria) throws Exception {
450: List searchResults = (List) exportCriteria;
451: ExportDataSet dataSet = new ExportDataSet(format);
452: dataSet.getDocumentTypes().addAll(searchResults);
453: return dataSet;
454: }
455:
456: public List getSupportedExportFormats() {
457: return EdenConstants.STANDARD_FORMATS;
458: }
459:
460: }
|