View Javadoc

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