001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.kernel.ejb;
034:
035: import org.libresource.Libresource;
036:
037: import org.libresource.kernel.KernelConstants;
038: import org.libresource.kernel.interfaces.KernelService;
039: import org.libresource.kernel.interfaces.NodeLocal;
040: import org.libresource.kernel.util.NodeUtil;
041:
042: import java.net.URI;
043:
044: import java.util.Collection;
045: import java.util.Date;
046: import java.util.HashMap;
047: import java.util.Iterator;
048: import java.util.StringTokenizer;
049: import java.util.Vector;
050:
051: import javax.ejb.CreateException;
052: import javax.ejb.EntityBean;
053: import javax.ejb.EntityContext;
054: import javax.ejb.FinderException;
055: import javax.ejb.ObjectNotFoundException;
056: import javax.ejb.RemoveException;
057:
058: import javax.naming.NamingException;
059:
060: /**
061: * LibreSource
062: *
063: * @author <a href="mailto:bort@loria.fr">Guillaume Bort</a> - <a href="http://www.inria.fr">INRIA Lorraine</a>
064: *
065: * @ejb.bean name="Node"
066: * type="CMP"
067: * transaction-type="Container"
068: * reentrant="true"
069: * view-type="both"
070: * primkey-field="id"
071: * cmp-version="2.x"
072: * jndi-name="Node"
073: * local-jndi-name="Node_L"
074: *
075: * @jonas.bean
076: * ejb-name="Node"
077: * jndi-name="Node"
078: *
079: * @jonas.jdbc-mapping
080: * jndi-name="@DATASOURCE@"
081: * automatic-pk="false"
082: *
083: * @jonas.shared false
084: * @jonas.passivation-timeout 600
085: * @jonas.min-pool-size 10000
086: *
087: * @ejb.finder signature = "java.lang.String findByBindedResource(java.lang.String resourceIdentifier)"
088: * query ="SELECT OBJECT(n) FROM Node n WHERE n.bindedResourceIdentifier = ?1"
089: * transaction-type="Supports"
090: *
091: * @ejb.finder signature = "java.util.Collection findAll()"
092: * query ="SELECT OBJECT(n) FROM Node n"
093: * transaction-type="Supports"
094: *
095: * @ejb.finder signature= "java.util.Collection findURIWhereResourceIsOwner(java.lang.String uri)"
096: * query ="SELECT OBJECT(n) FROM Node n WHERE n.owner = ?1"
097: * transaction-type="Supports"
098: *
099: * @ejb.finder signature= "java.util.Collection findResources(java.lang.String uri, java.lang.String resourceIdentifierPattern)"
100: * query ="SELECT OBJECT(n) FROM Node n WHERE n.parent = ?1 AND n.bindedResourceIdentifier LIKE ?2"
101: * transaction-type="Supports"
102: *
103: * @ejb.finder signature= "java.util.Collection findChildren(java.lang.String parentId)"
104: * query ="SELECT OBJECT(n) FROM Node n WHERE n.parent = ?1"
105: * transaction-type="Supports"
106: *
107: * @ejb.transaction type="Supports"
108: * @jonas.lock policy="container-read-committed"
109: */
110: public abstract class NodeBean implements EntityBean {
111: protected EntityContext ctx;
112:
113: /**
114: * @ejb.select
115: * query ="SELECT COUNT(n) FROM Node AS n WHERE n.bindedResourceIdentifier LIKE ?1"
116: * result-type-mapping="Local"
117: */
118: public abstract int ejbSelectCountResources(String resourceIdPattern)
119: throws FinderException;
120:
121: /**
122: * @ejb.home-method
123: */
124: public int ejbHomeCountResource(String service, String resourceType)
125: throws javax.ejb.FinderException {
126: return ejbSelectCountResources(service + "/" + resourceType
127: + "/%");
128: }
129:
130: /**
131: * @ejb.home-method
132: */
133: public NodeLocal ejbHomeGetByPath(String path) throws Exception {
134: StringTokenizer tokenizer = new StringTokenizer(path, "/");
135: String token = null;
136: NodeLocal current = null;
137:
138: try {
139: current = NodeUtil.getLocalHome().findByPrimaryKey("666");
140: } catch (Exception e) {
141: ((KernelService) Libresource
142: .getService(KernelConstants.SERVICE))
143: .systemBootstrap();
144: current = NodeUtil.getLocalHome().findByPrimaryKey("666");
145: }
146:
147: while (tokenizer.hasMoreTokens()) {
148: if (current.getChildren() == null) {
149: throw new ObjectNotFoundException("\""
150: + current.getPath() + "\" has no children");
151: } else {
152: token = tokenizer.nextToken();
153:
154: String child = (String) current.getChildren()
155: .get(token);
156:
157: if (child != null) {
158: current = NodeUtil.getLocalHome().findByPrimaryKey(
159: child);
160: } else {
161: throw new ObjectNotFoundException("\""
162: + current.getPath() + "\" has no child \""
163: + child + "\"");
164: }
165: }
166:
167: if (current == null) {
168: throw new ObjectNotFoundException();
169: }
170: }
171:
172: return current;
173: }
174:
175: /**
176: * @ejb.create-method
177: */
178: public String ejbCreate(NodeLocal parent, String pathPart)
179: throws CreateException {
180: try {
181: if (pathPart.compareTo("") != 0) {
182: setId(NodeUtil.generateGUID(this ));
183: setPathPart(pathPart);
184: } else {
185: setId("666");
186: setPathPart("");
187: }
188:
189: setShortName("(name not defined)");
190: setCreationDate(new Date());
191: setUpdateDate(new Date());
192: setAcls(new HashMap(1));
193: setProperties(new HashMap(1));
194: setOwner("");
195: setChildren(new HashMap(1));
196: } catch (Exception e) {
197: throw new CreateException(e.getMessage());
198: }
199:
200: return null;
201: }
202:
203: public void ejbPostCreate(NodeLocal parent, String pathPart)
204: throws CreateException {
205: try {
206: if (parent != null) {
207: setParent(parent.getId());
208: } else {
209: setParent("");
210: }
211:
212: updatePath();
213: } catch (Exception e) {
214: throw new CreateException("error in node.ejbPostCreate : "
215: + e.getMessage());
216: }
217: }
218:
219: // persistent fields
220:
221: /**
222: * @ejb.persistent-field
223: * @ejb.interface-method
224: * @ejb.transaction type="Supports"
225: */
226: public abstract String getId();
227:
228: /**
229: * @ejb.persistent-field
230: * @ejb.interface-method
231: * @ejb.transaction type="Mandatory"
232: */
233: public abstract void setId(String id);
234:
235: /**
236: * @ejb.persistent-field
237: * @ejb.interface-method
238: * @ejb.transaction type="Supports"
239: */
240: public abstract String getShortName();
241:
242: /**
243: * @ejb.persistent-field
244: * @ejb.interface-method
245: * @ejb.transaction type="Mandatory"
246: */
247: public abstract void setShortName(String shortName);
248:
249: /**
250: * @ejb.persistent-field
251: * @ejb.interface-method
252: * @ejb.transaction type="Supports"
253: */
254: public abstract Date getCreationDate();
255:
256: /**
257: * @ejb.persistent-field
258: * @ejb.interface-method
259: * @ejb.transaction type="Mandatory"
260: */
261: public abstract void setCreationDate(Date creationDate);
262:
263: /**
264: * @ejb.persistent-field
265: * @ejb.interface-method
266: * @ejb.transaction type="Supports"
267: */
268: public abstract Date getUpdateDate();
269:
270: /**
271: * @ejb.persistent-field
272: * @ejb.interface-method
273: * @ejb.transaction type="Mandatory"
274: */
275: public abstract void setUpdateDate(Date updateDate);
276:
277: /**
278: * @ejb.persistent-field
279: * @ejb.interface-method
280: * @ejb.transaction type="Supports"
281: */
282: public abstract String getBindedResourceIdentifier();
283:
284: /**
285: * @ejb.persistent-field
286: * @ejb.interface-method
287: * @ejb.transaction type="Mandatory"
288: */
289: public abstract void setBindedResourceIdentifier(
290: String libresourceResourceIdentifier);
291:
292: /**
293: * @ejb.persistent-field
294: * @ejb.interface-method
295: * @ejb.transaction type="Supports"
296: */
297: public abstract String getPathPart();
298:
299: /**
300: * @ejb.persistent-field
301: * @ejb.interface-method
302: * @ejb.transaction type="Mandatory"
303: */
304: public abstract void setPathPart(String name);
305:
306: /**
307: * @ejb.persistent-field
308: * @ejb.interface-method
309: * @ejb.transaction type="Supports"
310: */
311: public abstract String getPath();
312:
313: /**
314: * @ejb.persistent-field
315: * @ejb.interface-method
316: * @ejb.transaction type="Mandatory"
317: */
318: public abstract void setPath(String path);
319:
320: /**
321: * @ejb.persistent-field
322: * @ejb.interface-method
323: * @ejb.transaction type="Supports"
324: */
325: public abstract HashMap getProperties();
326:
327: /**
328: * @ejb.persistent-field
329: * @ejb.interface-method
330: * @ejb.transaction type="Mandatory"
331: */
332: public abstract void setProperties(HashMap properties);
333:
334: /**
335: * @ejb.persistent-field
336: * @ejb.interface-method view-type="local"
337: * @ejb.transaction type="Supports"
338: */
339: public abstract HashMap getChildren();
340:
341: /**
342: * @ejb.persistent-field
343: * @ejb.interface-method view-type="local"
344: * @ejb.transaction type="Mandatory"
345: */
346: public abstract void setChildren(HashMap children);
347:
348: /**
349: * @ejb.persistent-field
350: * @ejb.interface-method
351: * @ejb.transaction type="Supports"
352: */
353: public abstract HashMap getAcls();
354:
355: /**
356: * @ejb.persistent-field
357: * @ejb.interface-method
358: * @ejb.transaction type="Mandatory"
359: */
360: public abstract void setAcls(HashMap acls);
361:
362: /**
363: * @ejb.persistent-field
364: * @ejb.interface-method
365: * @ejb.transaction type="Supports"
366: */
367: public abstract String getParent();
368:
369: /**
370: * @ejb.persistent-field
371: * @ejb.interface-method
372: * @ejb.transaction type="Mandatory"
373: */
374: public abstract void setParent(String parent);
375:
376: /**
377: * @ejb.persistent-field
378: * @ejb.interface-method
379: * @ejb.transaction type="Supports"
380: */
381: public abstract String getOwner();
382:
383: /**
384: * @ejb.persistent-field
385: * @ejb.interface-method
386: * @ejb.transaction type="Mandatory"
387: */
388: public abstract void setOwner(String owner);
389:
390: // business method
391:
392: /**
393: * @ejb.interface-method view-type="local"
394: * @ejb.transaction type="Supports"
395: */
396: public Collection getChildren2() {
397: HashMap children = getChildren();
398: Vector result = new Vector();
399:
400: if (children != null) {
401: for (Iterator i = children.values().iterator(); i.hasNext();) {
402: try {
403: String next = (String) i.next();
404:
405: if (next != null) {
406: result.add(NodeUtil.getLocalHome()
407: .findByPrimaryKey(next));
408: }
409: } catch (Exception e) {
410: }
411: }
412: }
413:
414: return result;
415: }
416:
417: /**
418: * @ejb.interface-method
419: * @ejb.transaction type="Required"
420: */
421: public void updatePath() throws Exception {
422: if (getPath() != null) {
423: Libresource.removeFromIndex(getURI());
424: }
425:
426: if (getParent().trim().length() == 0) {
427: setPath("/");
428: } else {
429: if (NodeUtil.getLocalHome().findByPrimaryKey(getParent())
430: .getPath().equals("/")) {
431: setPath("/" + getPathPart());
432: } else {
433: setPath(NodeUtil.getLocalHome().findByPrimaryKey(
434: getParent()).getPath()
435: + "/" + getPathPart());
436: }
437: }
438:
439: Libresource.index(getURI());
440:
441: Collection children = getChildren2();
442:
443: if (children != null) {
444: for (Iterator i = children.iterator(); i.hasNext();) {
445: (NodeUtil.getLocalHome()
446: .findByPrimaryKey(((NodeLocal) i.next())
447: .getId())).updatePath();
448: }
449: }
450: }
451:
452: /**
453: * @ejb.interface-method
454: * @ejb.transaction type="Supports"
455: */
456: public URI getURI() throws Exception {
457: return new URI(getPath());
458: }
459:
460: /**
461: * @ejb.interface-method view-type="local"
462: * @ejb.transaction type="Required"
463: */
464: public void addChild(NodeLocal child) throws Exception {
465: HashMap children = getChildren();
466: children.put(child.getPathPart(), child.getId());
467: setChildren(children);
468: }
469:
470: /**
471: * @ejb.interface-method view-type="local"
472: * @ejb.transaction type="Required"
473: */
474: public void removeChild(String child) {
475: HashMap children = getChildren();
476: children.remove(child);
477: setChildren(children);
478: }
479:
480: /**
481: * @ejb.interface-method
482: * @ejb.transaction type="Supports"
483: */
484: public boolean hasResourceBinded() {
485: return getBindedResourceIdentifier() != null;
486: }
487:
488: // standard call back methods
489: public void setEntityContext(EntityContext ec) {
490: ctx = ec;
491: }
492:
493: public void unsetEntityContext() {
494: ctx = null;
495: }
496:
497: public void ejbLoad() {
498: }
499:
500: public void ejbStore() {
501: }
502:
503: public void ejbActivate() {
504: }
505:
506: public void ejbPassivate() {
507: }
508:
509: public void ejbRemove() throws RemoveException {
510: // indexation
511: try {
512: if (getPath() != null) {
513: Libresource.removeFromIndex(getURI());
514: }
515: } catch (Exception e) {
516: //System.out.println("Lucene index may be corrupted... You can re-index the platform to solve the problem...");
517: }
518:
519: try {
520: for (Iterator i = getChildren().values().iterator(); i
521: .hasNext();) {
522: String childId = "";
523: childId = (String) i.next();
524: i.remove();
525:
526: NodeLocal child = NodeUtil.getLocalHome()
527: .findByPrimaryKey(childId);
528:
529: // indexation
530: try {
531: if (child.getPath() != null) {
532: Libresource.removeFromIndex(child.getURI());
533: }
534: } catch (Exception e) {
535: //System.out.println("Lucene index may be corrupted... You can re-index the platform to solve the problem...");
536: }
537:
538: child.remove();
539: }
540:
541: NodeUtil.getLocalHome().findByPrimaryKey(getParent())
542: .removeChild(getPathPart());
543: } catch (FinderException e) {
544: // nothing
545: } catch (NamingException e) {
546: // nothing
547: } catch (Exception e) {
548: // nothing
549: }
550: }
551: }
|