001    /*
002     * Copyright 2011 The Kuali Foundation.
003     * 
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     * http://www.opensource.org/licenses/ecl2.php
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.kfs.module.cam.businessobject.lookup;
017    
018    import java.util.ArrayList;
019    import java.util.Collections;
020    import java.util.List;
021    import java.util.Map;
022    import java.util.Properties;
023    
024    import org.apache.commons.lang.StringUtils;
025    import org.kuali.kfs.integration.cam.CapitalAssetManagementAsset;
026    import org.kuali.kfs.module.cam.CamsConstants;
027    import org.kuali.kfs.module.cam.CamsPropertyConstants;
028    import org.kuali.kfs.module.cam.businessobject.Asset;
029    import org.kuali.kfs.module.cam.businessobject.AssetGlobal;
030    import org.kuali.kfs.module.cam.businessobject.AssetRetirementGlobal;
031    import org.kuali.kfs.module.cam.document.service.AssetService;
032    import org.kuali.kfs.sys.KFSConstants;
033    import org.kuali.kfs.sys.KFSPropertyConstants;
034    import org.kuali.kfs.sys.context.SpringContext;
035    import org.kuali.kfs.sys.document.authorization.FinancialSystemMaintenanceDocumentAuthorizerBase;
036    import org.kuali.rice.kim.bo.Person;
037    import org.kuali.rice.kim.service.PersonService;
038    import org.kuali.rice.kns.bo.BusinessObject;
039    import org.kuali.rice.kns.lookup.HtmlData;
040    import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
041    import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
042    import org.kuali.rice.kns.service.DocumentHelperService;
043    import org.kuali.rice.kns.util.GlobalVariables;
044    import org.kuali.rice.kns.util.KNSConstants;
045    import org.kuali.rice.kns.util.UrlFactory;
046    import org.kuali.rice.kns.web.ui.Field;
047    import org.kuali.rice.kns.web.ui.Row;
048    
049    /**
050     * This class overrids the base getActionUrls method
051     */
052    public class AssetLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl {
053        private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AssetLookupableHelperServiceImpl.class);
054    
055        protected AssetService assetService;
056        private PersonService<Person> personService;
057    
058        /**
059         * Custom action urls for Asset.
060         * 
061         * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#getCustomActionUrls(org.kuali.rice.kns.bo.BusinessObject,
062         *      List pkNames)
063         */
064        @Override
065        public List<HtmlData> getCustomActionUrls(BusinessObject bo, List pkNames) {
066            List<HtmlData> anchorHtmlDataList = super.getCustomActionUrls(bo, pkNames);
067            Asset asset = (Asset) bo;
068    
069            // For retired asset, all action link will be hidden.
070            if (assetService.isAssetRetired(asset)) {
071                // clear 'edit' link
072                anchorHtmlDataList.clear();
073                // add 'view' link instead
074                anchorHtmlDataList.add(getViewAssetUrl(asset));
075            }
076            else {
077                anchorHtmlDataList.add(getLoanUrl(asset));
078                anchorHtmlDataList.add(getMergeUrl(asset));
079                anchorHtmlDataList.add(getSeparateUrl(asset));
080                anchorHtmlDataList.add(getTransferUrl(asset));
081            }
082    
083            return anchorHtmlDataList;
084        }
085    
086        protected HtmlData getViewAssetUrl(Asset asset) {
087            Properties parameters = new Properties();
088            parameters.put(KFSConstants.DISPATCH_REQUEST_PARAMETER, KFSConstants.START_METHOD);
089            parameters.put(CamsPropertyConstants.Asset.CAPITAL_ASSET_NUMBER, asset.getCapitalAssetNumber().toString());
090            parameters.put(KFSConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, CapitalAssetManagementAsset.class.getName());
091    
092            String href = UrlFactory.parameterizeUrl(CamsConstants.INQUIRY_URL, parameters);
093    
094            AnchorHtmlData anchorHtmlData = new AnchorHtmlData(href, KFSConstants.START_METHOD, CamsConstants.AssetActions.VIEW);
095            anchorHtmlData.setTarget("blank");
096            return anchorHtmlData;
097        }
098    
099        protected HtmlData getMergeUrl(Asset asset) {
100            FinancialSystemMaintenanceDocumentAuthorizerBase documentAuthorizer = (FinancialSystemMaintenanceDocumentAuthorizerBase) SpringContext.getBean(DocumentHelperService.class).getDocumentAuthorizer(CamsConstants.DocumentTypeName.ASSET_RETIREMENT_GLOBAL);
101            boolean isAuthorized = documentAuthorizer.isAuthorized(asset, CamsConstants.CAM_MODULE_CODE, CamsConstants.PermissionNames.MERGE, GlobalVariables.getUserSession().getPerson().getPrincipalId());
102    
103            if (isAuthorized) {
104                Properties parameters = new Properties();
105                parameters.put(KFSConstants.DISPATCH_REQUEST_PARAMETER, KFSConstants.MAINTENANCE_NEWWITHEXISTING_ACTION);
106                parameters.put(KFSConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, AssetRetirementGlobal.class.getName());
107                parameters.put(CamsPropertyConstants.AssetRetirementGlobal.MERGED_TARGET_CAPITAL_ASSET_NUMBER, asset.getCapitalAssetNumber().toString());
108                parameters.put(KFSConstants.OVERRIDE_KEYS, CamsPropertyConstants.AssetRetirementGlobal.RETIREMENT_REASON_CODE + KFSConstants.FIELD_CONVERSIONS_SEPERATOR + CamsPropertyConstants.AssetRetirementGlobal.MERGED_TARGET_CAPITAL_ASSET_NUMBER);
109                parameters.put(CamsPropertyConstants.AssetRetirementGlobal.RETIREMENT_REASON_CODE, CamsConstants.AssetRetirementReasonCode.MERGED);
110                parameters.put(KFSConstants.REFRESH_CALLER, CamsPropertyConstants.AssetRetirementGlobal.RETIREMENT_REASON_CODE + "::" + CamsConstants.AssetRetirementReasonCode.MERGED);
111    
112                String href = UrlFactory.parameterizeUrl(KFSConstants.MAINTENANCE_ACTION, parameters);
113    
114                return new AnchorHtmlData(href, CamsConstants.AssetActions.MERGE, CamsConstants.AssetActions.MERGE);
115            }
116            else {
117                return new AnchorHtmlData("", "", "");
118            }
119        }
120    
121        protected HtmlData getLoanUrl(Asset asset) {
122            AnchorHtmlData anchorHtmlData = null;
123            List<HtmlData> childURLDataList = new ArrayList<HtmlData>();
124    
125            Properties parameters = new Properties();
126            parameters.put(KFSConstants.DISPATCH_REQUEST_PARAMETER, KNSConstants.DOC_HANDLER_METHOD);
127            parameters.put(CamsPropertyConstants.AssetTransferDocument.CAPITAL_ASSET_NUMBER, asset.getCapitalAssetNumber().toString());
128            parameters.put(KFSConstants.PARAMETER_COMMAND, "initiate");
129            parameters.put(KFSConstants.DOCUMENT_TYPE_NAME, CamsConstants.DocumentTypeName.ASSET_EQUIPMENT_LOAN_OR_RETURN);
130    
131            if (getAssetService().isAssetLoaned(asset)) {
132                anchorHtmlData = new AnchorHtmlData("", "", "");
133    
134                AnchorHtmlData childURLData = new AnchorHtmlData("", "", CamsConstants.AssetActions.LOAN);
135                childURLDataList.add(childURLData);
136    
137                parameters.put(CamsConstants.AssetActions.LOAN_TYPE, CamsConstants.AssetActions.LOAN_RENEW);
138                String childHref = UrlFactory.parameterizeUrl(CamsConstants.StrutsActions.ONE_UP + CamsConstants.StrutsActions.EQUIPMENT_LOAN_OR_RETURN, parameters);
139                childURLData = new AnchorHtmlData(childHref, KNSConstants.DOC_HANDLER_METHOD, CamsConstants.AssetActions.LOAN_RENEW);
140                childURLDataList.add(childURLData);
141    
142                parameters.remove(CamsConstants.AssetActions.LOAN_TYPE);
143                parameters.put(CamsConstants.AssetActions.LOAN_TYPE, CamsConstants.AssetActions.LOAN_RETURN);
144                childHref = UrlFactory.parameterizeUrl(CamsConstants.StrutsActions.ONE_UP + CamsConstants.StrutsActions.EQUIPMENT_LOAN_OR_RETURN, parameters);
145                childURLData = new AnchorHtmlData(childHref, KNSConstants.DOC_HANDLER_METHOD, CamsConstants.AssetActions.LOAN_RETURN);
146                childURLDataList.add(childURLData);
147    
148                anchorHtmlData.setChildUrlDataList(childURLDataList);
149            }
150            else {
151                anchorHtmlData = new AnchorHtmlData("", "", "");
152                // 
153                AnchorHtmlData childURLData = new AnchorHtmlData("", "", "");
154                if (asset.getCampusTagNumber() == null) {
155                    childURLData = new AnchorHtmlData("", "", CamsConstants.AssetActions.LOAN);
156                    childURLDataList.add(childURLData);
157                }
158                else {
159                    parameters.put(CamsConstants.AssetActions.LOAN_TYPE, CamsConstants.AssetActions.LOAN);
160                    String childHref = UrlFactory.parameterizeUrl(CamsConstants.StrutsActions.ONE_UP + CamsConstants.StrutsActions.EQUIPMENT_LOAN_OR_RETURN, parameters);
161                    childURLData = new AnchorHtmlData(childHref, KNSConstants.DOC_HANDLER_METHOD, CamsConstants.AssetActions.LOAN);
162                    childURLDataList.add(childURLData);
163                }
164    
165                childURLData = new AnchorHtmlData("", "", CamsConstants.AssetActions.LOAN_RENEW);
166                childURLDataList.add(childURLData);
167    
168                childURLData = new AnchorHtmlData("", "", CamsConstants.AssetActions.LOAN_RETURN);
169                childURLDataList.add(childURLData);
170    
171                anchorHtmlData.setChildUrlDataList(childURLDataList);
172            }
173    
174            return anchorHtmlData;
175        }
176    
177        protected HtmlData getSeparateUrl(Asset asset) {
178            FinancialSystemMaintenanceDocumentAuthorizerBase documentAuthorizer = (FinancialSystemMaintenanceDocumentAuthorizerBase) SpringContext.getBean(DocumentHelperService.class).getDocumentAuthorizer(CamsConstants.DocumentTypeName.ASSET_ADD_GLOBAL);
179            boolean isAuthorized = documentAuthorizer.isAuthorized(asset, CamsConstants.CAM_MODULE_CODE, CamsConstants.PermissionNames.SEPARATE, GlobalVariables.getUserSession().getPerson().getPrincipalId());
180    
181            if (isAuthorized) {
182                String href = UrlFactory.parameterizeUrl(KFSConstants.MAINTENANCE_ACTION, getSeparateParameters(asset));
183    
184                return new AnchorHtmlData(href, KFSConstants.MAINTENANCE_NEW_METHOD_TO_CALL, CamsConstants.AssetActions.SEPARATE);
185            }
186            else {
187                return new AnchorHtmlData("", "", "");
188            }
189        }
190    
191        protected Properties getSeparateParameters(Asset asset) {
192            Properties parameters = new Properties();
193            parameters.put(KFSConstants.DISPATCH_REQUEST_PARAMETER, KFSConstants.MAINTENANCE_NEW_METHOD_TO_CALL);
194            parameters.put(KFSConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, AssetGlobal.class.getName());
195            parameters.put(CamsPropertyConstants.AssetGlobal.SEPARATE_SOURCE_CAPITAL_ASSET_NUMBER, asset.getCapitalAssetNumber().toString());
196            // parameter that tells us this is a separate action. We read this in AssetMaintenanbleImpl.processAfterNew
197            parameters.put(KFSPropertyConstants.FINANCIAL_DOCUMENT_TYPE_CODE, CamsConstants.PaymentDocumentTypeCodes.ASSET_GLOBAL_SEPARATE);
198    
199            return parameters;
200        }
201    
202        protected HtmlData getTransferUrl(Asset asset) {
203            Properties parameters = new Properties();
204            parameters.put(KFSConstants.DISPATCH_REQUEST_PARAMETER, KNSConstants.DOC_HANDLER_METHOD);
205            parameters.put(CamsPropertyConstants.AssetTransferDocument.CAPITAL_ASSET_NUMBER, asset.getCapitalAssetNumber().toString());
206            parameters.put(KFSConstants.PARAMETER_COMMAND, "initiate");
207            parameters.put(KFSConstants.DOCUMENT_TYPE_NAME, CamsConstants.DocumentTypeName.ASSET_TRANSFER);
208    
209            String href = UrlFactory.parameterizeUrl(CamsConstants.StrutsActions.ONE_UP + CamsConstants.StrutsActions.TRANSFER, parameters);
210    
211            return new AnchorHtmlData(href, KNSConstants.DOC_HANDLER_METHOD, CamsConstants.AssetActions.TRANSFER);
212        }
213    
214        public AssetService getAssetService() {
215            return assetService;
216        }
217    
218        public void setAssetService(AssetService assetService) {
219            this.assetService = assetService;
220        }
221    
222    
223        /**
224         * Overridden to fix a field conversion
225         * 
226         * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#getRows()
227         */
228        @Override
229        public List<Row> getRows() {
230            convertOrganizationOwnerAccountField();
231            return super.getRows();
232        }
233    
234        /**
235         * Overridden to fix a field conversion
236         * 
237         * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#setRows()
238         */
239        @Override
240        protected void setRows() {
241            super.setRows();
242            convertOrganizationOwnerAccountField();
243        }
244    
245        /**
246         * Goes through all the rows, making sure that problematic field conversions are fixed
247         */
248        protected void convertOrganizationOwnerAccountField() {
249            boolean foundField = false;
250            int i = 0;
251            while (!foundField && i < super.getRows().size()) {
252                final Row r = super.getRows().get(i);
253                int j = 0;
254                while (!foundField && j < r.getFields().size()) {
255                    Field f = r.getField(j);
256                    if (f.getPropertyName().equals(CamsPropertyConstants.Asset.ORGANIZATION_CODE)) {
257                        f.setFieldConversions(fixProblematicField(f.getFieldConversions()));
258                        f.setLookupParameters(fixProblematicField(f.getLookupParameters()));
259                        foundField = true;
260                    }
261                    j += 1;
262                }
263                i += 1;
264            }
265        }
266    
267        /**
268         * Fixes the field conversions - replaces "organizationOwnerAccount.chartOfAccountsCode" with
269         * "organizationOwnerChartOfAccountsCode"
270         * 
271         * @param fieldConversions the original field conversions
272         * @return the fixed field conversions
273         */
274        protected String fixProblematicField(String problemChildField) {
275            if (StringUtils.isBlank(problemChildField))
276                return problemChildField;
277            return problemChildField.replace(CamsPropertyConstants.Asset.ORGANIZATION_OWNER_ACCOUNTS_COA_CODE, CamsPropertyConstants.Asset.ORGANIZATION_OWNER_CHART_OF_ACCOUNTS_CODE);
278        }
279    
280        @Override
281        protected List<? extends BusinessObject> getSearchResultsHelper(Map<String, String> fieldValues, boolean unbounded) {
282            // perform the lookup on the asset representative first
283            String principalName = fieldValues.get(CamsPropertyConstants.Asset.REP_USER_AUTH_ID);
284            if (StringUtils.isNotBlank(principalName)) {
285                Person person = getPersonService().getPersonByPrincipalName(principalName);
286    
287                if (person == null) {
288                    return Collections.EMPTY_LIST;
289                }
290                // place the universal ID into the fieldValues map and remove the dummy attribute
291                fieldValues.put(CamsPropertyConstants.Asset.REPRESENTATIVE_UNIVERSAL_IDENTIFIER, person.getPrincipalId());
292                fieldValues.remove(CamsPropertyConstants.Asset.REP_USER_AUTH_ID);
293            }
294    
295            return super.getSearchResultsHelper(fieldValues, unbounded);
296        }
297        
298        /**
299         * @return Returns the personService.
300         */
301        protected PersonService<Person> getPersonService() {
302            if(personService==null)
303                personService = SpringContext.getBean(PersonService.class);
304            return personService;
305        }
306    
307    }