View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.module.cam.businessobject.lookup;
20  
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Properties;
26  
27  import org.apache.commons.lang.StringUtils;
28  import org.kuali.kfs.integration.cam.CapitalAssetManagementAsset;
29  import org.kuali.kfs.module.cam.CamsConstants;
30  import org.kuali.kfs.module.cam.CamsPropertyConstants;
31  import org.kuali.kfs.module.cam.businessobject.Asset;
32  import org.kuali.kfs.module.cam.businessobject.AssetGlobal;
33  import org.kuali.kfs.module.cam.businessobject.AssetRetirementGlobal;
34  import org.kuali.kfs.module.cam.document.service.AssetService;
35  import org.kuali.kfs.sys.KFSConstants;
36  import org.kuali.kfs.sys.KFSPropertyConstants;
37  import org.kuali.kfs.sys.context.SpringContext;
38  import org.kuali.kfs.sys.document.authorization.FinancialSystemMaintenanceDocumentAuthorizerBase;
39  import org.kuali.kfs.sys.document.authorization.FinancialSystemTransactionalDocumentAuthorizerBase;
40  import org.kuali.rice.kim.api.identity.principal.Principal;
41  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
42  import org.kuali.rice.kns.lookup.HtmlData;
43  import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
44  import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
45  import org.kuali.rice.kns.web.struts.form.LookupForm;
46  import org.kuali.rice.kns.web.ui.Field;
47  import org.kuali.rice.kns.web.ui.Row;
48  import org.kuali.rice.krad.bo.BusinessObject;
49  import org.kuali.rice.krad.exception.ValidationException;
50  import org.kuali.rice.krad.service.DocumentDictionaryService;
51  import org.kuali.rice.krad.util.GlobalVariables;
52  import org.kuali.rice.krad.util.KRADConstants;
53  import org.kuali.rice.krad.util.UrlFactory;
54  
55  /**
56   * This class overrids the base getActionUrls method
57   */
58  public class AssetLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl {
59      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AssetLookupableHelperServiceImpl.class);
60  
61      protected AssetService assetService;
62  
63      /**
64       * Custom action urls for Asset.
65       *
66       * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#getCustomActionUrls(org.kuali.rice.krad.bo.BusinessObject,
67       *      List pkNames)
68       */
69      @Override
70      public List<HtmlData> getCustomActionUrls(BusinessObject bo, List pkNames) {
71          List<HtmlData> anchorHtmlDataList = super.getCustomActionUrls(bo, pkNames);
72          Asset asset = (Asset) bo;
73  
74          // For retired asset, all action link will be hidden.
75          if (assetService.isAssetRetired(asset)) {
76              // clear 'edit' link
77              anchorHtmlDataList.clear();
78              // add 'view' link instead
79              anchorHtmlDataList.add(getViewAssetUrl(asset));
80          }
81          else {
82              anchorHtmlDataList.add(getLoanUrl(asset));
83              anchorHtmlDataList.add(getMergeUrl(asset));
84              anchorHtmlDataList.add(getSeparateUrl(asset));
85              anchorHtmlDataList.add(getTransferUrl(asset));
86          }
87  
88          return anchorHtmlDataList;
89      }
90  
91      protected HtmlData getViewAssetUrl(Asset asset) {
92          Properties parameters = new Properties();
93          parameters.put(KFSConstants.DISPATCH_REQUEST_PARAMETER, KFSConstants.START_METHOD);
94          parameters.put(CamsPropertyConstants.Asset.CAPITAL_ASSET_NUMBER, asset.getCapitalAssetNumber().toString());
95          parameters.put(KFSConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, CapitalAssetManagementAsset.class.getName());
96  
97          String href = UrlFactory.parameterizeUrl(CamsConstants.INQUIRY_URL, parameters);
98  
99          AnchorHtmlData anchorHtmlData = new AnchorHtmlData(href, KFSConstants.START_METHOD, CamsConstants.AssetActions.VIEW);
100         anchorHtmlData.setTarget("blank");
101         return anchorHtmlData;
102     }
103 
104     protected HtmlData getMergeUrl(Asset asset) {
105         FinancialSystemMaintenanceDocumentAuthorizerBase documentAuthorizer = (FinancialSystemMaintenanceDocumentAuthorizerBase) SpringContext.getBean(DocumentDictionaryService.class).getDocumentAuthorizer(CamsConstants.DocumentTypeName.ASSET_RETIREMENT_GLOBAL);
106         boolean isAuthorized = documentAuthorizer.isAuthorized(asset, CamsConstants.CAM_MODULE_CODE, CamsConstants.PermissionNames.MERGE, GlobalVariables.getUserSession().getPerson().getPrincipalId());
107 
108         if (isAuthorized) {
109             Properties parameters = new Properties();
110             parameters.put(KFSConstants.DISPATCH_REQUEST_PARAMETER, KFSConstants.MAINTENANCE_NEWWITHEXISTING_ACTION);
111             parameters.put(KFSConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, AssetRetirementGlobal.class.getName());
112             parameters.put(CamsPropertyConstants.AssetRetirementGlobal.MERGED_TARGET_CAPITAL_ASSET_NUMBER, asset.getCapitalAssetNumber().toString());
113             parameters.put(KFSConstants.OVERRIDE_KEYS, CamsPropertyConstants.AssetRetirementGlobal.RETIREMENT_REASON_CODE + KFSConstants.FIELD_CONVERSIONS_SEPERATOR + CamsPropertyConstants.AssetRetirementGlobal.MERGED_TARGET_CAPITAL_ASSET_NUMBER);
114             parameters.put(CamsPropertyConstants.AssetRetirementGlobal.RETIREMENT_REASON_CODE, CamsConstants.AssetRetirementReasonCode.MERGED);
115             parameters.put(KFSConstants.REFRESH_CALLER, CamsPropertyConstants.AssetRetirementGlobal.RETIREMENT_REASON_CODE + "::" + CamsConstants.AssetRetirementReasonCode.MERGED);
116 
117 
118             String href = UrlFactory.parameterizeUrl(KFSConstants.MAINTENANCE_ACTION, parameters);
119 
120             return new AnchorHtmlData(href, CamsConstants.AssetActions.MERGE, CamsConstants.AssetActions.MERGE);
121         }
122         else {
123             return new AnchorHtmlData("", "", "");
124         }
125     }
126 
127     @Override
128     protected String getReturnHref(Properties parameters, LookupForm lookupForm, List returnKeys) {
129         String href = super.getReturnHref(parameters, lookupForm, returnKeys);
130         href += "&referencesToRefresh=mergedTargetCapitalAsset";
131         return href;
132 
133     }
134 
135 
136     protected HtmlData getLoanUrl(Asset asset) {
137         AnchorHtmlData anchorHtmlData = null;
138         List<HtmlData> childURLDataList = new ArrayList<HtmlData>();
139 
140         Properties parameters = new Properties();
141         parameters.put(KFSConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.DOC_HANDLER_METHOD);
142         parameters.put(CamsPropertyConstants.AssetTransferDocument.CAPITAL_ASSET_NUMBER, asset.getCapitalAssetNumber().toString());
143         parameters.put(KFSConstants.PARAMETER_COMMAND, "initiate");
144         parameters.put(KFSConstants.DOCUMENT_TYPE_NAME, CamsConstants.DocumentTypeName.ASSET_EQUIPMENT_LOAN_OR_RETURN);
145 
146         if (getAssetService().isAssetLoaned(asset)) {
147             anchorHtmlData = new AnchorHtmlData("", "", "");
148 
149             AnchorHtmlData childURLData = new AnchorHtmlData("", "", CamsConstants.AssetActions.LOAN);
150             childURLDataList.add(childURLData);
151 
152             parameters.put(CamsConstants.AssetActions.LOAN_TYPE, CamsConstants.AssetActions.LOAN_RENEW);
153             String childHref = UrlFactory.parameterizeUrl(CamsConstants.StrutsActions.ONE_UP + CamsConstants.StrutsActions.EQUIPMENT_LOAN_OR_RETURN, parameters);
154             childURLData = new AnchorHtmlData(childHref, KRADConstants.DOC_HANDLER_METHOD, CamsConstants.AssetActions.LOAN_RENEW);
155             childURLDataList.add(childURLData);
156 
157             parameters.remove(CamsConstants.AssetActions.LOAN_TYPE);
158             parameters.put(CamsConstants.AssetActions.LOAN_TYPE, CamsConstants.AssetActions.LOAN_RETURN);
159             childHref = UrlFactory.parameterizeUrl(CamsConstants.StrutsActions.ONE_UP + CamsConstants.StrutsActions.EQUIPMENT_LOAN_OR_RETURN, parameters);
160             childURLData = new AnchorHtmlData(childHref, KRADConstants.DOC_HANDLER_METHOD, CamsConstants.AssetActions.LOAN_RETURN);
161             childURLDataList.add(childURLData);
162 
163             anchorHtmlData.setChildUrlDataList(childURLDataList);
164         }
165         else {
166             anchorHtmlData = new AnchorHtmlData("", "", "");
167             //
168             AnchorHtmlData childURLData = new AnchorHtmlData("", "", "");
169             if (asset.getCampusTagNumber() == null) {
170                 childURLData = new AnchorHtmlData("", "", CamsConstants.AssetActions.LOAN);
171                 childURLDataList.add(childURLData);
172             }
173             else {
174                 parameters.put(CamsConstants.AssetActions.LOAN_TYPE, CamsConstants.AssetActions.LOAN);
175                 String childHref = UrlFactory.parameterizeUrl(CamsConstants.StrutsActions.ONE_UP + CamsConstants.StrutsActions.EQUIPMENT_LOAN_OR_RETURN, parameters);
176                 childURLData = new AnchorHtmlData(childHref, KRADConstants.DOC_HANDLER_METHOD, CamsConstants.AssetActions.LOAN);
177                 childURLDataList.add(childURLData);
178             }
179 
180             childURLData = new AnchorHtmlData("", "", CamsConstants.AssetActions.LOAN_RENEW);
181             childURLDataList.add(childURLData);
182 
183             childURLData = new AnchorHtmlData("", "", CamsConstants.AssetActions.LOAN_RETURN);
184             childURLDataList.add(childURLData);
185 
186             anchorHtmlData.setChildUrlDataList(childURLDataList);
187         }
188 
189         return anchorHtmlData;
190     }
191 
192     protected HtmlData getSeparateUrl(Asset asset) {
193         FinancialSystemMaintenanceDocumentAuthorizerBase documentAuthorizer = (FinancialSystemMaintenanceDocumentAuthorizerBase) SpringContext.getBean(DocumentDictionaryService.class).getDocumentAuthorizer(CamsConstants.DocumentTypeName.ASSET_ADD_GLOBAL);
194         boolean isAuthorized = documentAuthorizer.isAuthorized(asset, CamsConstants.CAM_MODULE_CODE, CamsConstants.PermissionNames.SEPARATE, GlobalVariables.getUserSession().getPerson().getPrincipalId());
195 
196         if (isAuthorized) {
197             String href = UrlFactory.parameterizeUrl(KFSConstants.MAINTENANCE_ACTION, getSeparateParameters(asset));
198 
199             return new AnchorHtmlData(href, KFSConstants.MAINTENANCE_NEW_METHOD_TO_CALL, CamsConstants.AssetActions.SEPARATE);
200         }
201         else {
202             return new AnchorHtmlData("", "", "");
203         }
204     }
205 
206     protected Properties getSeparateParameters(Asset asset) {
207         Properties parameters = new Properties();
208         parameters.put(KFSConstants.DISPATCH_REQUEST_PARAMETER, KFSConstants.MAINTENANCE_NEW_METHOD_TO_CALL);
209         parameters.put(KFSConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, AssetGlobal.class.getName());
210         parameters.put(CamsPropertyConstants.AssetGlobal.SEPARATE_SOURCE_CAPITAL_ASSET_NUMBER, asset.getCapitalAssetNumber().toString());
211         // parameter that tells us this is a separate action. We read this in AssetMaintenanbleImpl.processAfterNew
212         parameters.put(KFSPropertyConstants.FINANCIAL_DOCUMENT_TYPE_CODE, CamsConstants.PaymentDocumentTypeCodes.ASSET_GLOBAL_SEPARATE);
213 
214         return parameters;
215     }
216 
217     protected HtmlData getTransferUrl(Asset asset) {
218         boolean isAuthorized = true;
219         boolean assetMovable = false;
220         try {
221            assetMovable = getAssetService().isAssetMovableCheckByPayment(asset);
222         } catch (ValidationException ve) {
223             isAuthorized = false;
224         }
225         if (!assetMovable) {
226             FinancialSystemTransactionalDocumentAuthorizerBase documentAuthorizer = (FinancialSystemTransactionalDocumentAuthorizerBase)SpringContext.getBean(DocumentDictionaryService.class).getDocumentAuthorizer(CamsConstants.DocumentTypeName.ASSET_TRANSFER);
227             isAuthorized = documentAuthorizer.isAuthorized(asset, CamsConstants.CAM_MODULE_CODE, CamsConstants.PermissionNames.SEPARATE, GlobalVariables.getUserSession().getPerson().getPrincipalId());
228         }
229 
230         if (isAuthorized) {
231             Properties parameters = new Properties();
232             parameters.put(KFSConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.DOC_HANDLER_METHOD);
233             parameters.put(CamsPropertyConstants.AssetTransferDocument.CAPITAL_ASSET_NUMBER, asset.getCapitalAssetNumber().toString());
234             parameters.put(KFSConstants.PARAMETER_COMMAND, "initiate");
235             parameters.put(KFSConstants.DOCUMENT_TYPE_NAME, CamsConstants.DocumentTypeName.ASSET_TRANSFER);
236 
237             String href = UrlFactory.parameterizeUrl(CamsConstants.StrutsActions.ONE_UP + CamsConstants.StrutsActions.TRANSFER, parameters);
238 
239             return new AnchorHtmlData(href, KRADConstants.DOC_HANDLER_METHOD, CamsConstants.AssetActions.TRANSFER);
240         }
241         else {
242             return new AnchorHtmlData("", "", "");
243         }
244     }
245 
246     public AssetService getAssetService() {
247         return assetService;
248     }
249 
250     public void setAssetService(AssetService assetService) {
251         this.assetService = assetService;
252     }
253 
254 
255     /**
256      * Overridden to fix a field conversion
257      *
258      * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#getRows()
259      *
260      * KRAD Conversion: Performs customization of the  Row objects to be used to generate the search query screen.
261      */
262     @Override
263     public List<Row> getRows() {
264         convertOrganizationOwnerAccountField();
265         return super.getRows();
266     }
267 
268     /**
269      * Overridden to fix a field conversion
270      *
271      * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#setRows()
272      */
273     @Override
274     protected void setRows() {
275         super.setRows();
276         convertOrganizationOwnerAccountField();
277     }
278 
279     /**
280      * Goes through all the rows, making sure that problematic field conversions are fixed
281      *
282      * KRAD Conversion: Performs customization of the fields.
283      */
284     protected void convertOrganizationOwnerAccountField() {
285         boolean foundField = false;
286         int i = 0;
287         while (!foundField && i < super.getRows().size()) {
288             final Row r = super.getRows().get(i);
289             int j = 0;
290             while (!foundField && j < r.getFields().size()) {
291                 Field f = r.getField(j);
292                 if (f.getPropertyName().equals(CamsPropertyConstants.Asset.ORGANIZATION_CODE)) {
293                     f.setFieldConversions(fixProblematicField(f.getFieldConversions()));
294                     f.setLookupParameters(fixProblematicField(f.getLookupParameters()));
295                     foundField = true;
296                 }
297                 j += 1;
298             }
299             i += 1;
300         }
301     }
302 
303     /**
304      * Fixes the field conversions - replaces "organizationOwnerAccount.chartOfAccountsCode" with
305      * "organizationOwnerChartOfAccountsCode"
306      *
307      * @param fieldConversions the original field conversions
308      * @return the fixed field conversions
309      */
310     protected String fixProblematicField(String problemChildField) {
311         if (StringUtils.isBlank(problemChildField)) {
312             return problemChildField;
313         }
314         return problemChildField.replace(CamsPropertyConstants.Asset.ORGANIZATION_OWNER_ACCOUNTS_COA_CODE, CamsPropertyConstants.Asset.ORGANIZATION_OWNER_CHART_OF_ACCOUNTS_CODE);
315     }
316 
317     @Override
318     protected List<? extends BusinessObject> getSearchResultsHelper(Map<String, String> fieldValues, boolean unbounded) {
319         // perform the lookup on the asset representative first
320         String principalName = fieldValues.get(CamsPropertyConstants.Asset.REP_USER_AUTH_ID);
321         if (StringUtils.isNotBlank(principalName)) {
322             Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(principalName);
323 
324             if (principal == null) {
325                 return Collections.EMPTY_LIST;
326             }
327             // place the universal ID into the fieldValues map and remove the dummy attribute
328             fieldValues.put(CamsPropertyConstants.Asset.REPRESENTATIVE_UNIVERSAL_IDENTIFIER, principal.getPrincipalId());
329             fieldValues.remove(CamsPropertyConstants.Asset.REP_USER_AUTH_ID);
330         }
331 
332         return super.getSearchResultsHelper(fieldValues, unbounded);
333     }
334 }