/
ImportItem.java
322 lines (277 loc) · 12.3 KB
/
ImportItem.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/**
* This file is part of the CRISTAL-iSE kernel.
* Copyright (c) 2001-2015 The CRISTAL Consortium. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*
* http://www.fsf.org/licensing/licenses/lgpl.html
*/
package org.cristalise.kernel.entity.imports;
import static org.cristalise.kernel.property.BuiltInItemProperties.CREATOR;
import static org.cristalise.kernel.property.BuiltInItemProperties.NAME;
import static org.cristalise.kernel.security.BuiltInAuthc.ADMIN_ROLE;
import java.util.ArrayList;
import org.apache.commons.lang3.StringUtils;
import org.cristalise.kernel.collection.Aggregation;
import org.cristalise.kernel.collection.CollectionArrayList;
import org.cristalise.kernel.collection.Dependency;
import org.cristalise.kernel.common.CannotManageException;
import org.cristalise.kernel.common.InvalidCollectionModification;
import org.cristalise.kernel.common.InvalidDataException;
import org.cristalise.kernel.common.ObjectAlreadyExistsException;
import org.cristalise.kernel.common.ObjectCannotBeUpdated;
import org.cristalise.kernel.common.ObjectNotFoundException;
import org.cristalise.kernel.common.PersistencyException;
import org.cristalise.kernel.entity.TraceableEntity;
import org.cristalise.kernel.events.Event;
import org.cristalise.kernel.events.History;
import org.cristalise.kernel.lifecycle.CompositeActivityDef;
import org.cristalise.kernel.lifecycle.instance.CompositeActivity;
import org.cristalise.kernel.lifecycle.instance.Workflow;
import org.cristalise.kernel.lifecycle.instance.predefined.PredefinedStep;
import org.cristalise.kernel.lookup.AgentPath;
import org.cristalise.kernel.lookup.DomainPath;
import org.cristalise.kernel.lookup.ItemPath;
import org.cristalise.kernel.lookup.Path;
import org.cristalise.kernel.persistency.ClusterType;
import org.cristalise.kernel.persistency.outcome.Outcome;
import org.cristalise.kernel.persistency.outcome.Schema;
import org.cristalise.kernel.persistency.outcome.Viewpoint;
import org.cristalise.kernel.process.Gateway;
import org.cristalise.kernel.process.module.ModuleImport;
import org.cristalise.kernel.property.Property;
import org.cristalise.kernel.property.PropertyArrayList;
import org.cristalise.kernel.utils.LocalObjectLoader;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
/**
* Complete Structure for new Item created by different bootstrap uses cases including testing
*/
@Getter @Setter @Slf4j
public class ImportItem extends ModuleImport {
protected String initialPath;
protected String workflow;
protected Integer workflowVer;
protected ArrayList<Property> properties = new ArrayList<Property>();
protected ArrayList<ImportAggregation> aggregationList = new ArrayList<ImportAggregation>();
protected ArrayList<ImportDependency> dependencyList = new ArrayList<ImportDependency>();
protected ArrayList<ImportOutcome> outcomes = new ArrayList<ImportOutcome>();
/**
* Alternative way to provide workflow definition.
* It is not marshallable by castor, therefore cannot be set in module.xml
*/
protected CompositeActivityDef compActDef;
/**
* Alternative way to provide workflow.
* It is not marshallable by castor, therefore cannot be set in module.xml
*/
protected Workflow wf;
public ImportItem() {}
public ImportItem(String ns, String name, String initialPath, ItemPath itemPath, String wf, int wfVer) {
setNamespace(ns);
setName(name);
setItemPath(itemPath);
setInitialPath(initialPath);
setWorkflow(wf);
setWorkflowVer(wfVer);
compActDef = null;
wf = null;
}
/**
* Try to find ItemPath if already exists. If not create new one.
*/
@Override
public ItemPath getItemPath() {
if (itemPath == null) {
DomainPath existingItem = new DomainPath(initialPath + "/" + name);
if (existingItem.exists()) {
try {
itemPath = existingItem.getItemPath();
}
catch (ObjectNotFoundException ex) {}
}
}
if (itemPath == null) itemPath = new ItemPath();
return itemPath;
}
@Override
public void setNamespace(String ns) {
super.setNamespace(ns);
if (initialPath == null) initialPath = "/desc/" + ns;
}
@Override
public void setName(String name) {
super.setName(name);
}
/**
*
* @return
* @throws ObjectNotFoundException
* @throws CannotManageException
* @throws ObjectAlreadyExistsException
* @throws ObjectCannotBeUpdated
*/
private TraceableEntity getTraceableEntitiy()
throws ObjectNotFoundException, CannotManageException, ObjectAlreadyExistsException, ObjectCannotBeUpdated
{
TraceableEntity newItem;
ItemPath ip = getItemPath();
if (ip.exists()) {
log.info("getTraceableEntitiy() - Verifying module item "+domainPath+" at "+ip);
newItem = Gateway.getCorbaServer().getItem(getItemPath());
isNewItem = false;
}
else {
log.info("getTraceableEntitiy() - Creating module item "+ip+" at "+domainPath);
newItem = Gateway.getCorbaServer().createItem(ip);
Gateway.getLookupManager().add(ip);
}
return newItem;
}
/**
*
*/
@Override
public Path create(AgentPath agentPath, boolean reset)
throws InvalidDataException, ObjectCannotBeUpdated, ObjectNotFoundException,
CannotManageException, ObjectAlreadyExistsException, InvalidCollectionModification, PersistencyException
{
domainPath = new DomainPath(new DomainPath(initialPath), name);
log.info("create() - path:{}", domainPath);
if (domainPath.exists()) {
ItemPath domItem = domainPath.getItemPath();
if (!getItemPath().equals(domItem)) {
throw new CannotManageException("Item "+domainPath+" was found with the wrong itemPath ("+domainPath.getItemPath()+" vs "+getItemPath()+")");
}
}
else isDOMPathExists = false;
TraceableEntity newItem = getTraceableEntitiy();
// (re)initialise the new item with properties, workflow and collections
try {
newItem.initialise(
agentPath.getSystemKey(),
Gateway.getMarshaller().marshall(createItemProperties()),
Gateway.getMarshaller().marshall(createCompositeActivity()),
Gateway.getMarshaller().marshall(createCollections()),
"", ""
);
}
catch (Exception ex) {
log.error("Error initialising new item " + ns + "/" + name, ex);
if (isNewItem) Gateway.getLookupManager().delete(itemPath);
throw new CannotManageException("Problem initialising new item. See server log:" + ex.getMessage());
}
History hist = new History(getItemPath(), null);
// import outcomes
for (ImportOutcome thisOutcome : outcomes) {
String outcomeData = thisOutcome.getData(ns);
// load schema and state machine
Schema schema = LocalObjectLoader.getSchema(thisOutcome.schema, thisOutcome.version);
// parse new outcome and validate
Outcome newOutcome = new Outcome(-1, outcomeData, schema);
newOutcome.validateAndCheck();
Viewpoint impView;
try {
impView = (Viewpoint) Gateway.getStorage().get(getItemPath(), ClusterType.VIEWPOINT + "/" + thisOutcome.schema + "/" + thisOutcome.viewname, null);
if (newOutcome.isIdentical(impView.getOutcome())) {
log.debug("create() - View "+thisOutcome.schema+"/"+thisOutcome.viewname+" in "+ns+"/"+name+" identical, no update required");
continue;
}
else {
log.info("create() - Difference found in view "+thisOutcome.schema+"/"+thisOutcome.viewname+" in "+ns+"/"+name);
if (!reset && !impView.getEvent().getStepPath().equals("Import")) {
log.info("create() - Last edit was not done by import, and reset not requested. Not overwriting.");
continue;
}
}
}
catch (ObjectNotFoundException ex) {
log.info("create() - View "+thisOutcome.schema+"/"+thisOutcome.viewname+" not found in "+ns+"/"+name+". Creating.");
impView = new Viewpoint(getItemPath(), schema, thisOutcome.viewname, -1);
}
// write new view/outcome/event
Event newEvent = hist.addEvent(
agentPath, null, ADMIN_ROLE.getName(), "Import", "Import", "Import", schema,
LocalObjectLoader.getStateMachine("PredefinedStep", 0), PredefinedStep.DONE, thisOutcome.viewname);
newOutcome.setID(newEvent.getID());
impView.setEventId(newEvent.getID());
Gateway.getStorage().put(getItemPath(), newOutcome, null);
Gateway.getStorage().put(getItemPath(), impView, null);
}
// register domain path (before collections in case of recursive collections)
if (!isDOMPathExists) {
domainPath.setItemPath(getItemPath());
Gateway.getLookupManager().add(domainPath);
}
return domainPath;
}
/**
*
* @return
*/
protected PropertyArrayList createItemProperties() {
properties.add(new Property(NAME, name, true));
properties.add(new Property(CREATOR, "bootstrap", true));
return new PropertyArrayList(properties);
}
/**
* This method enables to use ImportItem in different bootstrap uses cases (e.g. testing)
*
* @return the the domain workflow of the Item
* @throws ObjectNotFoundException
* @throws InvalidDataException
*/
protected CompositeActivity createCompositeActivity() throws ObjectNotFoundException, InvalidDataException {
if (wf != null) {
return (CompositeActivity) wf.search("workflow/domain");
}
else {
if (compActDef == null) {
// default workflow version is 0 if not given
if (StringUtils.isNotBlank(workflow)) {
compActDef = (CompositeActivityDef) LocalObjectLoader.getActDef(workflow, workflowVer == null ? 0 : workflowVer);
}
else {
log.warn("createCompositeActivity() - NO Workflow was set for domainPath:"+domainPath);
compActDef = (CompositeActivityDef) LocalObjectLoader.getActDef("NoWorkflow", workflowVer == null ? 0 : workflowVer);
}
}
}
return (CompositeActivity) compActDef.instantiate();
}
/**
*
* @return
* @throws InvalidCollectionModification
* @throws ObjectNotFoundException
* @throws ObjectAlreadyExistsException
*/
protected CollectionArrayList createCollections()
throws InvalidCollectionModification, ObjectNotFoundException, ObjectAlreadyExistsException
{
CollectionArrayList colls = new CollectionArrayList();
for (ImportDependency element : dependencyList) {
Dependency newDep = element.create();
colls.put(newDep);
}
for (ImportAggregation element : aggregationList) {
Aggregation newAgg = element.create();
colls.put(newAgg);
}
log.info("createCollections() - name:{} number of colls:{}", name, colls.list.size());
return colls;
}
}