001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2008
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.ejb.beans;
034:
035: import com.flexive.core.Database;
036: import static com.flexive.core.DatabaseConst.TBL_GROUP;
037: import static com.flexive.core.DatabaseConst.TBL_MANDATORS;
038: import com.flexive.core.LifeCycleInfoImpl;
039: import com.flexive.core.structure.StructureLoader;
040: import com.flexive.shared.CacheAdmin;
041: import com.flexive.shared.FxContext;
042: import com.flexive.shared.FxSharedUtils;
043: import com.flexive.shared.content.FxPermissionUtils;
044: import com.flexive.shared.exceptions.*;
045: import com.flexive.shared.interfaces.*;
046: import com.flexive.shared.security.Mandator;
047: import com.flexive.shared.security.Role;
048: import com.flexive.shared.security.UserTicket;
049: import com.flexive.shared.structure.FxEnvironment;
050: import org.apache.commons.logging.Log;
051: import org.apache.commons.logging.LogFactory;
052:
053: import javax.annotation.Resource;
054: import javax.ejb.*;
055: import java.sql.Connection;
056: import java.sql.PreparedStatement;
057: import java.sql.SQLException;
058:
059: /**
060: * Mandator Engine implementation
061: *
062: * @author Markus Plesser (markus.plesser@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
063: */
064: @Stateless(name="MandatorEngine")
065: @TransactionManagement(TransactionManagementType.CONTAINER)
066: public class MandatorEngineBean implements MandatorEngine,
067: MandatorEngineLocal {
068: // Our logger
069: private static transient Log LOG = LogFactory
070: .getLog(MandatorEngineBean.class);
071:
072: @Resource
073: javax.ejb.SessionContext ctx;
074:
075: @EJB
076: SequencerEngineLocal seq;
077: @EJB
078: UserGroupEngineLocal grp;
079:
080: /**
081: * {@inheritDoc}
082: */
083: @TransactionAttribute(TransactionAttributeType.REQUIRED)
084: public int create(String name, boolean active)
085: throws FxApplicationException {
086: final UserTicket ticket = FxContext.get().getTicket();
087: final FxEnvironment environment;
088: // Security
089: FxPermissionUtils.checkRole(ticket, Role.GlobalSupervisor);
090: FxSharedUtils.checkParameterEmpty(name, "NAME");
091: environment = CacheAdmin.getEnvironment();
092: //exist check
093: for (Mandator m : environment.getMandators(true, true))
094: if (m.getName().equalsIgnoreCase(name))
095: throw new FxEntryExistsException("ex.mandator.exists",
096: name);
097:
098: Connection con = null;
099: PreparedStatement ps = null;
100: String sql;
101:
102: try {
103:
104: // Obtain a database connection
105: con = Database.getDbConnection();
106:
107: // Obtain a new id
108: int newId = (int) seq
109: .getId(SequencerEngine.System.MANDATOR);
110:
111: sql = "INSERT INTO " + TBL_MANDATORS + "("
112: +
113: //1 2 3 4 5 6 7 8
114: "ID,NAME,METADATA,IS_ACTIVE,CREATED_BY,CREATED_AT,MODIFIED_BY,MODIFIED_AT)"
115: + "VALUES (?,?,?,?,?,?,?,?)";
116: final long NOW = System.currentTimeMillis();
117: ps = con.prepareStatement(sql);
118: ps.setInt(1, newId);
119: ps.setString(2, name.trim());
120: ps.setNull(3, java.sql.Types.INTEGER);
121: ps.setBoolean(4, active);
122: ps.setLong(5, ticket.getUserId());
123: ps.setLong(6, NOW);
124: ps.setLong(7, ticket.getUserId());
125: ps.setLong(8, NOW);
126: ps.executeUpdate();
127: ps.close();
128: sql = "INSERT INTO "
129: + TBL_GROUP
130: + " "
131: + "(ID,MANDATOR,AUTOMANDATOR,ISSYSTEM,NAME,COLOR,CREATED_BY,CREATED_AT,MODIFIED_BY,MODIFIED_AT) VALUES ("
132: + "?,?," + newId + ",TRUE,?,?,?,?,?,?)";
133: ps = con.prepareStatement(sql);
134: long gid = seq.getId(SequencerEngine.System.GROUP);
135: ps.setLong(1, gid);
136: ps.setLong(2, newId);
137: ps.setString(3, "Everyone (" + name.trim() + ")");
138: ps.setString(4, "#00AA00");
139: ps.setLong(5, 0);
140: ps.setLong(6, NOW);
141: ps.setLong(7, 0);
142: ps.setLong(8, NOW);
143: ps.executeUpdate();
144: StructureLoader.addMandator(
145: FxContext.get().getDivisionId(), new Mandator(
146: newId, name.trim(), -1, active,
147: new LifeCycleInfoImpl(ticket.getUserId(),
148: NOW, ticket.getUserId(), NOW)));
149: return newId;
150: } catch (SQLException exc) {
151: final boolean uniqueConstraintViolation = Database
152: .isUniqueConstraintViolation(exc);
153: ctx.setRollbackOnly();
154: if (uniqueConstraintViolation) {
155: throw new FxEntryExistsException(LOG,
156: "ex.mandator.exists", name);
157: } else {
158: throw new FxCreateException(LOG, exc,
159: "ex.mandator.createFailed", name, exc
160: .getMessage());
161: }
162: } finally {
163: Database.closeObjects(MandatorEngineBean.class, con, ps);
164: }
165: }
166:
167: /**
168: * {@inheritDoc}
169: */
170: @TransactionAttribute(TransactionAttributeType.REQUIRED)
171: public void assignMetaData(int mandatorId, long contentId)
172: throws FxApplicationException {
173: //TODO: code me!
174: }
175:
176: /**
177: * {@inheritDoc}
178: */
179: @TransactionAttribute(TransactionAttributeType.REQUIRED)
180: public void removeMetaData(int mandatorId)
181: throws FxApplicationException {
182: //TODO: code me!
183: }
184:
185: /**
186: * {@inheritDoc}
187: */
188: @TransactionAttribute(TransactionAttributeType.REQUIRED)
189: public void activate(long mandatorId) throws FxApplicationException {
190: final UserTicket ticket = FxContext.get().getTicket();
191: final FxEnvironment environment;
192: // Security
193: FxPermissionUtils.checkRole(ticket, Role.GlobalSupervisor);
194: environment = CacheAdmin.getEnvironment();
195: //exist check
196: Mandator mand = environment.getMandator(mandatorId);
197: if (mand.isActive())
198: return; //silently ignore
199: Connection con = null;
200: PreparedStatement ps = null;
201: String sql;
202:
203: try {
204:
205: // Obtain a database connection
206: con = Database.getDbConnection();
207:
208: // 1 2 3 4
209: sql = "UPDATE "
210: + TBL_MANDATORS
211: + " SET IS_ACTIVE=?, MODIFIED_BY=?, MODIFIED_AT=? WHERE ID=?";
212: final long NOW = System.currentTimeMillis();
213: ps = con.prepareStatement(sql);
214:
215: ps.setBoolean(1, true);
216: ps.setLong(2, ticket.getUserId());
217: ps.setLong(3, NOW);
218: ps.setLong(4, mandatorId);
219: ps.executeUpdate();
220: StructureLoader.updateMandator(FxContext.get()
221: .getDivisionId(),
222: new Mandator(mand.getId(), mand.getName(), mand
223: .getMetadataId(), true,
224: new LifeCycleInfoImpl(mand
225: .getLifeCycleInfo().getCreatorId(),
226: mand.getLifeCycleInfo()
227: .getCreationTime(), ticket
228: .getUserId(), NOW)));
229: } catch (SQLException exc) {
230: ctx.setRollbackOnly();
231: throw new FxUpdateException(LOG, exc,
232: "ex.mandator.updateFailed", mand.getName(), exc
233: .getMessage());
234: } finally {
235: Database.closeObjects(MandatorEngineBean.class, con, ps);
236: }
237: }
238:
239: /**
240: * {@inheritDoc}
241: */
242: @TransactionAttribute(TransactionAttributeType.REQUIRED)
243: public void deactivate(long mandatorId)
244: throws FxApplicationException {
245: final UserTicket ticket = FxContext.get().getTicket();
246: final FxEnvironment environment;
247: // Security
248: FxPermissionUtils.checkRole(ticket, Role.GlobalSupervisor);
249: environment = CacheAdmin.getEnvironment();
250: //exist check
251: Mandator mand = environment.getMandator(mandatorId);
252: if (!mand.isActive())
253: return; //silently ignore
254: if (mand.getId() == ticket.getMandatorId())
255: throw new FxInvalidParameterException("mandatorId",
256: "ex.mandator.deactivate.own", mand.getName(), mand
257: .getId());
258: Connection con = null;
259: PreparedStatement ps = null;
260: String sql;
261:
262: try {
263:
264: // Obtain a database connection
265: con = Database.getDbConnection();
266:
267: // 1 2 3 4
268: sql = "UPDATE "
269: + TBL_MANDATORS
270: + " SET IS_ACTIVE=?, MODIFIED_BY=?, MODIFIED_AT=? WHERE ID=?";
271: final long NOW = System.currentTimeMillis();
272: ps = con.prepareStatement(sql);
273:
274: ps.setBoolean(1, false);
275: ps.setLong(2, ticket.getUserId());
276: ps.setLong(3, NOW);
277: ps.setLong(4, mandatorId);
278: ps.executeUpdate();
279: StructureLoader.updateMandator(FxContext.get()
280: .getDivisionId(),
281: new Mandator(mand.getId(), mand.getName(), mand
282: .getMetadataId(), false,
283: new LifeCycleInfoImpl(mand
284: .getLifeCycleInfo().getCreatorId(),
285: mand.getLifeCycleInfo()
286: .getCreationTime(), ticket
287: .getUserId(), NOW)));
288: } catch (SQLException exc) {
289: ctx.setRollbackOnly();
290: throw new FxUpdateException(LOG, exc,
291: "ex.mandator.updateFailed", mand.getName(), exc
292: .getMessage());
293: } finally {
294: Database.closeObjects(MandatorEngineBean.class, con, ps);
295: }
296: }
297:
298: /**
299: * {@inheritDoc}
300: */
301: @TransactionAttribute(TransactionAttributeType.REQUIRED)
302: public void remove(long mandatorId) throws FxApplicationException {
303: final UserTicket ticket = FxContext.get().getTicket();
304: final FxEnvironment environment = CacheAdmin.getEnvironment();
305: // Security
306: FxPermissionUtils.checkRole(ticket, Role.GlobalSupervisor);
307: //exist check
308: Mandator mand = environment.getMandator(mandatorId);
309: Connection con = null;
310: PreparedStatement ps = null;
311: String sql;
312:
313: try {
314: try {
315: FxContext.get().runAsSystem();
316: grp.remove(grp.loadMandatorGroup(mandatorId).getId());
317: } finally {
318: FxContext.get().stopRunAsSystem();
319: }
320: con = Database.getDbConnection();
321: // 1
322: sql = "DELETE FROM " + TBL_GROUP
323: + " WHERE MANDATOR=? AND AUTOMANDATOR=TRUE";
324: ps = con.prepareStatement(sql);
325: ps.setLong(1, mandatorId);
326: ps.executeUpdate();
327: ps.close();
328: // 1
329: sql = "DELETE FROM " + TBL_MANDATORS + " WHERE ID=?";
330: ps = con.prepareStatement(sql);
331: ps.setLong(1, mandatorId);
332: ps.executeUpdate();
333: StructureLoader.removeMandator(FxContext.get()
334: .getDivisionId(), mandatorId);
335: } catch (SQLException exc) {
336: final boolean keyViolation = Database
337: .isForeignKeyViolation(exc);
338: ctx.setRollbackOnly();
339: if (keyViolation)
340: throw new FxEntryInUseException(exc,
341: "ex.mandator.removeFailed.inUse", mand
342: .getName());
343: throw new FxRemoveException(LOG, exc,
344: "ex.mandator.removeFailed", mand.getName(), exc
345: .getMessage());
346: } finally {
347: Database.closeObjects(MandatorEngineBean.class, con, ps);
348: }
349: }
350:
351: /**
352: * {@inheritDoc}
353: */
354: @TransactionAttribute(TransactionAttributeType.REQUIRED)
355: public void changeName(long mandatorId, String name)
356: throws FxApplicationException {
357: final UserTicket ticket = FxContext.get().getTicket();
358: final FxEnvironment environment = CacheAdmin.getEnvironment();
359: // Security
360: FxPermissionUtils.checkRole(ticket, Role.GlobalSupervisor);
361: //exist check
362: Mandator mand = environment.getMandator(mandatorId);
363: Connection con = null;
364: PreparedStatement ps = null;
365: String sql;
366:
367: try {
368: name = name.trim();
369: // Obtain a database connection
370: con = Database.getDbConnection();
371: // 1 2 3 4
372: sql = "UPDATE "
373: + TBL_MANDATORS
374: + " SET NAME=?, MODIFIED_BY=?, MODIFIED_AT=? WHERE ID=?";
375: final long NOW = System.currentTimeMillis();
376: ps = con.prepareStatement(sql);
377: ps.setString(1, name);
378: ps.setLong(2, ticket.getUserId());
379: ps.setLong(3, NOW);
380: ps.setLong(4, mandatorId);
381: ps.executeUpdate();
382: ps.close();
383: sql = "UPDATE " + TBL_GROUP
384: + " SET NAME=? WHERE AUTOMANDATOR=?";
385: ps = con.prepareStatement(sql);
386: ps.setString(1, "Everyone (" + name + ")");
387: ps.setLong(2, mandatorId);
388: ps.executeUpdate();
389: StructureLoader.updateMandator(FxContext.get()
390: .getDivisionId(),
391: new Mandator(mand.getId(), name, mand
392: .getMetadataId(), mand.isActive(),
393: new LifeCycleInfoImpl(mand
394: .getLifeCycleInfo().getCreatorId(),
395: mand.getLifeCycleInfo()
396: .getCreationTime(), ticket
397: .getUserId(), NOW)));
398: } catch (SQLException exc) {
399: ctx.setRollbackOnly();
400: throw new FxUpdateException(LOG, exc,
401: "ex.mandator.updateFailed", mand.getName(), exc
402: .getMessage());
403: } finally {
404: Database.closeObjects(MandatorEngineBean.class, con, ps);
405: }
406: }
407:
408: }
|