001/**
002 * Copyright 2004-2014 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 */
016package org.kuali.kpme.tklm.time.rules.clocklocation.web;
017
018import java.util.List;
019import java.util.Map;
020
021import org.apache.commons.lang.StringUtils;
022import org.kuali.kpme.core.bo.HrBusinessObject;
023import org.kuali.kpme.core.bo.HrBusinessObjectMaintainableImpl;
024import org.kuali.kpme.core.cache.CacheUtils;
025import org.kuali.kpme.tklm.time.rules.clocklocation.ClockLocationRule;
026import org.kuali.kpme.tklm.time.rules.clocklocation.ClockLocationRuleIpAddress;
027import org.kuali.kpme.tklm.time.rules.clocklocation.validation.ClockLocationRuleRule;
028import org.kuali.kpme.tklm.time.service.TkServiceLocator;
029import org.kuali.rice.kns.document.MaintenanceDocument;
030import org.kuali.rice.krad.service.KRADServiceLocator;
031import org.kuali.rice.krad.util.GlobalVariables;
032import org.kuali.rice.krad.util.KRADConstants;
033
034/**
035 * 
036 * Used to modify behavior of Clock Location Maintenance page
037 * 
038 */
039public class ClockLocationMaintainableImpl extends HrBusinessObjectMaintainableImpl {
040
041        /**
042     * 
043     */
044        private static final long serialVersionUID = 1L;
045
046        /**
047         * Used to swap out wildcard values for numeric types
048         */
049        @Override
050        public Map populateBusinessObject(Map<String, String> fieldValues,
051                        MaintenanceDocument maintenanceDocument, String methodToCall) {
052                if (fieldValues.containsKey("workArea")
053                                && StringUtils.equals(fieldValues.get("workArea"), "%")) {
054                        fieldValues.put("workArea", "-1");
055                }
056                if (fieldValues.containsKey("jobNumber")
057                                && StringUtils.equals(fieldValues.get("jobNumber"), "%")) {
058                        fieldValues.put("jobNumber", "-1");
059                }
060                return super.populateBusinessObject(fieldValues, maintenanceDocument,
061                                methodToCall);
062        }
063
064        @Override
065        public void processAfterEdit(MaintenanceDocument document,
066                        Map<String, String[]> parameters) {
067                ClockLocationRule clockLocationRule = (ClockLocationRule) this
068                                .getBusinessObject();
069                List<ClockLocationRule> clockLocationRuleList = TkServiceLocator
070                                .getClockLocationRuleService()
071                                .getNewerVersionClockLocationRule(clockLocationRule.getDept(),
072                                                clockLocationRule.getWorkArea(),
073                                                clockLocationRule.getPrincipalId(),
074                                                clockLocationRule.getJobNumber(),
075                                                clockLocationRule.getEffectiveLocalDate());
076                if (clockLocationRuleList.size() > 0) {
077                        GlobalVariables.getMessageMap().putWarningForSectionId(
078                                        "Clock Location Rule Maintenance",
079                                        "clocklocationrule.newer.exists", null);
080                }
081                super.processAfterEdit(document, parameters);
082        }
083
084        @Override
085        public HrBusinessObject getObjectById(String id) {
086                return TkServiceLocator.getClockLocationRuleService().getClockLocationRule(id);
087        }
088        
089        @Override
090    public void addNewLineToCollection( String collectionName ) {
091                // need to validate each new ipAddress
092        if (collectionName.equals("ipAddresses")) {
093                ClockLocationRuleIpAddress anIP = (ClockLocationRuleIpAddress)newCollectionLines.get(collectionName );
094                if(!ClockLocationRuleRule.validateIpAddress(anIP.getIpAddress())) {
095                        GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.MAINTENANCE_NEW_MAINTAINABLE +"ipAddresses", 
096                                        "ipaddress.invalid.format",anIP.getIpAddress());
097                        return;
098                }
099        }
100                super.addNewLineToCollection(collectionName);
101    }
102                
103        @Override
104        public void saveBusinessObject() {
105                ClockLocationRule clr = (ClockLocationRule) this.getBusinessObject();
106                List<ClockLocationRuleIpAddress> ips = clr.getIpAddresses();
107                super.saveBusinessObject();
108                if(!ips.isEmpty()) {
109                        for(ClockLocationRuleIpAddress ipAddress : ips) {
110                                ipAddress.setTkClockLocationRuleId(clr.getTkClockLocationRuleId());
111                                KRADServiceLocator.getBusinessObjectService().save(ipAddress);
112                        }
113            CacheUtils.flushCache(ClockLocationRule.CACHE_NAME);
114                }
115                
116        }
117}