001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2007
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.war.beans.admin.main;
034:
035: import com.flexive.shared.CacheAdmin;
036: import com.flexive.shared.EJBLookup;
037: import com.flexive.shared.FxContext;
038: import com.flexive.shared.security.UserTicket;
039: import com.flexive.shared.security.Role;
040: import com.flexive.shared.exceptions.FxLoadException;
041: import com.flexive.shared.exceptions.FxApplicationException;
042: import com.flexive.shared.interfaces.ScriptingEngine;
043: import com.flexive.shared.scripting.*;
044: import com.flexive.faces.FxJsfUtils;
045: import com.flexive.faces.beans.MessageBean;
046: import com.flexive.faces.messages.FxFacesMsgErr;
047: import com.flexive.faces.messages.FxFacesMsgInfo;
048:
049: import javax.faces.context.FacesContext;
050: import javax.faces.event.ActionEvent;
051: import javax.faces.model.SelectItem;
052: import java.util.*;
053: import java.sql.SQLException;
054:
055: /**
056: * JSF scripting bean
057: *
058: * @author Johannes Wernig-Pichler (johannes.wernig-pichler@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
059: * @version $Rev: 203 $
060: */
061: public class ScriptBean {
062:
063: private long id = -1;
064: private String name;
065: private String desc;
066: private String code;
067: private boolean active;
068: private FxScriptInfoEdit sinfo;
069: private ScriptingEngine scriptInterface;
070: private FxScriptMapping mapping;
071: private Map<Long, String> typeMappingNames;
072: private Map<Long, String> assignmentMappingNames;
073: private FxScriptScope selectedScope = null;
074: private long selectedScriptEventId = -1;
075:
076: private static final String ID_CACHE_KEY = ScriptBean.class + "_id";
077:
078: // constructor
079: public ScriptBean() {
080: this .scriptInterface = EJBLookup.getScriptingEngine();
081: this .sinfo = new FxScriptInfo().asEditable();
082: }
083:
084: public FxScriptScope getSelectedScope() {
085: if (selectedScope == null)
086: selectedScope = FxScriptScope.All;
087: return selectedScope;
088: }
089:
090: public List<SelectItem> getEventsForScope() {
091: //if the selected event is not in the list of events for the currently selected scope
092: //reset the selected event id to default -1==all events
093: boolean selectedEventFound = false;
094: FxScriptScope scope = getSelectedScope();
095: List<SelectItem> eventsForScope = new ArrayList<SelectItem>();
096: eventsForScope.add(new SelectItem(-1, MessageBean.getInstance()
097: .getMessage("Script.selectItem.allEvents")));
098: for (FxScriptEvent e : FxScriptEvent.values()) {
099: if (e.getScope().compareTo(scope) == 0
100: || scope.compareTo(FxScriptScope.All) == 0) {
101: eventsForScope.add(new SelectItem(e.getId(), e
102: .getName()));
103: if (getSelectedScriptEventId() == e.getId())
104: selectedEventFound = true;
105: }
106: }
107:
108: if (!selectedEventFound)
109: this .selectedScriptEventId = -1;
110:
111: return eventsForScope;
112: }
113:
114: public void setSelectedScope(FxScriptScope selectedScope) {
115: this .selectedScope = selectedScope;
116: }
117:
118: public long getSelectedScriptEventId() {
119: return selectedScriptEventId;
120: }
121:
122: public void setSelectedScriptEventId(long selectedScriptEventId) {
123: this .selectedScriptEventId = selectedScriptEventId;
124: }
125:
126: public List<FxScriptInfo> getScriptsForEvent() {
127: long eventId = getSelectedScriptEventId();
128: List<FxScriptInfo> scriptsForEvent = new ArrayList<FxScriptInfo>();
129: for (FxScriptInfo s : CacheAdmin.getFilteredEnvironment()
130: .getScripts())
131: if ((eventId == -1 && getSelectedScope().compareTo(
132: FxScriptScope.All) == 0)
133: || (eventId == -1 && s.getEvent().getScope()
134: .compareTo(getSelectedScope()) == 0)
135: || (s.getEvent().getId() == eventId && getSelectedScope()
136: .compareTo(FxScriptScope.All) == 0)
137: || (s.getEvent().getId() == eventId && s.getEvent()
138: .getScope().compareTo(getSelectedScope()) == 0))
139: scriptsForEvent.add(s);
140: return scriptsForEvent;
141: }
142:
143: public long getId() {
144: return id;
145: }
146:
147: public void setId(long id) {
148: this .id = id;
149: }
150:
151: public boolean isActive() {
152: return active;
153: }
154:
155: public void setActive(boolean active) {
156: this .active = active;
157: }
158:
159: /**
160: * Loads the script specified by the parameter id.
161: *
162: * @return the next page to render
163: */
164:
165: public String editScript() {
166: ensureScriptIdSet();
167: setSinfo(CacheAdmin.getEnvironment().getScript(id).asEditable());
168: try {
169: this .mapping = scriptInterface.loadScriptMapping(null, id);
170: this .typeMappingNames = new HashMap<Long, String>();
171: // we need the type names for the user interface and the type ids for the links
172: for (FxScriptMappingEntry entry : this .mapping
173: .getMappedTypes()) {
174: this .typeMappingNames.put(entry.getId(), CacheAdmin
175: .getEnvironment().getType(entry.getId())
176: .getName());
177: for (long id : entry.getDerivedIds())
178: this .typeMappingNames.put(id, CacheAdmin
179: .getEnvironment().getType(id).getName());
180: }
181: this .assignmentMappingNames = new HashMap<Long, String>();
182: for (FxScriptMappingEntry entry : this .mapping
183: .getMappedAssignments()) {
184: this .assignmentMappingNames.put(entry.getId(),
185: CacheAdmin.getEnvironment().getAssignment(
186: entry.getId()).getXPath());
187: for (long id : entry.getDerivedIds())
188: this .assignmentMappingNames.put(id, CacheAdmin
189: .getEnvironment().getAssignment(id)
190: .getXPath());
191: }
192: } catch (FxLoadException e) {
193: new FxFacesMsgErr("Script.err.loadMap").addToContext();
194: } catch (SQLException e) {
195: new FxFacesMsgErr("Script.err.loadMap").addToContext();
196: }
197: setName(sinfo.getName());
198: setDesc(sinfo.getDescription());
199: setCode(sinfo.getCode());
200: setActive(sinfo.isActive());
201: return "scriptEdit";
202: }
203:
204: public FxScriptInfo getSinfo() {
205: return sinfo;
206: }
207:
208: public void setSinfo(FxScriptInfoEdit sinfo) {
209: this .sinfo = sinfo;
210: }
211:
212: public String getName() {
213: return name;
214: }
215:
216: public void setName(String name) {
217: this .name = name;
218: }
219:
220: public String getDesc() {
221: return desc;
222: }
223:
224: public void setDesc(String desc) {
225: this .desc = desc;
226: }
227:
228: public String getCode() {
229: return code;
230: }
231:
232: public void setCode(String code) {
233: this .code = code;
234: }
235:
236: private void ensureScriptIdSet() {
237: if (this .id <= 0) {
238: this .id = (Long) FxJsfUtils
239: .getSessionAttribute(ID_CACHE_KEY);
240: }
241: }
242:
243: /**
244: * Deletes a script, with the id specified by id.
245: *
246: * @return the next pageto render
247: */
248: public String deleteScript() {
249:
250: final UserTicket ticket = FxContext.get().getTicket();
251: if (!ticket.isInRole(Role.ScriptManagement)) {
252: new FxFacesMsgErr("Script.err.deletePerm").addToContext();
253: return "scriptOverview";
254: }
255:
256: ensureScriptIdSet();
257: try {
258: scriptInterface.removeScript(id);
259: // display updated script list -->handled via a4j now
260: //updateScriptList(); -->handled via a4j now
261: new FxFacesMsgInfo("Script.nfo.deleted").addToContext();
262: } catch (FxApplicationException e) {
263: new FxFacesMsgErr("Script.err.delete").addToContext();
264: }
265: return "scriptOverview";
266: }
267:
268: /**
269: * Executes a script, with the id specified by id.
270: *
271: * @return the next page to render
272: */
273: public String runScript() {
274:
275: final UserTicket ticket = FxContext.get().getTicket();
276: if (!ticket.isInRole(Role.ScriptExecution)) {
277: new FxFacesMsgErr("Script.err.runPerm").addToContext();
278: return "scriptOverview";
279: }
280:
281: ensureScriptIdSet();
282: try {
283: scriptInterface.runScript(id);
284: new FxFacesMsgInfo("Script.nfo.executed", CacheAdmin
285: .getEnvironment().getScript(id).getName())
286: .addToContext();
287: } catch (FxApplicationException e) {
288: new FxFacesMsgErr("Script.err.run").addToContext();
289: }
290: return "scriptOverview";
291: }
292:
293: /**
294: * Creates a new script from the beans data.
295: *
296: * @return the next jsf page to render
297: */
298: public String createScript() {
299: final UserTicket ticket = FxContext.get().getTicket();
300: if (!ticket.isInRole(Role.ScriptManagement)) {
301: new FxFacesMsgErr("Script.err.createPerm").addToContext();
302: return "scriptOverview";
303: }
304:
305: if (sinfo.getName().length() < 1 || sinfo.getEvent() == null) {
306: new FxFacesMsgErr("Script.err.createMiss").addToContext();
307: return "scriptCreate";
308: }
309:
310: try {
311: setId(scriptInterface.createScript(sinfo.getEvent(),
312: sinfo.getName(), sinfo.getDescription(),
313: sinfo.getCode()).getId());
314: setSinfo(CacheAdmin.getEnvironment().getScript(id)
315: .asEditable());
316: // display updated script list
317: //updateScriptList();
318: new FxFacesMsgInfo("Script.nfo.created", sinfo.getName())
319: .addToContext();
320: } catch (FxApplicationException e) {
321: new FxFacesMsgErr("Script.err.create").addToContext();
322: }
323: return "scriptOverview";
324: }
325:
326: /**
327: * Saves the edited script
328: *
329: * @return the next page to render
330: */
331: public String saveScript() {
332:
333: final UserTicket ticket = FxContext.get().getTicket();
334: if (!ticket.isInRole(Role.ScriptManagement)) {
335: new FxFacesMsgErr("Script.err.editPerm").addToContext();
336: return "scriptOverview";
337: }
338:
339: ensureScriptIdSet();
340:
341: try {
342: scriptInterface.updateScriptInfo(id, sinfo.getEvent(),
343: sinfo.getName(), sinfo.getDescription(), sinfo
344: .getCode(), sinfo.isActive());
345: //updateScriptList(); needed (see mandators) ???
346: new FxFacesMsgInfo("Script.nfo.updated").addToContext();
347: return "scriptOverview";
348: } catch (FxApplicationException e) {
349: new FxFacesMsgErr("Script.err.save").addToContext();
350: return "scriptOverview";
351: }
352: }
353:
354: public List<Map.Entry<Long, String>> getTypeMappingNames() {
355: ArrayList<Map.Entry<Long, String>> list = new ArrayList<Map.Entry<Long, String>>(
356: this .typeMappingNames.entrySet().size());
357: for (Map.Entry<Long, String> entry : this .typeMappingNames
358: .entrySet()) {
359: list.add(entry);
360: }
361: return list;
362: }
363:
364: public List<Map.Entry<Long, String>> getAssignmentMappingNames() {
365: ArrayList<Map.Entry<Long, String>> list = new ArrayList<Map.Entry<Long, String>>(
366: this .assignmentMappingNames.entrySet().size());
367: for (Map.Entry<Long, String> entry : this .assignmentMappingNames
368: .entrySet()) {
369: list.add(entry);
370: }
371: return list;
372: }
373:
374: public FxScriptMapping getMapping() {
375: return mapping;
376: }
377:
378: /**
379: * called from the structure editor -> get the oid of the script to show from the request parameters
380: *
381: * @param e
382: */
383: public void structureListen(ActionEvent e) {
384: FacesContext context = FacesContext.getCurrentInstance();
385: Map requestParams = context.getExternalContext()
386: .getRequestParameterMap();
387: long oid = -1;
388: if (requestParams.get("oid") != null) {
389: oid = Long.valueOf(requestParams.get("oid").toString());
390: }
391: if (oid != -1) {
392: // this specifies the script to work on...
393: this.id = oid;
394: }
395: }
396:
397: }
|