View Javadoc
1   /*
2    * Copyright 2006 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.fp.businessobject.format;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.ole.sys.OLEConstants;
20  import org.kuali.ole.sys.OLEKeyConstants;
21  import org.kuali.ole.sys.context.SpringContext;
22  import org.kuali.rice.core.api.config.property.ConfigurationService;
23  import org.kuali.rice.core.web.format.Formatter;
24  
25  public class CashDrawerStatusCodeFormatter extends Formatter {
26      private final String CLOSED_CD;
27      private final String CLOSED_MSG;
28  
29      private final String OPEN_CD;
30      private final String OPEN_MSG;
31  
32      private final String LOCKED_CD;
33      private final String LOCKED_MSG;
34  
35      public CashDrawerStatusCodeFormatter() {
36          CLOSED_CD = OLEConstants.CashDrawerConstants.STATUS_CLOSED;
37          CLOSED_MSG = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(OLEKeyConstants.CashDrawer.CASH_DRAWER_STATUS_CLOSED);
38  
39          OPEN_CD = OLEConstants.CashDrawerConstants.STATUS_OPEN;
40          OPEN_MSG = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(OLEKeyConstants.CashDrawer.CASH_DRAWER_STATUS_OPEN);
41  
42          LOCKED_CD = OLEConstants.CashDrawerConstants.STATUS_LOCKED;
43          LOCKED_MSG = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(OLEKeyConstants.CashDrawer.CASH_DRAWER_STATUS_LOCKED);
44      }
45  
46  
47      /**
48       * Converts the given statusCode into a status message.
49       */
50      protected Object convertToObject(String target) {
51          Object result = target;
52  
53          if (target instanceof String) {
54              String message = (String) target;
55  
56              if (StringUtils.equals(message, OPEN_MSG)) {
57                  result = OPEN_CD;
58              }
59              else if (StringUtils.equals(message, CLOSED_MSG)) {
60                  result = CLOSED_CD;
61              }
62              else if (StringUtils.equals(message, LOCKED_MSG)) {
63                  result = LOCKED_CD;
64              }
65          }
66  
67          return result;
68      }
69  
70      /**
71       * Converts the given status message into a status code.
72       */
73      public Object format(Object value) {
74          Object formatted = value;
75  
76          if (value instanceof String) {
77              String statusCode = (String) value;
78  
79              if (StringUtils.equals(statusCode, CLOSED_CD)) {
80                  formatted = CLOSED_MSG;
81              }
82              else if (StringUtils.equals(statusCode, OPEN_CD)) {
83                  formatted = OPEN_MSG;
84              }
85              else if (StringUtils.equals(statusCode, LOCKED_CD)) {
86                  formatted = LOCKED_MSG;
87              }
88          }
89  
90          return formatted;
91      }
92  
93  }