1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
49
50
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
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
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
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
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 }