View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kim.lookup;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.kew.api.KewApiConstants;
20  import org.kuali.rice.kim.api.KimConstants;
21  import org.kuali.rice.kim.api.identity.Person;
22  import org.kuali.rice.kim.impl.KIMPropertyConstants;
23  import org.kuali.rice.kim.impl.identity.PersonImpl;
24  import org.kuali.rice.kim.util.KimCommonUtilsInternal;
25  import org.kuali.rice.kns.lookup.HtmlData;
26  import org.kuali.rice.kns.web.ui.Field;
27  import org.kuali.rice.kns.web.ui.Row;
28  import org.kuali.rice.krad.bo.BusinessObject;
29  import org.kuali.rice.krad.util.KRADConstants;
30  import org.kuali.rice.krad.util.UrlFactory;
31  
32  import java.util.ArrayList;
33  import java.util.Iterator;
34  import java.util.List;
35  import java.util.Map;
36  import java.util.Properties;
37  
38  /**
39   * This is a description of what this class does - shyu don't forget to fill this in. 
40   * 
41   * @author Kuali Rice Team (rice.collab@kuali.org)
42   *
43   */
44  public class PersonLookupableHelperServiceImpl  extends KimLookupableHelperServiceImpl {
45  	
46  	private static final long serialVersionUID = 1971744785776844579L;
47  	
48  	@Override
49  	public List<? extends BusinessObject> getSearchResults(
50  			Map<String, String> fieldValues) {
51  		if (fieldValues != null && StringUtils.isNotEmpty(fieldValues.get(KIMPropertyConstants.Person.PRINCIPAL_NAME))) {
52  			fieldValues.put(KIMPropertyConstants.Person.PRINCIPAL_NAME, fieldValues.get(KIMPropertyConstants.Person.PRINCIPAL_NAME).toLowerCase());
53  		}
54  		return super.getSearchResults(fieldValues);
55  	}
56  
57  	@SuppressWarnings("unchecked")
58  	@Override
59  	public List<HtmlData> getCustomActionUrls(BusinessObject bo, List pkNames) {
60          List<HtmlData> anchorHtmlDataList = new ArrayList<HtmlData>();
61  		if(allowsNewOrCopyAction(KimConstants.KimUIConstants.KIM_PERSON_DOCUMENT_TYPE_NAME)){
62  			String href = "";
63  			Properties parameters = new Properties();
64  	        parameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.DOC_HANDLER_METHOD);
65  	        parameters.put(KRADConstants.PARAMETER_COMMAND, KewApiConstants.INITIATE_COMMAND);
66  	        parameters.put(KRADConstants.DOCUMENT_TYPE_NAME, KimConstants.KimUIConstants.KIM_PERSON_DOCUMENT_TYPE_NAME);
67  	        parameters.put(KimConstants.PrimaryKeyConstants.PRINCIPAL_ID, ((PersonImpl)bo).getPrincipalId());
68  	        if (StringUtils.isNotBlank(getReturnLocation())) {
69  	        	parameters.put(KRADConstants.RETURN_LOCATION_PARAMETER, getReturnLocation());
70  			}
71  	        href = UrlFactory.parameterizeUrl(KimCommonUtilsInternal.getKimBasePath()+KimConstants.KimUIConstants.KIM_PERSON_DOCUMENT_ACTION, parameters);
72  	
73  	        HtmlData.AnchorHtmlData anchorHtmlData = new HtmlData.AnchorHtmlData(href,
74  	        		KRADConstants.DOC_HANDLER_METHOD, KRADConstants.MAINTENANCE_EDIT_METHOD_TO_CALL);
75  	
76  	    	anchorHtmlDataList.add(anchorHtmlData);
77  		}
78      	return anchorHtmlDataList;
79  	}
80  
81  	@Override
82  	public HtmlData getInquiryUrl(BusinessObject bo, String propertyName) {
83  		HtmlData inqUrl = super.getInquiryUrl(bo, propertyName);
84  		Properties parameters = new Properties();
85          parameters.put(KewApiConstants.COMMAND_PARAMETER, KewApiConstants.INITIATE_COMMAND);
86          parameters.put(KRADConstants.DOCUMENT_TYPE_NAME, KimConstants.KimUIConstants.KIM_PERSON_DOCUMENT_TYPE_NAME);
87          parameters.put(KimConstants.PrimaryKeyConstants.PRINCIPAL_ID, ((Person)bo).getPrincipalId());
88          String href = UrlFactory.parameterizeUrl(KimCommonUtilsInternal.getKimBasePath()+ KimConstants.KimUIConstants.KIM_PERSON_INQUIRY_ACTION, parameters);
89  	    ((HtmlData.AnchorHtmlData)inqUrl).setHref(href);
90  	    return inqUrl;
91  	}
92  	
93  	/**
94  	 * Checks for the special role lookup parameters and removes/marks read-only the fields in the search criteria.
95  	 * If present, this method also has a side-effect of updating the title with the role name.
96  	 * 
97  	 * @see org.kuali.rice.krad.lookup.AbstractLookupableHelperServiceImpl#getRows()
98  	 */
99  	@Override
100 	public List<Row> getRows() {
101 		title.remove(); 
102 		List<Row> rows = super.getRows();
103 		Iterator<Row> i = rows.iterator();
104 		String roleName = null;
105 		String namespaceCode = null;
106 		while ( i.hasNext() ) {
107 			Row row = i.next();
108 			int numFieldsRemoved = 0;
109 			boolean rowIsBlank = true;
110 			// Since multi-column lookups can be specified on Rice lookups, all the fields in each row must be iterated over as well,
111 			// just in case the person lookup ever gets configured to have multiple columns per row.
112 			for (Iterator<Field> fieldIter = row.getFields().iterator(); fieldIter.hasNext();) {
113 			    Field field = fieldIter.next();
114 			    String propertyName = field.getPropertyName();
115 			    if ( propertyName.equals("lookupRoleName") ) {
116 				    Object val = getParameters().get( propertyName );
117 				    String propVal = null;
118 				    if ( val != null ) {
119 					    propVal = (val instanceof String)?(String)val:((String[])val)[0];
120 				    }
121 				    if ( StringUtils.isBlank( propVal ) ) {
122 					    fieldIter.remove();
123 					    numFieldsRemoved++;
124 				    } else {
125 					    field.setReadOnly(true);
126 					    field.setDefaultValue( propVal );
127 					    roleName = propVal;
128 					    rowIsBlank = false;
129 				    }
130 			    } else if ( propertyName.equals("lookupRoleNamespaceCode") ) {
131 				    Object val = getParameters().get( propertyName );
132 				    String propVal = null;
133 				    if ( val != null ) {
134 					    propVal = (val instanceof String)?(String)val:((String[])val)[0];
135 				    }
136 				    if ( StringUtils.isBlank( propVal ) ) {
137 					    fieldIter.remove();
138 					    numFieldsRemoved++;
139 				    } else {
140 					    field.setReadOnly(true);
141 					    field.setDefaultValue( propVal );
142 					    namespaceCode = propVal;
143 					    rowIsBlank = false;
144 				    }				
145 			    } else if (!Field.BLANK_SPACE.equals(field.getFieldType())) {
146 			    	rowIsBlank = false;
147 			    }
148 			}
149 			// Check if any fields were removed from the row.
150 			if (numFieldsRemoved > 0) {
151 				// If fields were removed, check whether or not the remainder of the row is empty or only has blank space fields.
152 				if (rowIsBlank) {
153 					// If so, then remove the row entirely.
154 					i.remove();
155 				} else {
156 					// Otherwise, add one blank space for each field that was removed, using a technique similar to FieldUtils.createBlankSpace.
157 					// It is safe to just add blank spaces as needed, since the two removable fields are the last of the visible ones in the list.
158 					while (numFieldsRemoved > 0) {
159 						Field blankSpace = new Field();
160 						blankSpace.setFieldType(Field.BLANK_SPACE);
161 						blankSpace.setPropertyName(Field.BLANK_SPACE);
162 						row.getFields().add(blankSpace);
163 						numFieldsRemoved--;
164 					}
165 				}
166 			}
167 		}
168 		if ( roleName != null && namespaceCode != null ) {
169 			title.set( namespaceCode + " " + roleName + " Lookup" );
170 		}
171 		return rows;
172 	}
173 	
174 	private ThreadLocal<String> title = new ThreadLocal<String>();
175 	public String getTitle() {
176 		if ( title.get() == null ) {
177 			return super.getTitle();
178 		}
179 		return title.get();
180 	}
181 }