1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.fatwire.gst.foundation.facade.assetapi.code;
18
19 import java.io.IOException;
20 import java.io.PrintWriter;
21 import java.io.StringWriter;
22 import java.io.Writer;
23 import java.util.Arrays;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Set;
27
28 import javax.servlet.jsp.JspWriter;
29
30 import COM.FutureTense.Interfaces.ICS;
31
32 import com.fatwire.assetapi.common.AssetAccessException;
33 import com.fatwire.assetapi.def.AssetAssociationDef;
34 import com.fatwire.assetapi.def.AssetTypeDef;
35 import com.fatwire.assetapi.def.AssetTypeDefManager;
36 import com.fatwire.assetapi.def.AssetTypeDefProperties;
37 import com.fatwire.assetapi.def.AttributeDef;
38 import com.fatwire.assetapi.def.AttributeDefProperties;
39 import com.fatwire.assetapi.def.AttributeTypeEnum;
40 import com.fatwire.gst.foundation.DebugHelper;
41 import com.fatwire.system.Session;
42 import com.fatwire.system.SessionFactory;
43
44 import org.apache.commons.lang.StringEscapeUtils;
45
46 public class CodeGen {
47
48 static Set<String> KEYWORDS = new HashSet<String>(
49 Arrays.asList(("abstract,assert,boolean,break,byte,case,catch,char,class,const,continue"
50 + ",default,do,double,else,enum,extends,final,finally,float"
51 + ",for,goto,if,implements,import,instanceof,int,interface,long,native,new,"
52 + "ackage,private,protected,public,return,short,static,strictfp,super,switch,"
53 + "synchronized,this,throw,throws,transient,try,void,volatile,while").split(",")));
54 static Set<String> USELESS_ATTRIBUTES = new HashSet<String>(
55 Arrays.asList(("urlexternaldoc,externaldoctype,urlexternaldocxml").split(",")));
56
57 interface Generator {
58
59 void printAssetTypeDef(AssetTypeDef def);
60
61 }
62
63 static class DefPrint implements Generator {
64 PrintWriter out;
65
66 public DefPrint(Writer out2) {
67 out = new PrintWriter(out2);
68 }
69
70 public void printAssetTypeDef(AssetTypeDef def) {
71 if (def == null)
72 return;
73 if (def.getSubtype() == null && def.getProperties().getIsFlexAsset()) {
74 return;
75 }
76 String s = def.getSubtype() == null ? "" : ("/" + def.getSubtype());
77
78 out.print(def.getName() + s);
79 out.print(" // description: '" + def.getDescription() + "'");
80 out.print(" plural: '" + def.getPlural() + "'");
81 out.print(" canbechild: " + def.getCanBeChild() + "");
82 AssetTypeDefProperties p = def.getProperties();
83
84 out.print(" canAddSubtypes: " + p.getCanAddSubtypes());
85 String type = p.getIsAssetmakerAsset() ? "AssetmakerAsset" : "Unknown Type";
86 if (p.getIsCoreAsset())
87 type = "CoreAsset";
88 if (p.getIsFlexAsset())
89 type = "FlexAsset";
90 out.print(" type: '" + type + "'");
91 out.println(" nameMustUnique: " + p.getIsNameMustUnique());
92
93 printAttributes(def.getAttributeDefs());
94 printParentDefs(def.getParentDefs());
95 printAssocations(def.getAssociations());
96
97 out.println();
98 out.println();
99 }
100
101 private void printParentDefs(List<AttributeDef> list) {
102 printAttributeList(list, "parents");
103
104 }
105
106 private void printAssocations(List<AssetAssociationDef> assocs) {
107 if (assocs != null && !assocs.isEmpty()) {
108 out.println("associations: ");
109 for (AssetAssociationDef assoc : assocs) {
110
111 out.println("assoc name: '" + assoc.getName() + "'");
112 out.println("description: '" + assoc.getDescription() + "'");
113 out.println("multiple: '" + assoc.isMultiple() + "'");
114 out.println("legalChildAssetTypes: '" + assoc.getLegalChildAssetTypes() + "'");
115 out.println("childAssetType: '" + assoc.getChildAssetType() + "'");
116 out.println("subTypes: '" + assoc.getSubTypes() + "'");
117 out.println();
118 }
119 }
120
121 }
122
123 private void printAttributes(List<AttributeDef> list) {
124 printAttributeList(list, "attributes");
125 }
126
127 private void printAttributeList(List<AttributeDef> attributes, String label) {
128
129 if (attributes != null && !attributes.isEmpty()) {
130 out.println(label + ": ");
131
132 for (AttributeDef a : attributes) {
133
134 out.print("name: '" + a.getName() + "'");
135 out.print(" description: '" + a.getDescription() + "'");
136 out.print(" type: '" + a.getType() + "'");
137
138 AttributeDefProperties p = a.getProperties();
139 out.print(" size: '" + p.getSize() + "'");
140 out.print(" count: '" + p.getValueCount() + "'");
141 if (a.getType() == AttributeTypeEnum.ASSET) {
142 out.print(" assettype: '" + p.getAssetType() + "'");
143 out.print(" assettype: '" + p.getAssetType() + "'");
144 }
145 if (a.isDataMandatory())
146 out.print(" mandatory");
147 if (a.isMetaDataAttribute())
148 out.print(" meta");
149
150 if (p.isAllowEmbeddedLinks())
151 out.print(" allow embedded");
152 if (p.isDerivedFlexAttribute())
153 out.print(" derived");
154 if (p.isInheritedFlexAttribute())
155 out.print(" inherited");
156
157 if (p.getMultiple() != null)
158 out.print(" multiple: '" + p.getMultiple() + "'");
159 if (p.getOrdinal() != null)
160 out.print(" ordinal: '" + p.getOrdinal() + "'");
161 if (p.getRequired() != null)
162 out.print(" required: '" + p.getRequired() + "'");
163 out.println();
164 }
165 }
166 }
167
168 }
169
170 static class AssetApiGen implements Generator {
171 protected PrintWriter out;
172
173 public AssetApiGen(Writer writer) {
174 out = new PrintWriter(writer);
175 }
176
177 public void printAssetTypeDef(AssetTypeDef def) {
178 if (def == null)
179 return;
180 if (def.getSubtype() == null && def.getProperties().getIsFlexAsset()) {
181 return;
182 }
183 String s = def.getSubtype() == null ? "" : def.getSubtype() + "/";
184
185 out.print(def.getName() + "/" + s + "Detail");
186 out.print(" // description: '" + def.getDescription() + "'");
187 out.print(" plural: '" + def.getPlural() + "'");
188 out.print(" canbechild: " + def.getCanBeChild() + "");
189 AssetTypeDefProperties p = def.getProperties();
190 out.print(" canAddSubtypes: " + p.getCanAddSubtypes());
191 String type = p.getIsAssetmakerAsset() ? "AssetmakerAsset" : "Unknown Type";
192 if (p.getIsCoreAsset())
193 type = "CoreAsset";
194 if (p.getIsFlexAsset())
195 type = "FlexAsset";
196 out.print(" type: '" + type + "'");
197 out.println(" nameMustUnique: " + p.getIsNameMustUnique());
198
199 printAttributes(def);
200 printParentDefs(def.getParentDefs());
201 printAssocations(def.getAssociations());
202 out.println();
203 out.println();
204 }
205
206 private void printAssocations(List<AssetAssociationDef> associations) {
207
208
209 }
210
211 private void printParentDefs(List<AttributeDef> parentDefs) {
212
213
214 }
215
216 protected String toVarName(String name) {
217 if (name == null || name.length() == 0)
218 return "noname";
219 if (KEYWORDS.contains(name))
220 return name + "_";
221 char[] t = name.replace("-", "_").toCharArray();
222 t[0] = Character.toLowerCase(t[0]);
223 return new String(t);
224 }
225
226 protected void printAttributes(AssetTypeDef def) {
227 StringWriter sw = new StringWriter();
228 PrintWriter pw = new PrintWriter(sw);
229
230 pw.println("//sample copy/paste code");
231 pw.println("public void read" + def.getName() + (def.getSubtype() != null ? "_" + def.getSubtype() : "")
232 + "(ICS ics) throws AssetNotExistException, AssetAccessException {");
233 pw.println();
234 pw.println(" Session ses = SessionFactory.getSession(ics);");
235 pw.println(" AssetDataManager mgr = (AssetDataManager) ses.getManager(AssetDataManager.class.getName());");
236 pw.println(" AssetId id = new AssetIdImpl(ics.GetVar(\"c\"), Long.parseLong(ics.GetVar(\"cid\")));");
237 pw.println(" Iterable<AssetData> assets = mgr.read(Collections.singletonList(id));");
238
239 pw.println(" for (AssetData asset : assets) {");
240 pw.println("");
241
242 List<AttributeDef> attributes = def.getAttributeDefs();
243 if (attributes != null) {
244 pw.println(" // attributes: ");
245 pw.println(" AttributeData attribute = null;");
246
247 for (AttributeDef a : attributes) {
248 if (USELESS_ATTRIBUTES.contains(a.getName()))
249 continue;
250 AttributeTypeEnum t = a.getType();
251 String cast = toCastType(t);
252 String name = toVarName(a.getName());
253 pw.println(" attribute = asset.getAttributeData(\"" + a.getName() + "\", "
254 + a.isMetaDataAttribute() + ");");
255 pw.println(" if (attribute != null){");
256 pw.println(" // type = " + t + ", valueCount = " + a.getProperties().getValueCount());
257 boolean singleValued = AttributeDefProperties.ValueCount.SINGLE.equals(a.getProperties()
258 .getValueCount());
259 if (singleValued) {
260 pw.println(" " + cast + " " + name + " = (" + cast + ")attribute.getData();");
261 } else {
262 pw.println(" List<?> " + name + " = attribute.getDataAsList();");
263 }
264 if (a.getProperties().getMultiple() != null) {
265 pw.println(" multiple " + a.getProperties().getMultiple());
266
267 }
268
269
270
271
272
273 switch (t) {
274 case BLOB:
275 case URL:
276 pw.println(" BlobAddress address = " + name + ".getBlobAddress();");
277 pw.println(" String blobcol = address.getColumnName();");
278 pw.println(" Object blobid = address.getIdentifier();");
279 pw.println(" String idcol = address.getIdentifierColumnName();");
280 pw.println(" String blobtable = address.getTableName();");
281 pw.println(" //InputStream stream = " + name + ".getBinaryStream();");
282 break;
283 default:
284 break;
285
286 }
287
288 pw.println(" }");
289 }
290 pw.println(" }");
291 pw.println("}");
292 out.print(org.apache.commons.lang.StringEscapeUtils.escapeHtml(sw.toString()));
293 }
294 }
295
296 String toCastType(AttributeTypeEnum t) {
297
298 switch (t) {
299 case INT:
300 return "Integer";
301 case FLOAT:
302 return "Double";
303 case STRING:
304 return "String";
305 case LONG:
306 return "Long";
307 case DATE:
308 return "Date";
309 case MONEY:
310 return "Double";
311 case LARGE_TEXT:
312 return "String";
313 case ASSET:
314 return "AssetId";
315 case ASSETREFERENCE:
316 return "AssetId";
317 case BLOB:
318 return "BlobObject";
319 case URL:
320 return "BlobObject";
321 case ARRAY:
322 return "List<?>";
323 case STRUCT:
324 return "Map<?,?>";
325 case LIST:
326 return "List<?>";
327 case ONEOF:
328 return "Object";
329 }
330 throw new IllegalArgumentException("Don't know about " + t);
331 }
332 }
333
334 static class HtmlWriter extends Writer {
335
336 final Writer writer;
337
338
339
340
341 private HtmlWriter(Writer writer) {
342 super();
343 this.writer = writer;
344 }
345
346 @Override
347 public void close() throws IOException {
348 writer.close();
349
350 }
351
352 @Override
353 public void flush() throws IOException {
354 writer.flush();
355
356 }
357
358 @Override
359 public void write(char[] cbuf, int off, int len) throws IOException {
360 StringEscapeUtils.escapeHtml(writer, new String(cbuf, off, len));
361
362 }
363
364 }
365
366 static public class GsfJavaGen extends AssetApiGen {
367
368 public GsfJavaGen(Writer writer) {
369 super(writer);
370 }
371
372 protected void printAttributes(List<AttributeDef> attributes) {
373 StringWriter sw = new StringWriter();
374 PrintWriter pw = new PrintWriter(sw);
375
376 if (attributes != null) {
377
378
379 for (AttributeDef a : attributes) {
380 if (USELESS_ATTRIBUTES.contains(a.getName()))
381 continue;
382 AttributeTypeEnum t = a.getType();
383 String cast = toCastType(t);
384 String name = toVarName(a.getName());
385 String method = toMethodName(a.getType());
386 pw.println("");
387
388
389 boolean singleValued = AttributeDefProperties.ValueCount.SINGLE.equals(a.getProperties()
390 .getValueCount());
391 if (singleValued) {
392 pw.println(" " + cast + " " + name + " = asset." + method + "(\"" + a.getName() + "\");");
393 } else {
394 pw.println(" List<?> " + name + " = asset.asList(\"" + a.getName() + "\");");
395 }
396
397 switch (t) {
398 case BLOB:
399 case URL:
400 pw.println("");
401 pw.println(" /*");
402 pw.println(" BlobAddress address = asset.asBlobAddress(\"" + a.getName() + "\");");
403 pw.println(" String blobcol = address.getColumnName();");
404 pw.println(" Object blobid = address.getIdentifier();");
405 pw.println(" String idcol = address.getIdentifierColumnName();");
406 pw.println(" String blobtable = address.getTableName();");
407 pw.println("");
408 pw.println(" String uri =new BlobUriBuilder(address).mimeType(\"image/jpeg\").toURI(ics);");
409
410 pw.println(" */");
411 pw.println(" //InputStream stream = " + name + ".getBinaryStream();");
412 break;
413
414 }
415
416 }
417
418
419
420 }
421 }
422
423 private String toMethodName(AttributeTypeEnum t) {
424
425 switch (t) {
426 case INT:
427 return "asInt";
428 case FLOAT:
429 return "asDouble";
430 case STRING:
431 return "asString";
432 case LONG:
433 return "asLong";
434 case DATE:
435 return "asDate";
436 case MONEY:
437 return "asDouble";
438 case LARGE_TEXT:
439 return "asString";
440 case ASSET:
441 return "asAssetId";
442 case ASSETREFERENCE:
443 return "asAssetId";
444 case BLOB:
445 return "asBlob";
446 case URL:
447 return "asBlob";
448 case ARRAY:
449 return "asList";
450 case STRUCT:
451 return "asMap";
452 case LIST:
453 return "asList";
454 case ONEOF:
455 return "asObject";
456 }
457 throw new IllegalArgumentException("Don't know about " + t);
458 }
459
460
461
462
463
464
465
466
467 @Override
468 public void printAssetTypeDef(AssetTypeDef def) {
469
470 out.println("//sample copy/paste code");
471 out.println("public void read" + def.getName() + (def.getSubtype() != null ? "_" + def.getSubtype() : "")
472 + "(ICS ics) throws AssetNotExistException, AssetAccessException {");
473 out.println();
474 out.println(" AssetAccessTemplate aat = new AssetAccessTemplate(ics);");
475 out.println(" TemplateAsset asset = aat.readAsset(ics.GetVar(\"c\"), ics.GetVar(\"cid\"), new TemplateAssetMapper());");
476 out.println("");
477 printAttributes(def.getAttributeDefs());
478 out.println("}");
479
480 }
481
482 }
483
484 static public class GsfJspGen implements Generator {
485 PrintWriter out;
486
487 public GsfJspGen(Writer writer) {
488 out = new PrintWriter(writer);
489 }
490
491 String getAttributeNames(List<AttributeDef> list) {
492 StringBuilder b = new StringBuilder();
493 for (AttributeDef def : list) {
494 if (b.length() > 0)
495 b.append(",");
496 b.append(def.getName());
497 }
498 return b.toString();
499
500 }
501
502 public void printAssetTypeDef(AssetTypeDef def) {
503 out.println("<%@ taglib prefix=\"cs\" uri=\"futuretense_cs/ftcs1_0.tld\"");
504 out.println("%><%@ taglib uri=\"http://java.sun.com/jsp/jstl/core\" prefix=\"c\"");
505 out.println("%><%@ taglib uri=\"http://gst.fatwire.com/foundation/tags/gsf\" prefix=\"gsf\"");
506 out.println("%><cs:ftcs><gsf:root>");
507
508 out.println("<gsf:asset-load name=\"asset\" attributes=\"" + getAttributeNames(def.getAttributeDefs())
509 + "\" />");
510
511 printAttributes(def.getAttributeDefs());
512 out.println("%></gsf:root></cs:ftcs>");
513 }
514
515 private void printAttributes(List<AttributeDef> list) {
516 for (AttributeDef def : list) {
517 if (def.getType() == AttributeTypeEnum.BLOB || def.getType() == AttributeTypeEnum.URL) {
518
519
520
521
522 } else {
523 out.println(def.getName() + ": ${asset." + def.getName() + "} <br/>");
524 }
525
526 }
527
528 }
529 }
530
531 void gen(ICS ics, JspWriter out) throws AssetAccessException, IOException {
532
533 Session s = SessionFactory.getSession(ics);
534 AssetTypeDefManager mgr = (AssetTypeDefManager) s.getManager(com.fatwire.assetapi.def.AssetTypeDefManager.class
535 .getName());
536
537 String ats = ics.GetVar("assettype");
538 Generator generator = null;
539 String what = ics.GetVar("what");
540 if ("assetapicode".equals(what)) {
541 generator = new AssetApiGen(out);
542 } else if ("def".equals(what)) {
543 generator = new DefPrint(out);
544 } else if ("gsfjspcode".equals(what)) {
545 generator = new GsfJspGen(out);
546 } else if ("gsfjavacode".equals(what)) {
547 generator = new GsfJavaGen(out);
548 }
549
550 if (ats != null && generator != null) {
551 for (String at : ats.split(";")) {
552 String[] p = at.split(":");
553 String a = p[0];
554 String st = p.length == 2 ? p[1] : null;
555 try {
556 out.write("<pre>");
557 AssetTypeDef def = mgr.findByName(a, st);
558 if (def != null) {
559 generator.printAssetTypeDef(def);
560 }
561 } catch (Exception e) {
562 out.println(e.getMessage() + " on " + a + "/" + st);
563 e.printStackTrace(new PrintWriter(out));
564 }
565 out.write("</pre>");
566
567 }
568
569 }
570
571 for (String assetType : mgr.getAssetTypes()) {
572 for (String subtype : mgr.getSubTypes(assetType)) {
573
574
575
576 }
577 try {
578 AssetTypeDef def = mgr.findByName(assetType, null);
579 if (!def.getProperties().getIsFlexAsset()) {
580
581
582 }
583 } catch (Exception e) {
584 out.println(e.getMessage());
585 DebugHelper.printStackTrace(out, e);
586 }
587
588 }
589 }
590
591 }