summaryrefslogtreecommitdiff
path: root/src/main/java/org/cristalise/kernel/process/module/Module.java
blob: d78e7b207ce306ffa721048b47083b634d85eab3 (plain)
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
/**
 * This file is part of the CRISTAL-iSE kernel.
 * Copyright (c) 2001-2014 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.process.module;

import java.util.ArrayList;
import java.util.Properties;

import org.cristalise.kernel.common.ObjectNotFoundException;
import org.cristalise.kernel.entity.imports.ImportAgent;
import org.cristalise.kernel.entity.imports.ImportDependency;
import org.cristalise.kernel.entity.imports.ImportDependencyMember;
import org.cristalise.kernel.entity.imports.ImportItem;
import org.cristalise.kernel.entity.imports.ImportOutcome;
import org.cristalise.kernel.entity.imports.ImportRole;
import org.cristalise.kernel.entity.proxy.AgentProxy;
import org.cristalise.kernel.entity.proxy.ItemProxy;
import org.cristalise.kernel.lookup.DomainPath;
import org.cristalise.kernel.lookup.RolePath;
import org.cristalise.kernel.process.Gateway;
import org.cristalise.kernel.property.Property;
import org.cristalise.kernel.scripting.ErrorInfo;
import org.cristalise.kernel.scripting.ScriptingEngineException;
import org.cristalise.kernel.utils.Logger;


public class Module extends ImportItem {

	private ModuleInfo info;
	private String resURL;
	private ModuleImports imports = new ModuleImports();
	private ArrayList<ModuleConfig> config = new ArrayList<ModuleConfig>();
	private ArrayList<ModuleScript> scripts = new ArrayList<ModuleScript>();
    
	public Module() {
		super();
		// Module properties
		properties.add(new Property("Type", "Module", false));
		setInitialPath("/desc/modules/");
		setWorkflow("NoWorkflow");
		setWorkflowVer(0);
		imports.list.add(this);
	}
	
	public void runScript(String event, AgentProxy user, boolean isServer) throws ScriptingEngineException {
		for (ModuleScript script : scripts) {
			if (script.shouldRun(event, isServer)) {
				Logger.msg("Running "+script.event+" "+script.target+" script from "+name);
				Object result = script.getScript(ns, user).execute();
				if (result instanceof ErrorInfo) {
					ErrorInfo error = (ErrorInfo) result;
					Logger.error(error.toString());
					if (error.getFatal())
						throw new ScriptingEngineException("Fatal Script Error");
				}
				else if (result != null)
					Logger.msg(result.toString());
			}
		}
	}
	
	public void setModuleXML(String moduleXML) {
		ImportOutcome moduleOutcome = new ImportOutcome("Module", 0, "last", null);
		moduleOutcome.data = moduleXML;
		outcomes.add(moduleOutcome);
	}
	
	@Override
	public void setNamespace(String ns) {
		super.setNamespace(ns);
		replaceProp(new Property("Namespace", ns, false));
	}

	@Override
	public void setName(String name) {
		super.setName(name);
		replaceProp(new Property("Name", name, false));
	}

	private void replaceProp(Property newProp) {
		for (Property prop : properties) {
			if (prop.getName().equals("Namespace")) {
				prop.setMutable(newProp.isMutable());
				prop.setValue(newProp.getValue());
				return;
			}
		}
		properties.add(newProp);
	}
	public void importAll(ItemProxy serverEntity, AgentProxy systemAgent, String moduleXML, boolean reset) throws Exception {
		setModuleXML(moduleXML);
		
		for (ModuleResource thisRes : imports.getResources()) {
			try {
				thisRes.setNamespace(ns);
				thisRes.create(systemAgent.getPath(), reset);
			} catch (Exception ex) {
				Logger.error(ex);
				Logger.die("Error importing module resources. Unsafe to continue.");
			}
		}

		for (ImportRole thisRole : imports.getRoles()) {
			RolePath rolePath;
			try {
				String roleName = thisRole.name;
				if (roleName.indexOf('/') > -1) roleName = roleName.substring(roleName.indexOf('/')+1);
				rolePath = Gateway.getLookup().getRolePath(roleName);
				if (rolePath.hasJobList() != thisRole.hasJobList()) {
					Logger.msg("Module.importAll() - Role '"+thisRole.name+"' has incorrect joblist settings. Correcting.");
					rolePath.setHasJobList(thisRole.hasJobList());
					Gateway.getLookupManager().createRole(rolePath);
				}
			} catch (ObjectNotFoundException ex) {
				Logger.msg("Module.importAll() - Role '"+thisRole.name+"' not found. Creating.");
				thisRole.create(systemAgent.getPath(), reset);
			}
		}
		
		for (ImportAgent thisAgent : imports.getAgents()) {
			try {
				Gateway.getLookup().getAgentPath(thisAgent.name);
			    Logger.msg(3, "Module.importAll() - User '"+thisAgent.name+"' found.");
			    continue;
			 } catch (ObjectNotFoundException ex) { }
			 Logger.msg("Module.importAll() - User '"+thisAgent.name+"' not found. Creating.");
			thisAgent.create(systemAgent.getPath(), reset);
		}
		
		for (ImportItem thisItem : imports.getItems()) {
			thisItem.setNamespace(ns);
			thisItem.create(systemAgent.getPath(), reset);
		}
		
		// Import the module item
		this.create(systemAgent.getPath(), reset);
		
	}
	
	public Properties getProperties(boolean isServer) {
		Properties props = new Properties();
		for (ModuleConfig thisProp : config) {
			if (thisProp.include(isServer))
				props.put(thisProp.name, thisProp.value);
		}
		return props;
	}

	public ArrayList<ModuleScript> getScripts() {
		return scripts;
	}

	public void setResURL(String resURL) {
		this.resURL = resURL;
	}
	public String getDesc() {
		return info.desc;
	}
	public String getVersion() {
		return info.version;
	}
	public String getResURL() {
		return resURL;
	}
	public ArrayList<String> getDependencies() {
		return info.dependency;
	}
	public boolean hasDependency(String dep) {
		return info.dependency.contains(dep);
	}

	public ModuleInfo getInfo() {
		return info;
	}

	public void setInfo(ModuleInfo info) {
		this.info = info;
		replaceProp(new Property("Version", info.version, true));
	}

	public ModuleImports getImports() {
		return imports;
	}
	
	public void setImports(ModuleImports imp) {
		// Add dependency for all children
		imports = imp;
		ImportDependency children = new ImportDependency("Contents");
		for (ModuleImport thisImport : imports.list) {
			DomainPath path = thisImport.domainPath;
			if (path != null)
			children.dependencyMemberList.add(new ImportDependencyMember(path.toString()));
		}
		dependencyList.add(children);
	}
	
	public void setConfig(ArrayList<ModuleConfig> config) {
		this.config = config;
	}
	
	public void setScripts(ArrayList<ModuleScript> scripts) {
		this.scripts = scripts;
	}

	public ArrayList<ModuleConfig> getConfig() {
		return config;
	}
	
	
}