View Javadoc
1   /**
2    * Copyright 2005-2014 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.rice.krms.impl.ui;
17  
18  import org.apache.commons.collections.CollectionUtils;
19  import org.kuali.rice.core.api.criteria.QueryByCriteria;
20  import org.kuali.rice.core.api.criteria.QueryResults;
21  import org.kuali.rice.core.api.data.DataType;
22  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
23  import org.kuali.rice.core.api.uif.RemotableAttributeField;
24  import org.kuali.rice.core.api.uif.RemotableTextInput;
25  import org.kuali.rice.krad.data.platform.MaxValueIncrementerFactory;
26  import org.kuali.rice.krad.maintenance.MaintainableImpl;
27  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
28  import org.kuali.rice.krad.uif.container.CollectionGroup;
29  import org.kuali.rice.krad.uif.container.Container;
30  import org.kuali.rice.krad.uif.view.View;
31  import org.kuali.rice.krad.uif.view.ViewModel;
32  import org.kuali.rice.krad.util.KRADConstants;
33  import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
34  import org.kuali.rice.krms.api.KrmsConstants;
35  import org.kuali.rice.krms.impl.repository.TermBo;
36  import org.kuali.rice.krms.impl.repository.TermParameterBo;
37  import org.kuali.rice.krms.impl.repository.TermResolverBo;
38  import org.kuali.rice.krms.impl.repository.TermResolverParameterSpecificationBo;
39  
40  import javax.sql.DataSource;
41  import java.util.ArrayList;
42  import java.util.Collections;
43  import java.util.Comparator;
44  import java.util.List;
45  import java.util.Map;
46  
47  /**
48   * {@link org.kuali.rice.krad.maintenance.Maintainable} for the {@link org.kuali.rice.krms.impl.ui.AgendaEditor}
49   *
50   * @author Kuali Rice Team (rice.collab@kuali.org)
51   *
52   */
53  public class TermMaintainable extends MaintainableImpl {
54  
55      private static final long serialVersionUID = 1L;
56  
57      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(TermMaintainable.class);
58  
59      public List<RemotableAttributeField> retrieveCustomAttributes(View view, Object model, Container container) {
60          List<RemotableAttributeField> results = new ArrayList<RemotableAttributeField>();
61          String termSpecId =
62                  ((TermBo) ((MaintenanceDocumentForm) model).getDocument().getNewMaintainableObject().getDataObject())
63                          .getSpecificationId();
64  
65          QueryResults<TermResolverBo> termResolvers = getDataObjectService().findMatching(TermResolverBo.class,
66                  QueryByCriteria.Builder.forAttribute("outputId", termSpecId).build());
67  
68          TermResolverBo termResolver = null;
69  
70          if (termResolvers.getResults() != null && termResolvers.getResults().size() == 1) {
71              termResolver = termResolvers.getResults().get(0);
72          }
73  
74          if (termResolver != null && !CollectionUtils.isEmpty(termResolver.getParameterSpecifications())) {
75              List<TermResolverParameterSpecificationBo> params = new ArrayList<TermResolverParameterSpecificationBo>(
76                      termResolver.getParameterSpecifications());
77  
78              Collections.sort(params, new Comparator<TermResolverParameterSpecificationBo>() {
79                  @Override
80                  public int compare(TermResolverParameterSpecificationBo o1, TermResolverParameterSpecificationBo o2) {
81                      return o1.getName().compareTo(o2.getName());
82                  }
83              });
84  
85              for (TermResolverParameterSpecificationBo param : params) {
86                  RemotableAttributeField.Builder builder = RemotableAttributeField.Builder.create(param.getName());
87                  RemotableTextInput.Builder inputBuilder = RemotableTextInput.Builder.create();
88                  inputBuilder.setSize(80);
89                  builder.setControl(inputBuilder);
90                  builder.setDataType(DataType.STRING);
91                  builder.setLongLabel(param.getName());
92                  builder.setShortLabel(param.getName());
93  
94                  results.add(builder.build());
95              }
96          }
97  
98          return results;
99      }
100 
101 //    private AgendaTypeService getAgendaTypeService(String krmsTypeId) {
102 //        //
103 //        // Get the AgendaTypeService by hook or by crook
104 //        //
105 //
106 //        KrmsTypeDefinition krmsType =
107 //                    KrmsRepositoryServiceLocator.getKrmsTypeRepositoryService().
108 //                            getTypeById(krmsTypeId);
109 //
110 //        AgendaTypeService agendaTypeService = null;
111 //
112 //        if (!StringUtils.isBlank(krmsTypeId)) {
113 //            String serviceName = krmsType.getServiceName();
114 //
115 //            if (!StringUtils.isBlank(serviceName)) {
116 //                agendaTypeService = KrmsRepositoryServiceLocator.getService(serviceName);
117 //            }
118 //        }
119 //
120 //        if (agendaTypeService == null) { agendaTypeService = AgendaTypeServiceBase.defaultAgendaTypeService; }
121 //
122 //        return agendaTypeService;
123 //    }
124 
125     @Override
126     public Object retrieveObjectForEditOrCopy(MaintenanceDocument document, Map<String, String> dataObjectKeys) {
127         TermBo termBo = (TermBo) super.retrieveObjectForEditOrCopy(document, dataObjectKeys);
128         termBo.exportToParametersMap();
129 
130         if (KRADConstants.MAINTENANCE_COPY_ACTION.equals(getMaintenanceAction())) {
131             document.getDocumentHeader().setDocumentDescription("New Term Document");
132         }
133 
134         return termBo;
135     }
136 
137     /**
138      * {@inheritDoc}
139      */
140     @Override
141     public void processAfterNew(MaintenanceDocument document, Map<String, String[]> requestParameters) {
142         super.processAfterNew(document, requestParameters);
143         document.getDocumentHeader().setDocumentDescription("New Term Document");
144     }
145 
146     /**
147      * {@inheritDoc}
148      */
149     @Override
150     public void processAfterEdit(MaintenanceDocument document, Map<String, String[]> requestParameters) {
151         super.processAfterEdit(document, requestParameters);
152         document.getDocumentHeader().setDocumentDescription("Edited Term Document");
153     }
154 
155     /**
156      * {@inheritDoc}
157      */
158     @Override
159     public void prepareForSave() {
160         TermBo term = (TermBo) getDataObject();
161         if (term.getId() == null) {
162             term.setId(MaxValueIncrementerFactory.getIncrementer(getDataSource(), TermBo.TERM_SEQ_NAME)
163                     .nextStringValue());
164         }
165         term.importFromParametersMap();
166 
167         for (TermParameterBo param : term.getParameters()) {
168             param.setTerm(term);
169         }
170     }
171 
172     /**
173      * {@inheritDoc}
174      */
175     @Override
176     public void processAfterRetrieve() {
177         super.processAfterRetrieve();
178         TermBo term = (TermBo) getDataObject();
179         term.exportToParametersMap();
180     }
181 
182     @Override
183     public Class getDataObjectClass() {
184         return TermBo.class;
185     }
186 
187     @Override
188     public void processBeforeAddLine(ViewModel model, Object addLine, String collectionId, String collectionPath) {
189         super.processBeforeAddLine(model, addLine, collectionId, collectionPath);
190     }
191 
192     public static DataSource getDataSource() {
193         return (DataSource)GlobalResourceLoader.getService(KrmsConstants.KRMS_DATA_SOURCE);
194     }
195 }