1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.efaps.db.print.value;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import org.efaps.admin.program.esjp.EFapsClassLoader;
27 import org.efaps.db.Instance;
28 import org.efaps.db.print.OneSelect;
29 import org.efaps.eql.IEsjpSelect;
30 import org.efaps.util.EFapsException;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34
35
36
37
38
39
40
41 public class EsjpValueSelect
42 extends InstanceValueSelect
43 {
44
45
46
47 private static final Logger LOG = LoggerFactory.getLogger(EsjpValueSelect.class);
48
49
50
51
52 private String className;
53
54
55
56
57 private IEsjpSelect esjp;
58
59
60
61
62 private final List<String> parameters = new ArrayList<>();
63
64
65
66
67 public EsjpValueSelect(final OneSelect _oneSelect,
68 final String _esjp)
69 {
70 super(_oneSelect);
71 LOG.debug("instanciated EsjpValueSelect with: '{}'", _esjp);
72 final String[] paraAr = _esjp.split("(?<!\\\\),");
73 if (paraAr == null || paraAr.length == 0) {
74 LOG.error("Invalid esjp Value select: '{]'", _esjp);
75 } else {
76 this.className = paraAr[0];
77 for (int i = 1; i < paraAr.length; i++) {
78 final String string = paraAr[i];
79 if (string.startsWith("\"")) {
80 this.parameters.add(string.substring(1, string.length() - 2));
81 } else {
82 this.parameters.add(string);
83 }
84 };
85 }
86 }
87
88
89
90
91
92
93
94
95 @Override
96 public Object getValue(final Object _object)
97 throws EFapsException
98 {
99 final Instance inst = (Instance) super.getValue(_object);
100 if (this.esjp == null) {
101 try {
102 final Class<?> clazz = Class.forName(this.className, false, EFapsClassLoader.getInstance());
103 this.esjp = (IEsjpSelect) clazz.newInstance();
104 final List<Instance> instances = new ArrayList<>();
105 for (final Object obj : getOneSelect().getObjectList()) {
106 instances.add((Instance) super.getValue(obj));
107 }
108 if (this.parameters.isEmpty()) {
109 this.esjp.initialize(instances);
110 } else {
111 this.esjp.initialize(instances, this.parameters.toArray(new String[this.parameters.size()]));
112 }
113 } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
114
115 e.printStackTrace();
116 }
117 }
118 return this.esjp.getValue(inst);
119 }
120
121
122
123
124
125 @Override
126 public String getValueType()
127 {
128 return "esjp";
129 }
130
131 }