001    /**
002     * Copyright 2004-2012 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.hr.time.roles.validation;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.hr.time.roles.TkRole;
020    import org.kuali.hr.time.roles.TkRoleGroup;
021    import org.kuali.hr.time.util.TkConstants;
022    import org.kuali.rice.kns.document.MaintenanceDocument;
023    import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
024    import org.kuali.rice.krad.bo.PersistableBusinessObject;
025    
026    public class TkRoleValidation extends MaintenanceDocumentRuleBase{
027    
028        private static final String ADD_LINE_LOCATION = "add.roles.";
029    
030        protected boolean validateTkRole(TkRole role, String fieldPrefix) {
031            boolean valid = true;
032    
033            if (fieldPrefix == null)
034                fieldPrefix = "";
035    
036           String rname = role.getRoleName();
037           if (StringUtils.equalsIgnoreCase(rname, TkConstants.ROLE_TK_LOCATION_ADMIN) || StringUtils.equalsIgnoreCase(rname, TkConstants.ROLE_TK_LOCATION_VO)) {
038               if (StringUtils.isBlank(role.getChart())) {
039                       this.putFieldError(fieldPrefix + "chart", "error.role.chart.required");
040                       valid = false;
041               }
042            } else {
043                    if (!StringUtils.isBlank(role.getChart())) {
044                       this.putFieldError(fieldPrefix + "chart", "error.role.chart.disallowed");
045                       valid = false;
046               }
047            }
048           
049            return valid;
050        }
051    
052        /**
053         * Currently, the prefix will not work when the attributes of the collection
054         * items are set to readOnlyAfterAdd. This seems to be a built in function
055         * of rice. Errors will still appear but no marker will be placed next to
056         * the field.
057         */
058        boolean validateTkRoleGroup(TkRoleGroup roleGroup) {
059            boolean valid = true;
060    
061            int pos = 0;
062            for (TkRole role: roleGroup.getRoles()) {
063                StringBuffer prefix = new StringBuffer("roles[");
064                prefix.append(pos).append("].");
065                validateTkRole(role, prefix.toString());
066                pos++;
067            }
068    
069            return valid;
070        }
071    
072            @Override
073            protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
074                    boolean valid = true;
075    
076                    PersistableBusinessObject pbo = (PersistableBusinessObject) this.getNewBo();
077                    if (pbo instanceof TkRole) {
078                            TkRole role = (TkRole)pbo;
079                valid = validateTkRole(role, null);
080                    } else if (pbo instanceof TkRoleGroup) {
081                TkRoleGroup roleGroup = (TkRoleGroup)pbo;
082                valid = validateTkRoleGroup(roleGroup);
083            }
084    
085                    return valid;
086            }
087    
088        @Override
089        /**
090         * Validation for the 'add' role in the collection manipulator.
091         */
092        public boolean processCustomAddCollectionLineBusinessRules(MaintenanceDocument document, String collectionName, PersistableBusinessObject line) {
093            boolean valid = true;
094    
095            if (line instanceof TkRole) {
096                TkRole role = (TkRole)line;
097                valid = validateTkRole(role, ADD_LINE_LOCATION);
098            }
099    
100            return valid;
101        }
102    }