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.util;
017    
018    import org.apache.commons.collections.Closure;
019    import org.apache.commons.collections.Predicate;
020    import org.apache.commons.lang.StringUtils;
021    import org.kuali.kfs.module.cam.CamsConstants;
022    import org.kuali.kfs.module.cam.businessobject.BarcodeInventoryErrorDetail;
023    import org.kuali.kfs.module.cam.document.BarcodeInventoryErrorDocument;
024    
025    /**
026     * 
027     * Helps filter out records from a collection of BCIE and replace its elements with the inputted data 
028     */
029    public class BarcodeInventoryErrorDetailPredicate implements Predicate, Closure {
030        private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BarcodeInventoryErrorDetailPredicate.class);
031        private BarcodeInventoryErrorDocument doc;
032    
033        /**
034         * 
035         * Constructs a BarcodeInventoryErrorDetailPredicate.java.
036         * @param form
037         */
038        public BarcodeInventoryErrorDetailPredicate(BarcodeInventoryErrorDocument doc) {
039            this.doc = doc;
040        }
041    
042        /**
043         * 
044         * @see org.apache.commons.collections.Predicate#evaluate(java.lang.Object)
045         */
046        public boolean evaluate(Object object) {
047            boolean satisfies = true;
048            
049            if (object instanceof BarcodeInventoryErrorDetail) {            
050                
051                BarcodeInventoryErrorDetail detail = (BarcodeInventoryErrorDetail) object;
052                
053                // It will only replace when the status is equals to error
054                if (detail.getErrorCorrectionStatusCode().equals(CamsConstants.BarCodeInventoryError.STATUS_CODE_ERROR)) {            
055                    if ((this.doc.getCurrentTagNumber() != null) && !StringUtils.isBlank(this.doc.getCurrentTagNumber())) {
056                        if (!StringUtils.equals(this.doc.getCurrentTagNumber(), detail.getAssetTagNumber())) {
057                            satisfies = false;
058                        }
059                    }
060        
061                    if (this.doc.getCurrentScanCode() != null && !StringUtils.isBlank(this.doc.getCurrentScanCode())) {
062                        satisfies = (this.doc.getCurrentScanCode().equals("Y") && detail.isUploadScanIndicator());
063                    }
064        
065                    if (this.doc.getCurrentCampusCode() != null && !StringUtils.isBlank(this.doc.getCurrentCampusCode())) {
066                        if (!StringUtils.equals(this.doc.getCurrentCampusCode(), detail.getCampusCode())) {
067                            satisfies = false;
068                        }
069                    }
070        
071                    if ((this.doc.getCurrentBuildingNumber() != null) && !StringUtils.isBlank(this.doc.getCurrentBuildingNumber())) {
072                        if (!StringUtils.equals(this.doc.getCurrentBuildingNumber(), detail.getBuildingCode())) {
073                            satisfies = false;
074                        }
075                    }
076        
077                    if ((this.doc.getCurrentRoom() != null) && !StringUtils.isBlank(this.doc.getCurrentRoom())) {
078                        if (!StringUtils.equals(this.doc.getCurrentRoom(), detail.getBuildingRoomNumber())) {
079                            satisfies = false;
080                        }
081                    }
082        
083                    if ((this.doc.getCurrentSubroom() != null) && !StringUtils.isBlank(this.doc.getCurrentSubroom())) {
084                        if (!StringUtils.equals(this.doc.getCurrentSubroom(), detail.getBuildingSubRoomNumber())) {
085                            satisfies = false;
086                        }
087                    }
088        
089                    if ((this.doc.getCurrentConditionCode() != null) && !StringUtils.isBlank(this.doc.getCurrentConditionCode())) {
090                        if (!StringUtils.equals(this.doc.getCurrentConditionCode(), detail.getAssetConditionCode())) {
091                            satisfies = false;
092                        }
093                    }
094                }
095                else {
096                    satisfies = false;
097                }
098            }
099            return satisfies;
100        }
101    
102        /**
103         * 
104         * @see org.apache.commons.collections.Closure#execute(java.lang.Object)
105         */
106        public void execute(Object object) {
107            if (this.evaluate(object)) {
108                BarcodeInventoryErrorDetail detail = (BarcodeInventoryErrorDetail) object;
109                if (this.doc.getNewCampusCode() != null && !StringUtils.isBlank(this.doc.getNewCampusCode())) {
110                    detail.setCampusCode(this.doc.getNewCampusCode());
111                }
112    
113                if ((this.doc.getNewBuildingNumber() != null) && !StringUtils.isBlank(this.doc.getNewBuildingNumber())) {
114                    detail.setBuildingCode(this.doc.getNewBuildingNumber());
115                }
116    
117                if ((this.doc.getNewRoom() != null) && !StringUtils.isBlank(this.doc.getNewRoom())) {
118                    detail.setBuildingRoomNumber(this.doc.getNewRoom());
119                }
120    
121                if ((this.doc.getNewSubroom() != null) && !StringUtils.isBlank(this.doc.getNewSubroom())) {
122                    detail.setBuildingSubRoomNumber(this.doc.getNewSubroom());
123                }
124    
125                if ((this.doc.getNewConditionCode() != null) && !StringUtils.isBlank(this.doc.getNewConditionCode())) {
126                    detail.setAssetConditionCode(this.doc.getNewConditionCode());
127                }
128            }
129        }
130    }