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.coa.businessobject.inquiry;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    
021    import org.kuali.kfs.coa.service.OrganizationReversionService;
022    import org.kuali.kfs.sys.businessobject.inquiry.KfsInquirableImpl;
023    import org.kuali.kfs.sys.context.SpringContext;
024    import org.kuali.rice.kns.bo.BusinessObject;
025    import org.kuali.rice.kns.web.ui.Field;
026    import org.kuali.rice.kns.web.ui.Row;
027    import org.kuali.rice.kns.web.ui.Section;
028    
029    public class OrganizationReversionInquirable extends KfsInquirableImpl {
030        private OrganizationReversionService organizationReversionService;
031    
032        /**
033         * Overridden to take out details with inactive categories
034         * @see org.kuali.rice.kns.inquiry.KualiInquirableImpl#getSections(org.kuali.rice.kns.bo.BusinessObject)
035         */
036        @Override
037        public List<Section> getSections(BusinessObject bo) {
038            List<Section> sections = super.getSections(bo);
039            if (organizationReversionService == null) {
040                organizationReversionService = SpringContext.getBean(OrganizationReversionService.class);
041            }
042            for (Section section : sections) {
043                for (Row row : section.getRows()) {
044                    List<Field> updatedFields = new ArrayList<Field>();
045                    for (Field field : row.getFields()) {
046                        if (shouldIncludeField(field)) {
047                            updatedFields.add(field);
048                        }
049                    }
050                    row.setFields(updatedFields);
051                }
052            }
053            return sections;
054        }
055    
056        /**
057         * Determines if the given field should be included in the updated row, once we take out inactive categories
058         * @param field the field to check
059         * @return true if the field should be included (ie, it doesn't describe an organization reversion with an inactive category); false otherwise
060         */
061        protected boolean shouldIncludeField(Field field) {
062            boolean includeField = true;
063            if (field.getContainerRows() != null) {
064                for (Row containerRow : field.getContainerRows()) {
065                    for (Field containedField : containerRow.getFields()) {
066                        if (containedField.getPropertyName().matches("organizationReversionDetail\\[\\d+\\]\\.organizationReversionCategoryCode")) {
067                            final String categoryValue = containedField.getPropertyValue();
068                            includeField = organizationReversionService.isCategoryActive(categoryValue);
069                        }
070                    }
071                }
072            }
073            return includeField;
074        }
075    }