View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.module.tem.batch.service.impl;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.kuali.kfs.module.tem.TemKeyConstants;
25  import org.kuali.kfs.module.tem.batch.service.PerDiemLoadValidationService;
26  import org.kuali.kfs.module.tem.businessobject.PerDiem;
27  import org.kuali.kfs.module.tem.service.PerDiemService;
28  import org.kuali.kfs.sys.KFSConstants;
29  import org.kuali.kfs.sys.KFSKeyConstants;
30  import org.kuali.kfs.sys.Message;
31  import org.kuali.kfs.sys.MessageBuilder;
32  import org.kuali.rice.core.api.util.type.KualiDecimal;
33  import org.kuali.rice.krad.service.DictionaryValidationService;
34  import org.kuali.rice.krad.util.ErrorMessage;
35  import org.kuali.rice.krad.util.GlobalVariables;
36  import org.kuali.rice.krad.util.MessageMap;
37  import org.kuali.rice.krad.util.ObjectUtils;
38  
39  /**
40   * implement the validation methods against the given per diem
41   */
42  public class PerDiemLoadValidationServiceImpl implements PerDiemLoadValidationService {
43  
44      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PerDiemLoadValidationServiceImpl.class);
45  
46      private DictionaryValidationService dictionaryValidationService;
47      private PerDiemService perDiemService;
48  
49      /**
50       * @see org.kuali.kfs.module.tem.batch.service.PerDiemLoadValidationService#validate(java.util.List)
51       */
52      @Override
53      public <T extends PerDiem> boolean validate(List<T> perDiemList) {
54          MessageMap messageMap = GlobalVariables.getMessageMap();
55  
56          for(T perDiem: perDiemList){
57              List<Message> errorMessages = this.validate(perDiem);
58  
59              this.populateMessageMap(messageMap, errorMessages);
60  
61              if(ObjectUtils.isNotNull(messageMap) && messageMap.hasErrors()){
62                  return false;
63              }
64          }
65  
66          return true;
67      }
68  
69      /**
70       * @see org.kuali.kfs.module.tem.batch.service.PerDiemLoadValidationService#validate(org.kuali.kfs.module.tem.businessobject.PerDiem)
71       */
72      @Override
73      public <T extends PerDiem> List<Message> validate(T perDiem) {
74          List<Message> meesageList = new ArrayList<Message>();
75  
76          this.getDictionaryValidationService().isBusinessObjectValid(perDiem);
77          MessageMap messageMap = GlobalVariables.getMessageMap();
78  
79          if(messageMap.hasErrors()){
80              List<Message> message = this.translateErrorsFromErrorMap(messageMap);
81              meesageList.addAll(message);
82  
83              messageMap.clearErrorMessages();
84          }
85  
86          if(!isValidMealsAndIncidentals(perDiem)){
87              Message message = MessageBuilder.buildMessageWithPlaceHolder(TemKeyConstants.ERROR_PER_DIEM_MEAL_INCIDENTAL_NON_POSITIVE_AMOUNT, perDiem.getPrimaryDestination().getRegion().getRegionName(), perDiem.getPrimaryDestination(), perDiem.getLineNumber(), perDiem.getMealsAndIncidentals());
88              meesageList.add(message);
89          }
90  
91          return meesageList;
92      }
93  
94      protected boolean isValidMealsAndIncidentals(PerDiem perDiem){
95          KualiDecimal mealsAndIncidentals = perDiem.getMealsAndIncidentals();
96  
97          return ObjectUtils.isNotNull(mealsAndIncidentals) && mealsAndIncidentals.isPositive();
98      }
99  
100     /**
101      * put the error message into the given message map
102      */
103     protected void populateMessageMap(MessageMap messageMap, List<Message> errorMessages) {
104         for(Message message : errorMessages){
105             messageMap.putError(KFSConstants.GLOBAL_ERRORS, KFSKeyConstants.ERROR_CUSTOM, message.getMessage());
106         }
107     }
108 
109     /**
110      * Builds actual error message from error key and parameters.
111      */
112     protected List<Message> translateErrorsFromErrorMap(MessageMap errorMap) {
113         List<Message> errors = new ArrayList<Message>();
114 
115         for (String errorKey : errorMap.getPropertiesWithErrors()) {
116 
117             for (Object message : errorMap.getMessages(errorKey)) {
118                 ErrorMessage errorMessage = (ErrorMessage)message;
119 
120                 Message messageWithPlaceHolder = MessageBuilder.buildMessageWithPlaceHolder(errorMessage.getErrorKey(), (Object[])errorMessage.getMessageParameters());
121 
122                 errors.add(messageWithPlaceHolder);
123             }
124         }
125 
126         return errors;
127     }
128 
129     /**
130      * Gets the dictionaryValidationService attribute.
131      * @return Returns the dictionaryValidationService.
132      */
133     public DictionaryValidationService getDictionaryValidationService() {
134         return dictionaryValidationService;
135     }
136 
137     /**
138      * Sets the dictionaryValidationService attribute value.
139      * @param dictionaryValidationService The dictionaryValidationService to set.
140      */
141     public void setDictionaryValidationService(DictionaryValidationService dictionaryValidationService) {
142         this.dictionaryValidationService = dictionaryValidationService;
143     }
144 
145     /**
146      * Gets the perDiemService attribute.
147      * @return Returns the perDiemService.
148      */
149     public PerDiemService getPerDiemService() {
150         return perDiemService;
151     }
152 
153     /**
154      * Sets the perDiemService attribute value.
155      * @param perDiemService The perDiemService to set.
156      */
157     public void setPerDiemService(PerDiemService perDiemService) {
158         this.perDiemService = perDiemService;
159     }
160 }