1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.efaps.update.schema.program.staticsource;
22
23 import java.io.ByteArrayInputStream;
24 import java.io.UnsupportedEncodingException;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.efaps.ci.CIAdminProgram;
31 import org.efaps.ci.CIType;
32 import org.efaps.db.Checkin;
33 import org.efaps.db.Insert;
34 import org.efaps.db.Instance;
35 import org.efaps.db.MultiPrintQuery;
36 import org.efaps.db.QueryBuilder;
37 import org.efaps.db.Update;
38 import org.efaps.update.schema.program.jasperreport.JasperReportCompiler;
39 import org.efaps.update.schema.program.staticsource.AbstractStaticSourceCompiler.AbstractSource;
40 import org.efaps.util.EFapsException;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44
45
46
47
48
49
50
51 public abstract class AbstractStaticSourceCompiler<T extends AbstractSource>
52 {
53
54
55
56
57 protected static final Logger LOG = LoggerFactory.getLogger(AbstractStaticSourceCompiler.class);
58
59
60
61
62
63
64
65 public static void compileAll(final List<String> _classPathElements)
66 throws EFapsException
67 {
68 new JasperReportCompiler(_classPathElements).compile();
69 new CSSCompiler().compile();
70 new JavaScriptCompiler().compile();
71 new WikiCompiler().compile();
72 }
73
74
75
76
77
78
79
80
81
82
83 public void compile() throws EFapsException
84 {
85 final Map<String, String> compiled = readCompiledSources();
86
87 final List<T> allsource = readSources();
88
89 for (final T onesource : allsource) {
90
91 if (AbstractStaticSourceCompiler.LOG.isInfoEnabled()) {
92 AbstractStaticSourceCompiler.LOG.info("compiling " + onesource.getName());
93 }
94
95 final List<Instance> supers = getSuper(onesource.getInstance());
96 final StringBuilder builder = new StringBuilder();
97 while (!supers.isEmpty()) {
98 builder.append(getCompiledString(supers.get(supers.size() - 1)));
99 supers.remove(supers.size() - 1);
100 }
101 builder.append(getCompiledString(onesource.getInstance()));
102 final Update update;
103 if (compiled.containsKey(onesource.getName())) {
104 update = new Update(compiled.get(onesource.getName()));
105 } else {
106 update = new Insert(getClassName4TypeCompiled());
107 }
108 update.add("Name", onesource.getName());
109 update.add("ProgramLink", "" + onesource.getInstance().getId());
110 update.executeWithoutAccessCheck();
111 final Instance instance = update.getInstance();
112 update.close();
113
114 byte[] mybytes = null;
115 try {
116 mybytes = builder.toString().getBytes("UTF-8");
117 } catch (final UnsupportedEncodingException e) {
118 AbstractStaticSourceCompiler.LOG.error("error in reading Bytes from String using UTF-8", e);
119 }
120 final ByteArrayInputStream str = new ByteArrayInputStream(mybytes);
121 String name = onesource.getName().substring(0, onesource.getName().lastIndexOf("."));
122 name = name.substring(name.lastIndexOf(".") + 1)
123 + onesource.getName().substring(onesource.getName().lastIndexOf("."));
124
125 final Checkin checkin = new Checkin(instance);
126 checkin.executeWithoutAccessCheck(name, str, mybytes.length);
127 }
128
129 }
130
131
132
133
134
135
136 protected abstract CIType getClassName4TypeCompiled();
137
138
139
140
141
142
143 protected abstract CIType getClassName4Type();
144
145
146
147
148
149
150 protected abstract CIType getClassName4Type2Type();
151
152
153
154
155
156
157
158
159
160
161 protected abstract T getNewSource(final String _name,
162 final Instance _instance);
163
164
165
166
167
168
169
170
171 protected abstract String getCompiledString(final Instance _instance)
172 throws EFapsException;
173
174
175
176
177
178
179
180
181 protected Map<String, String> readCompiledSources()
182 throws EFapsException
183 {
184 final Map<String, String> ret = new HashMap<String, String>();
185 final QueryBuilder queryBldr = new QueryBuilder(getClassName4TypeCompiled());
186 final MultiPrintQuery multi = queryBldr.getPrint();
187 multi.addAttribute(CIAdminProgram.StaticCompiled.Name);
188 multi.executeWithoutAccessCheck();
189 while (multi.next()) {
190 final String name = multi.<String>getAttribute(CIAdminProgram.StaticCompiled.Name);
191 ret.put(name, multi.getCurrentInstance().getOid());
192 }
193 return ret;
194 }
195
196
197
198
199
200
201
202
203 protected List<T> readSources()
204 throws EFapsException
205 {
206 final List<T> ret = new ArrayList<>();
207 final QueryBuilder queryBldr = new QueryBuilder(getClassName4Type());
208 final MultiPrintQuery multi = queryBldr.getPrint();
209 multi.addAttribute(CIAdminProgram.Abstract.Name);
210 multi.executeWithoutAccessCheck();
211 while (multi.next()) {
212 final String name = multi.<String>getAttribute(CIAdminProgram.StaticCompiled.Name);
213 ret.add(getNewSource(name, multi.getCurrentInstance()));
214 }
215 return ret;
216 }
217
218
219
220
221
222
223
224
225
226 protected List<Instance> getSuper(final Instance _instance)
227 throws EFapsException
228 {
229 final List<Instance> ret = new ArrayList<Instance>();
230 final QueryBuilder queryBldr = new QueryBuilder(getClassName4Type2Type());
231 queryBldr.addWhereAttrEqValue(CIAdminProgram.Program2Program.From, _instance.getId());
232 final MultiPrintQuery multi = queryBldr.getPrint();
233 multi.addAttribute(CIAdminProgram.Program2Program.To);
234 multi.execute();
235 if (multi.next()) {
236 final Instance instance = Instance.get(getClassName4Type().getType(),
237 multi.<Long>getAttribute(CIAdminProgram.Program2Program.To));
238 ret.add(instance);
239 ret.addAll(getSuper(instance));
240 }
241 return ret;
242 }
243
244
245
246
247 public static abstract class AbstractSource
248 {
249
250
251
252 private final String name;
253
254
255
256
257 private final Instance instance;
258
259
260
261
262
263
264
265 public AbstractSource(final String _name,
266 final Instance _instance)
267 {
268 this.name = _name;
269 this.instance = _instance;
270 }
271
272
273
274
275
276
277 public String getName()
278 {
279 return this.name;
280 }
281
282
283
284
285
286
287 public Instance getInstance()
288 {
289 return this.instance;
290 }
291 }
292 }