1 | |
package org.kuali.student.lum.lu.assembly; |
2 | |
|
3 | |
import java.util.Iterator; |
4 | |
|
5 | |
import org.apache.commons.lang.StringUtils; |
6 | |
import org.apache.log4j.Logger; |
7 | |
import org.kuali.student.common.assembly.data.AssemblyException; |
8 | |
import org.kuali.student.common.assembly.data.Data; |
9 | |
import org.kuali.student.common.assembly.data.Metadata; |
10 | |
import org.kuali.student.common.assembly.data.Data.DataType; |
11 | |
import org.kuali.student.common.assembly.data.Data.Property; |
12 | |
import org.kuali.student.common.assembly.dictionary.MetadataServiceImpl; |
13 | |
import org.kuali.student.common.assembly.old.Assembler; |
14 | |
import org.kuali.student.common.assembly.old.PassThroughAssemblerFilter; |
15 | |
import org.kuali.student.common.assembly.old.data.SaveResult; |
16 | |
import org.kuali.student.common.assembly.util.IdTranslation; |
17 | |
import org.kuali.student.common.assembly.util.IdTranslator; |
18 | |
import org.kuali.student.lum.common.client.widgets.CluSetHelper; |
19 | |
|
20 | |
public class CluSetManagementIdTranslatorAssemblerFilter extends PassThroughAssemblerFilter<Data, Void> { |
21 | |
|
22 | 0 | final Logger LOG = Logger.getLogger(CluSetManagementIdTranslatorAssemblerFilter.class); |
23 | |
|
24 | |
private IdTranslator idTranslator; |
25 | |
private MetadataServiceImpl metadataService; |
26 | |
|
27 | |
public MetadataServiceImpl getMetadataService() { |
28 | 0 | return metadataService; |
29 | |
} |
30 | |
|
31 | |
public void setMetadataService(MetadataServiceImpl metadataService) { |
32 | 0 | this.metadataService = metadataService; |
33 | 0 | } |
34 | |
|
35 | 0 | public CluSetManagementIdTranslatorAssemblerFilter(IdTranslator idTranslator) { |
36 | 0 | this.idTranslator = idTranslator; |
37 | 0 | } |
38 | |
|
39 | |
@Override |
40 | |
public void doGetFilter(FilterParamWrapper<String> id, FilterParamWrapper<Data> response, GetFilterChain<Data, Void> chain) throws AssemblyException { |
41 | 0 | chain.doGetFilter(id, response); |
42 | 0 | Data data = response.getValue(); |
43 | 0 | if (data != null) { |
44 | 0 | translateIds(data, chain); |
45 | |
} |
46 | 0 | } |
47 | |
|
48 | |
@Override |
49 | |
public void doSaveFilter(FilterParamWrapper<Data> request, FilterParamWrapper<SaveResult<Data>> response, SaveFilterChain<Data, Void> chain) throws AssemblyException { |
50 | 0 | chain.doSaveFilter(request, response); |
51 | 0 | SaveResult<Data> saveResult = response.getValue(); |
52 | 0 | Data data = saveResult != null && saveResult.getValue() != null ? saveResult.getValue() : null; |
53 | 0 | if(data != null) { |
54 | 0 | translateIds(data, chain); |
55 | |
} |
56 | 0 | } |
57 | |
|
58 | |
private void translateIds(Data data, AssemblerManagerAccessable<Data, Void> chain) throws AssemblyException { |
59 | 0 | CluSetHelper cluSetHelper = new CluSetHelper(data); |
60 | 0 | Assembler<Data, Void> a = chain.getManager().getTarget(); |
61 | 0 | Metadata metadata = null; |
62 | 0 | if (cluSetHelper.getType() == null || cluSetHelper.getType().equals("kuali.cluSet.type.CreditCourse")) { |
63 | 0 | metadata = metadataService.getMetadata("courseSet"); |
64 | |
} else { |
65 | 0 | metadata = metadataService.getMetadata("programSet"); |
66 | |
} |
67 | 0 | if (metadata != null && data != null) { |
68 | 0 | __translateIds(data, metadata); |
69 | |
} |
70 | 0 | } |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
private void __translateIds(Data data, Metadata metadata) |
83 | |
throws AssemblyException { |
84 | |
try{ |
85 | 0 | if (data != null && metadata != null) { |
86 | |
|
87 | 0 | for (Iterator<Property> iter = data.realPropertyIterator(); iter.hasNext();) { |
88 | 0 | Property prop = iter.next(); |
89 | |
|
90 | 0 | Object fieldData = prop.getValue(); |
91 | 0 | Object fieldKey = prop.getKey(); |
92 | |
|
93 | 0 | Metadata fieldMetadata = metadata.getProperties().get(fieldKey); |
94 | |
|
95 | |
|
96 | 0 | if(fieldMetadata==null){ |
97 | 0 | fieldMetadata=metadata; |
98 | |
} |
99 | |
|
100 | |
|
101 | 0 | if (fieldData instanceof Data) { |
102 | 0 | if (DataType.LIST.equals(fieldMetadata.getDataType())) { |
103 | |
|
104 | 0 | Metadata listChildMetadata = fieldMetadata.getProperties().get("*"); |
105 | |
|
106 | 0 | if(DataType.DATA.equals(listChildMetadata.getDataType())){ |
107 | 0 | __translateIds((Data) prop.getValue(), listChildMetadata); |
108 | |
}else{ |
109 | |
|
110 | 0 | for(Iterator<Property> listIter = ((Data)fieldData).realPropertyIterator(); listIter.hasNext();){ |
111 | 0 | Property listItem = listIter.next(); |
112 | 0 | Object listData = listItem.getValue(); |
113 | 0 | if (listData != null && listData instanceof String) { |
114 | 0 | if (fieldMetadata.getInitialLookup() != null |
115 | |
&& !StringUtils.isEmpty((String) listData)) { |
116 | |
|
117 | 0 | IdTranslation trans = idTranslator.getTranslation(fieldMetadata.getInitialLookup(), (String) listData); |
118 | 0 | if (trans != null) { |
119 | 0 | Integer index = listItem.getKey(); |
120 | 0 | setTranslation((Data)fieldData, listItem.getKey().toString(), index, trans.getDisplay()); |
121 | |
} |
122 | |
} |
123 | |
} |
124 | 0 | } |
125 | |
} |
126 | 0 | } else { |
127 | |
|
128 | 0 | __translateIds((Data) prop.getValue(), fieldMetadata); |
129 | |
} |
130 | 0 | } else if (fieldData != null && fieldData instanceof String) { |
131 | 0 | if (fieldMetadata.getInitialLookup() != null |
132 | |
&& !StringUtils.isEmpty((String) fieldData)) { |
133 | |
|
134 | 0 | IdTranslation trans = idTranslator.getTranslation(fieldMetadata.getInitialLookup(), (String) fieldData); |
135 | 0 | if (trans != null) { |
136 | 0 | setTranslation(data, prop.getKey().toString(), null, trans.getDisplay()); |
137 | |
} |
138 | |
} |
139 | |
} |
140 | 0 | } |
141 | |
} |
142 | 0 | }catch(Exception e){ |
143 | 0 | LOG.error("Error translating", e); |
144 | 0 | } |
145 | 0 | } |
146 | |
|
147 | |
private static void setTranslation(Data data, String field, Integer index, String translation) { |
148 | 0 | if (data != null) { |
149 | |
|
150 | 0 | Data runtime = data.get("_runtimeData"); |
151 | 0 | if (runtime == null) { |
152 | 0 | runtime = new Data(); |
153 | 0 | data.set("_runtimeData", runtime); |
154 | |
} |
155 | 0 | if(index != null) { |
156 | |
|
157 | 0 | Data fieldIndexData = runtime.get(index); |
158 | 0 | if(fieldIndexData==null){ |
159 | 0 | fieldIndexData = new Data(); |
160 | 0 | runtime.set(index, fieldIndexData); |
161 | |
} |
162 | 0 | fieldIndexData.set("id-translation", translation); |
163 | 0 | }else{ |
164 | |
|
165 | |
|
166 | 0 | Data fieldData = runtime.get(field); |
167 | 0 | if(fieldData==null){ |
168 | 0 | fieldData = new Data(); |
169 | 0 | runtime.set(field, fieldData); |
170 | |
} |
171 | 0 | fieldData.set("id-translation", translation); |
172 | |
} |
173 | |
} |
174 | 0 | } |
175 | |
} |