View Javadoc
1   /**
2    * Copyright 2005-2016 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 edu.sampleu.krms.impl;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.List;
21  import java.util.Map;
22  
23  import javax.jws.WebParam;
24  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
25  
26  import org.apache.commons.lang.StringUtils;
27  import org.kuali.rice.core.api.config.property.ConfigurationService;
28  import org.kuali.rice.core.api.data.DataType;
29  import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
30  import org.kuali.rice.core.api.uif.RemotableAbstractWidget;
31  import org.kuali.rice.core.api.uif.RemotableAttributeError;
32  import org.kuali.rice.core.api.uif.RemotableAttributeField;
33  import org.kuali.rice.core.api.uif.RemotableAttributeLookupSettings;
34  import org.kuali.rice.core.api.uif.RemotableQuickFinder;
35  import org.kuali.rice.core.api.uif.RemotableTextInput;
36  import org.kuali.rice.core.api.util.jaxb.MapStringStringAdapter;
37  import org.kuali.rice.krad.lookup.LookupUtils;
38  import org.kuali.rice.krms.api.repository.type.KrmsAttributeDefinition;
39  import org.kuali.rice.krms.api.repository.type.KrmsTypeAttribute;
40  import org.kuali.rice.krms.impl.type.AgendaTypeServiceBase;
41  import org.kuali.rice.location.api.campus.Campus;
42  import org.kuali.rice.location.api.services.LocationApiServiceLocator;
43  import org.kuali.rice.location.impl.campus.CampusBo;
44  import org.springframework.beans.factory.annotation.Required;
45  
46  /**
47   * Sample AgendaTypeService that creates a RemotableAttributeField for specifying the campus
48   */
49  public class CampusAgendaTypeService extends AgendaTypeServiceBase {
50  
51      private static final String CAMPUS_FIELD_NAME = "Campus";
52  
53      private ConfigurationService configurationService;
54  
55      @Override
56      public RemotableAttributeField translateTypeAttribute(KrmsTypeAttribute inputAttribute,
57              KrmsAttributeDefinition attributeDefinition) {
58  
59          if (CAMPUS_FIELD_NAME.equals(attributeDefinition.getName())) {
60              return createCampusField();
61          } else {
62              return super.translateTypeAttribute(inputAttribute,
63                  attributeDefinition);
64          }
65      }
66  
67      @Required
68      public void setConfigurationService(ConfigurationService configurationService) {
69          this.configurationService = configurationService;
70      }
71  
72      private RemotableAttributeField createCampusField() {
73  
74          String campusBoClassName = CampusBo.class.getName();
75  
76          String baseLookupUrl = LookupUtils.getBaseLookupUrl();
77  
78          RemotableQuickFinder.Builder quickFinderBuilder =
79                  RemotableQuickFinder.Builder.create(baseLookupUrl, campusBoClassName);
80  
81          quickFinderBuilder.setLookupParameters(Collections.singletonMap("Campus", "code"));
82          quickFinderBuilder.setFieldConversions(Collections.singletonMap("code","Campus"));
83  
84          RemotableTextInput.Builder controlBuilder = RemotableTextInput.Builder.create();
85          controlBuilder.setSize(30);
86          controlBuilder = RemotableTextInput.Builder.create();
87          controlBuilder.setSize(Integer.valueOf(40));
88          controlBuilder.setWatermark("campus code");
89  
90          RemotableAttributeLookupSettings.Builder lookupSettingsBuilder = RemotableAttributeLookupSettings.Builder.create();
91          lookupSettingsBuilder.setCaseSensitive(Boolean.TRUE);
92          lookupSettingsBuilder.setInCriteria(true);
93          lookupSettingsBuilder.setInResults(true);
94          lookupSettingsBuilder.setRanged(false);
95  
96          RemotableAttributeField.Builder builder = RemotableAttributeField.Builder.create(CAMPUS_FIELD_NAME);
97          builder.setAttributeLookupSettings(lookupSettingsBuilder);
98          builder.setRequired(true);
99          builder.setDataType(DataType.STRING);
100         builder.setControl(controlBuilder);
101         builder.setLongLabel("Campus");
102         builder.setShortLabel("Campus");
103         builder.setMinLength(Integer.valueOf(1));
104         builder.setMaxLength(Integer.valueOf(40));
105         builder.setWidgets(Collections.<RemotableAbstractWidget.Builder>singletonList(quickFinderBuilder));
106 
107         return builder.build();
108     }
109 
110     @Override
111     public List<RemotableAttributeError> validateAttributes(@WebParam(name = "krmsTypeId") String krmsTypeId,
112             @WebParam(name = "attributes") @XmlJavaTypeAdapter(
113                     value = MapStringStringAdapter.class) Map<String, String> attributes) throws RiceIllegalArgumentException {
114 
115         List<RemotableAttributeError> errors = new ArrayList<RemotableAttributeError>(super.validateAttributes(krmsTypeId, attributes));
116 
117         RemotableAttributeError.Builder campusErrorBuilder = RemotableAttributeError.Builder.create(CAMPUS_FIELD_NAME);
118 
119         String campusValue = attributes.get(CAMPUS_FIELD_NAME);
120 
121         if (StringUtils.isEmpty(campusValue)) {
122             campusErrorBuilder.addErrors(configurationService.getPropertyValueAsString("error.agenda.invalidAttributeValue"));
123         } else {
124             Campus campus = LocationApiServiceLocator.getCampusService().getCampus(campusValue);
125 
126             if (campus == null) {
127                 campusErrorBuilder.addErrors(configurationService.getPropertyValueAsString("error.agenda.invalidAttributeValue"));
128             }
129         }
130 
131         if (campusErrorBuilder.getErrors().size() > 0) {
132             errors.add(campusErrorBuilder.build());
133         }
134 
135         return errors;
136     }
137 }