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.common;
22
23 import java.net.URL;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28 import java.util.UUID;
29
30 import org.efaps.admin.user.Company;
31 import org.efaps.ci.CIAdminCommon;
32 import org.efaps.db.Insert;
33 import org.efaps.db.Instance;
34 import org.efaps.db.InstanceQuery;
35 import org.efaps.db.QueryBuilder;
36 import org.efaps.db.Update;
37 import org.efaps.update.AbstractUpdate;
38 import org.efaps.update.UpdateLifecycle;
39 import org.efaps.update.util.InstallationException;
40 import org.efaps.util.EFapsException;
41
42
43
44
45
46
47
48
49 public class SystemConfigurationUpdate
50 extends AbstractUpdate
51 {
52
53
54
55
56
57
58 public SystemConfigurationUpdate(final URL _url)
59 {
60 super(_url, "Admin_Common_SystemConfiguration");
61 }
62
63
64
65
66
67
68
69 @Override
70 protected AbstractDefinition newDefinition()
71 {
72 return new Definition();
73 }
74
75
76
77
78
79 public class AttributeDefinition
80 extends AbstractDefinition
81 {
82
83
84
85 private String key;
86
87
88
89
90 private String value;
91
92
93
94
95 private String description;
96
97
98
99
100 private String companyUUID;
101
102
103
104
105
106
107
108
109 @Override
110 protected void readXML(final List<String> _tags,
111 final Map<String, String> _attributes,
112 final String _text)
113 throws EFapsException
114 {
115 final String tmpValue = _tags.get(0);
116 if ("key".equals(tmpValue)) {
117 this.key = _text;
118 } else if ("value".equals(tmpValue)) {
119 this.value = _text;
120 } else if ("description".equals(tmpValue)) {
121 this.description = _text;
122 } else if ("company".equals(tmpValue)) {
123 this.companyUUID = _text;
124 } else {
125 super.readXML(_tags, _attributes, _text);
126 }
127 }
128
129
130
131
132
133 public void updateInDB(final Instance _instance)
134 throws EFapsException
135 {
136 Company company = null;
137 if (this.companyUUID != null) {
138 company = Company.get(UUID.fromString(this.companyUUID));
139 }
140 final QueryBuilder queryBldr = new QueryBuilder(CIAdminCommon.SystemConfigurationAttribute);
141 queryBldr.addWhereAttrEqValue(CIAdminCommon.SystemConfigurationAttribute.Key, this.key);
142 queryBldr.addWhereAttrEqValue(CIAdminCommon.SystemConfigurationAttribute.AbstractLink, _instance);
143 if (company == null) {
144 queryBldr.addWhereAttrEqValue(CIAdminCommon.SystemConfigurationAttribute.CompanyLink, 0);
145 } else {
146 queryBldr.addWhereAttrEqValue(CIAdminCommon.SystemConfigurationAttribute.CompanyLink, company.getId());
147 }
148 final InstanceQuery query = queryBldr.getQuery();
149 query.executeWithoutAccessCheck();
150 Update update = null;
151 if (query.next()) {
152 update = new Update(query.getCurrentValue());
153 } else {
154 update = new Insert(CIAdminCommon.SystemConfigurationAttribute);
155 update.add(CIAdminCommon.SystemConfigurationAttribute.AbstractLink, _instance.getId());
156 update.add(CIAdminCommon.SystemConfigurationAttribute.Key, this.key);
157 }
158 if (company != null) {
159 update.add(CIAdminCommon.SystemConfigurationAttribute.CompanyLink, company.getId());
160 } else {
161 update.add(CIAdminCommon.SystemConfigurationAttribute.CompanyLink, 0);
162 }
163 update.add(CIAdminCommon.SystemConfigurationAttribute.Value, this.value);
164 update.add(CIAdminCommon.SystemConfigurationAttribute.Description, this.description);
165 update.executeWithoutAccessCheck();
166 }
167 }
168
169
170
171
172
173 public class Definition
174 extends AbstractDefinition
175 {
176
177
178
179
180
181 private AttributeDefinition curAttr;
182
183
184
185
186
187
188 private final List<SystemConfigurationUpdate.AttributeDefinition> attributes
189 = new ArrayList<SystemConfigurationUpdate.AttributeDefinition>();
190
191
192
193
194
195
196
197
198 @Override
199 protected void readXML(final List<String> _tags,
200 final Map<String, String> _attributes,
201 final String _text)
202 throws EFapsException
203 {
204 final String value = _tags.get(0);
205 if ("attribute".equals(value)) {
206 if (_tags.size() == 1) {
207 this.curAttr = new AttributeDefinition();
208 this.attributes.add(this.curAttr);
209 } else {
210 this.curAttr.readXML(_tags.subList(1, _tags.size()), _attributes, _text);
211 }
212 } else {
213 super.readXML(_tags, _attributes, _text);
214 }
215 }
216
217
218
219
220
221
222
223
224
225
226
227 @Override
228 public void updateInDB(final UpdateLifecycle _step,
229 final Set<Link> _allLinkTypes)
230 throws InstallationException
231 {
232 super.updateInDB(_step, _allLinkTypes);
233 try {
234 if (_step == UpdateLifecycle.EFAPS_UPDATE) {
235 for (final AttributeDefinition attr : this.attributes) {
236 attr.updateInDB(getInstance());
237 }
238 }
239 } catch (final EFapsException e) {
240 throw new InstallationException(" Type can not be updated", e);
241 }
242 }
243 }
244 }