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.document.authorization;
017    
018    import java.util.Arrays;
019    import java.util.List;
020    import java.util.Set;
021    
022    import org.kuali.kfs.module.cam.CamsConstants;
023    import org.kuali.kfs.module.cam.CamsPropertyConstants;
024    import org.kuali.kfs.module.cam.businessobject.Asset;
025    import org.kuali.kfs.module.cam.document.service.AssetService;
026    import org.kuali.kfs.sys.context.SpringContext;
027    import org.kuali.kfs.sys.document.authorization.FinancialSystemMaintenanceDocumentPresentationControllerBase;
028    import org.kuali.rice.kns.bo.BusinessObject;
029    import org.kuali.rice.kns.document.Document;
030    import org.kuali.rice.kns.document.MaintenanceDocument;
031    import org.kuali.rice.kns.service.DocumentHelperService;
032    import org.kuali.rice.kns.service.ParameterService;
033    import org.kuali.rice.kns.util.GlobalVariables;
034    import org.kuali.rice.kns.workflow.service.KualiWorkflowDocument;
035    
036    /**
037     * Presentation Controller for Asset Maintenance Documents
038     */
039    public class AssetPresentationController extends FinancialSystemMaintenanceDocumentPresentationControllerBase {
040    
041        @Override
042        public Set<String> getConditionallyHiddenPropertyNames(BusinessObject businessObject) {
043            Set<String> fields = super.getConditionallyHiddenPropertyNames(businessObject);
044    
045            MaintenanceDocument document = (MaintenanceDocument) businessObject;
046    
047            if (SpringContext.getBean(AssetService.class).isAssetFabrication(document)) {
048                fields.addAll(Arrays.asList(CamsConstants.Asset.EDIT_DETAIL_INFORMATION_FIELDS));
049                fields.addAll(Arrays.asList(CamsConstants.Asset.EDIT_ORGANIZATION_INFORMATION_FIELDS));
050            }
051    
052            // Hide payment sequence numbers
053            Asset asset = (Asset) document.getNewMaintainableObject().getBusinessObject();
054            int size = asset.getAssetPayments().size();
055            for (int i = 0; i < size; i++) {
056                fields.add(CamsPropertyConstants.Asset.ASSET_PAYMENTS + "[" + i + "]." + CamsPropertyConstants.AssetPayment.PAYMENT_SEQ_NUMBER);
057                fields.add(CamsPropertyConstants.Asset.ASSET_PAYMENTS + "[" + i + "]." + CamsPropertyConstants.AssetPayment.CAPITAL_ASSET_NUMBER);
058            }
059    
060            if (!CamsConstants.AssetRetirementReasonCode.MERGED.equals(asset.getRetirementReasonCode())) {
061                // hide merge target capital asset number
062                fields.add(CamsPropertyConstants.Asset.RETIREMENT_INFO_MERGED_TARGET);
063            }
064    
065            return fields;
066        }
067    
068        @Override
069        public Set<String> getConditionallyReadOnlyPropertyNames(MaintenanceDocument document) {
070            Set<String> fields = super.getConditionallyReadOnlyPropertyNames(document);
071    
072            if (SpringContext.getBean(AssetService.class).isAssetFabrication(document)) {
073                fields.add(CamsPropertyConstants.Asset.ASSET_INVENTORY_STATUS);
074                fields.add(CamsPropertyConstants.Asset.VENDOR_NAME);
075                fields.add(CamsPropertyConstants.Asset.ACQUISITION_TYPE_CODE);
076            }
077            else {
078                // acquisition type code is read-only during edit
079                fields.add(CamsPropertyConstants.Asset.ACQUISITION_TYPE_CODE);
080                // fabrication fields are read-only
081                fields.addAll(Arrays.asList(CamsConstants.Asset.FABRICATION_INFORMATION_FIELDS));
082            }
083    
084            Asset asset = (Asset) document.getNewMaintainableObject().getBusinessObject();
085            // if tag was created in a prior fiscal year, set tag number, asset type code and description as view only
086            if (SpringContext.getBean(AssetService.class).isAssetTaggedInPriorFiscalYear(asset)) {
087                AssetAuthorizer documentAuthorizer = (AssetAuthorizer) SpringContext.getBean(DocumentHelperService.class).getDocumentAuthorizer(document);
088                boolean isAuthorized = documentAuthorizer.isAuthorized(document, CamsConstants.CAM_MODULE_CODE, CamsConstants.PermissionNames.EDIT_WHEN_TAGGED_PRIOR_FISCAL_YEAR, GlobalVariables.getUserSession().getPerson().getPrincipalId());
089                if (!isAuthorized) {
090                    fields.addAll(SpringContext.getBean(ParameterService.class).getParameterValues(Asset.class, CamsConstants.Parameters.EDITABLE_FIELDS_WHEN_TAGGED_PRIOR_FISCAL_YEAR));
091                }                
092            }
093            
094            return fields;
095        }
096    
097        @Override
098        public Set<String> getConditionallyHiddenSectionIds(BusinessObject businessObject) {
099            Set<String> fields = super.getConditionallyHiddenSectionIds(businessObject);
100    
101            MaintenanceDocument document = (MaintenanceDocument) businessObject;
102            Asset asset = (Asset) document.getNewMaintainableObject().getBusinessObject();
103    
104            if (asset.getAssetPayments().size() > CamsConstants.Asset.ASSET_MAXIMUM_NUMBER_OF_PAYMENT_DISPLAY) {
105                // Hide the payment section if there are more then CamsConstants.ASSET_MAXIMUM_NUMBER_OF_PAYMENT_DISPLAY
106                fields.add(CamsConstants.Asset.SECTION_ID_PAYMENT_INFORMATION);
107            }
108    
109            if (SpringContext.getBean(AssetService.class).isAssetFabrication(document)) {
110                // fabrication request asset creation. Hide sections that are only applicable to asset edit. For fields
111                // that are to be hidden for asset edit, see AssetAuthorizer.addMaintenanceDocumentRestrictions
112                fields.add(CamsConstants.Asset.SECTION_ID_LAND_INFORMATION);
113                fields.add(CamsConstants.Asset.SECTION_ID_PAYMENT_INFORMATION);
114                fields.add(CamsConstants.Asset.SECTION_ID_PAYMENT_LOOKUP);
115                fields.add(CamsConstants.Asset.SECTION_ID_DOCUMENT_LOOKUP);
116                fields.add(CamsConstants.Asset.SECTION_ID_DEPRECIATION_INFORMATION);
117                fields.add(CamsConstants.Asset.SECTION_ID_HISTORY);
118                fields.add(CamsConstants.Asset.SECTION_ID_RETIREMENT_INFORMATION);
119                fields.add(CamsConstants.Asset.SECTION_ID_EQUIPMENT_LOAN_INFORMATION);
120                fields.add(CamsConstants.Asset.SECTION_ID_WARRENTY);
121                fields.add(CamsConstants.Asset.SECTION_ID_REPAIR_HISTORY);
122                fields.add(CamsConstants.Asset.SECTION_ID_COMPONENTS);
123                fields.add(CamsConstants.Asset.SECTION_ID_MERGE_HISTORY);
124            }
125            else {
126                // Asset edit
127                if (asset.getEstimatedFabricationCompletionDate() == null) {
128                    // hide fabrication only if asset is not fabricated through CAMS
129                    fields.add(CamsConstants.Asset.SECTION_ID_FABRICATION_INFORMATION);
130                }
131    
132                if (!SpringContext.getBean(AssetService.class).isAssetRetired(asset)) {
133                    // if asset is not retired, hide retirement information
134                    fields.add(CamsConstants.Asset.SECTION_ID_RETIREMENT_INFORMATION);
135                }
136    
137                if (asset.getExpectedReturnDate() == null || asset.getLoanReturnDate() != null) {
138                    // If asset is not loaned, hide the section
139                    fields.add(CamsConstants.Asset.SECTION_ID_LOAN_INFORMATION);
140                }
141                if (asset.getSeparateHistory() == null) {
142                    // If asset is not separated, hide the section
143                    fields.add(CamsConstants.Asset.SECTION_ID_HISTORY);
144                }
145            }
146    
147            return fields;
148        }
149    
150    
151        @Override
152        protected boolean canEdit(Document document) {
153            AssetService assetService = SpringContext.getBean(AssetService.class);
154    
155            // for fabrication document, disallow edit when document routing to the 'Management' FYI users.
156            if (assetService.isAssetFabrication((MaintenanceDocument) document)) {
157                KualiWorkflowDocument workflowDocument = (KualiWorkflowDocument) document.getDocumentHeader().getWorkflowDocument();
158                if (workflowDocument.stateIsEnroute()) {
159                    List<String> nodeNames = assetService.getCurrentRouteLevels(workflowDocument);
160    
161                    if (nodeNames.contains(CamsConstants.RouteLevelNames.MANAGEMENT) && !workflowDocument.isApprovalRequested()) {
162                        return false;
163                    }
164                }
165            }
166            // for retired asset, disable edit.
167            MaintenanceDocument maitDocument = (MaintenanceDocument) document;
168            Asset asset = (Asset) maitDocument.getOldMaintainableObject().getBusinessObject();
169            return !getAssetService().isAssetRetired(asset) & super.canEdit(document);
170        }
171        
172        
173        @Override
174        protected boolean canBlanketApprove(Document document) {
175            return true;
176        }
177        
178        @Override
179        protected boolean canRoute(Document document) {
180            MaintenanceDocument maitDocument = (MaintenanceDocument) document;
181            Asset asset = (Asset) maitDocument.getOldMaintainableObject().getBusinessObject();
182            return !getAssetService().isAssetRetired(asset) & super.canRoute(document);
183        }
184        
185        @Override
186        protected boolean canSave(Document document) {
187            MaintenanceDocument maitDocument = (MaintenanceDocument) document;
188            Asset asset = (Asset) maitDocument.getOldMaintainableObject().getBusinessObject();
189            return !getAssetService().isAssetRetired(asset) & super.canSave(document);
190        }
191        
192        protected AssetService getAssetService() {
193            return SpringContext.getBean(AssetService.class);
194        }
195    }