KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > media > entity > MetaDataEntityBean


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package org.jboss.media.entity;
9
10 import java.io.Reader JavaDoc;
11 import java.io.StringReader JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.Collection JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import javax.ejb.CreateException JavaDoc;
18 import javax.ejb.EntityBean JavaDoc;
19 import javax.ejb.EntityContext JavaDoc;
20 import javax.ejb.FinderException JavaDoc;
21 import javax.ejb.RemoveException JavaDoc;
22 import javax.emb.ContentAccessException;
23 import javax.emb.MediaEntityLocal;
24 import javax.emb.MediaException;
25 import javax.emb.MediaFormat;
26 import javax.emb.MetaDataEntityLocal;
27 import javax.emb.MetaDataSyntaxException;
28 import javax.emb.UnsupportedQueryLanguageException;
29 import javax.naming.InitialContext JavaDoc;
30 import javax.naming.NamingException JavaDoc;
31 import javax.xml.parsers.DocumentBuilder JavaDoc;
32 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
33
34 import org.jboss.ejb.plugins.keygenerator.KeyGenerator;
35 import org.jboss.ejb.plugins.keygenerator.KeyGeneratorFactory;
36 import org.jboss.logging.Logger;
37 import org.jboss.media.entity.query.JBossMediaQueryLanguage;
38 import org.jboss.media.entity.query.MediaQueryLanguage;
39 import org.w3c.dom.Document JavaDoc;
40 import org.xml.sax.InputSource JavaDoc;
41
42 /**
43  * JBoss implementation of the MetaDataEntity EJB.
44  *
45  * @version <tt>$Revision: 1.4 $</tt>
46  * @author <a HREF="mailto:ricardoarguello@users.sourceforge.net">Ricardo ArgŁello</a>
47  *
48  * @ejb.bean name="MetaDataEntity"
49  * local-jndi-name="ejb/media/MetaDataEntity"
50  * view-type="local"
51  * reentrant="true"
52  * type="CMP"
53  * cmp-version="2.x"
54  * primkey-field="managedIdentity"
55  *
56  * @ejb.interface generate="false" local-class="javax.emb.MetaDataEntityLocal"
57  * @ejb.home generate="false" local-class="javax.emb.MetaDataEntityLocalHome"
58  * @ejb.transaction type="Required"
59  * @ejb.util generate="false"
60  *
61  * @ejb.persistence table-name="JBOSS_METADATA_ENTITY_BEANS"
62  *
63  * @jboss.create-table "true"
64  * @jboss.remove-table "false"
65  */

66 public abstract class MetaDataEntityBean implements EntityBean JavaDoc
67 {
68    private EntityContext JavaDoc entityContext;
69
70    /** Logger. */
71    private Logger log = Logger.getLogger(MetaDataEntityBean.class);
72
73    /** KeyGenerator Factory JNDI. */
74    private static final String JavaDoc KEY_GENERATOR_JNDI = "UUIDKeyGeneratorFactory";
75
76    /** Query languages supported. */
77    private static final MediaQueryLanguage[] MEDIA_QUERY_LANGUAGES =
78       { new JBossMediaQueryLanguage()};
79
80    //--------------------------------------------------------------------------
81

82    /**
83     * @ejb.pk-field
84     * @ejb.persistence column-name="IDENTITY"
85     */

86    public abstract String JavaDoc getManagedIdentity();
87    public abstract void setManagedIdentity(String JavaDoc identity);
88
89    /**
90     * @ejb.persistence column-name="XML"
91     */

92    public abstract String JavaDoc getManagedXML();
93    public abstract void setManagedXML(String JavaDoc xml);
94
95    /**
96     * @ejb.persistence column-name="NAME"
97     */

98    public abstract String JavaDoc getManagedName();
99    public abstract void setManagedName(String JavaDoc name);
100
101    /**
102     * @ejb.persistence column-name="LAST_MODIFIED"
103     */

104    public abstract long getManagedLastModified();
105    public abstract void setManagedLastModified(long lastModified);
106
107    /**
108     * @ejb.relation name="PreviousMetaDataVersion-NextMetaDataVersion"
109     * role-name="previous-metadata-version-has-next-metadata-version"
110     *
111     * @jboss.relation related-pk-field="managedIdentity"
112     * fk-column="previous_identity_fk"
113     */

114    public abstract MetaDataEntityLocal getManagedPreviousVersion();
115    public abstract void setManagedPreviousVersion(MetaDataEntityLocal previousVersion);
116
117    /**
118     * @ejb.relation name="PreviousMetaDataVersion-NextMetaDataVersion"
119     * role-name="next-metadata-version-has-previous-metadata-version"
120     *
121     * @jboss.relation related-pk-field="managedIdentity"
122     * fk-column="child_identity_fk"
123     */

124    public abstract MetaDataEntityLocal getManagedNextVersion();
125    public abstract void setManagedNextVersion(MetaDataEntityLocal nextVersion);
126
127    /**
128     * @ejb.relation name="ParentMetaData-ChildMetaData"
129     * role-name="parent-metadata-has-child-metadata"
130     *
131     * @jboss.relation related-pk-field="managedIdentity"
132     * fk-column="parent_identity_fk"
133     */

134    public abstract Collection JavaDoc getManagedParents();
135    public abstract void setManagedParents(Collection JavaDoc parents);
136
137    /**
138     * @ejb.relation name="ParentMetaData-ChildMetaData"
139     * role-name="child-metadata-has-parent-metadata"
140     *
141     * @jboss.relation related-pk-field="managedIdentity"
142     * fk-column="child_identity_fk"
143     */

144    public abstract Collection JavaDoc getManagedChildren();
145    public abstract void setManagedChildren(Collection JavaDoc children);
146
147    /**
148     * @ejb.relation name="Media-MetaData"
149     * role-name="metadata-has-medias"
150     *
151     * @jboss.relation related-pk-field="managedIdentity"
152     * fk-column="media_identity_fk"
153     */

154    public abstract Collection JavaDoc getManagedMedias();
155    public abstract void setManagedMedias(Collection JavaDoc medias);
156
157    //--------------------------------------------------------------------------
158

159    /**
160     * @see javax.emb.MetaDataEntityLocal#addChild(javax.emb.MetaDataEntityLocal)
161     */

162    public void addChild(MetaDataEntityLocal child) throws MediaException
163    {
164       if (child == null)
165       {
166          throw new NullPointerException JavaDoc();
167       }
168
169       if (!this.getManagedChildren().contains(child))
170       {
171          this.getManagedChildren().add(child);
172       }
173    }
174
175    /**
176     * @see javax.emb.MetaDataEntityLocal#addMediaEntity(javax.emb.MediaEntityLocal)
177     */

178    public void addMediaEntity(MediaEntityLocal mediaEntity)
179       throws MediaException
180    {
181       if (mediaEntity == null)
182       {
183          throw new NullPointerException JavaDoc();
184       }
185
186       if (!this.getManagedMedias().contains(mediaEntity))
187       {
188          this.getManagedMedias().add(mediaEntity);
189          this.updateLastModified();
190       }
191    }
192
193    /**
194     * @see javax.emb.MetaDataEntityLocal#getChildren()
195     */

196    public MetaDataEntityLocal[] getChildren() throws MediaException
197    {
198       return (MetaDataEntityLocal[]) this.getManagedChildren().toArray(
199          new MetaDataEntityLocal[0]);
200    }
201
202    /**
203     * @see javax.emb.MetaDataEntityLocal#getLastModified()
204     */

205    public long getLastModified() throws MediaException
206    {
207       return this.getManagedLastModified();
208    }
209
210    /**
211     * @see javax.emb.MetaDataEntityLocal#getMediaEntities()
212     */

213    public MediaEntityLocal[] getMediaEntities() throws MediaException
214    {
215       return (MediaEntityLocal[]) this.getManagedMedias().toArray(
216          new MediaEntityLocal[0]);
217    }
218
219    /**
220     * @see javax.emb.MetaDataEntityLocal#getMediaEntities(javax.emb.MediaFormat, boolean)
221     */

222    public MediaEntityLocal[] getMediaEntities(
223       MediaFormat mediaFormat,
224       boolean searchChildren)
225       throws MediaException
226    {
227       if (mediaFormat == null)
228       {
229          throw new NullPointerException JavaDoc();
230       }
231
232       Collection JavaDoc mediaEntities = new ArrayList JavaDoc();
233       Iterator JavaDoc it = this.getManagedMedias().iterator();
234
235       while (it.hasNext())
236       {
237          MediaEntityLocal mediaEntity = (MediaEntityLocal) it.next();
238          MediaFormat format = mediaEntity.getFormat();
239          if (mediaFormat == format)
240          {
241             mediaEntities.add(mediaEntity);
242          }
243       }
244
245       if (searchChildren)
246       {
247          MetaDataEntityLocal children[] = this.getChildren();
248          for (int i = 0; i < children.length; i++)
249          {
250             MediaEntityLocal mediaEntity[] =
251                children[i].getMediaEntities(mediaFormat, searchChildren);
252
253             for (int j = 0; j < mediaEntity.length; j++)
254             {
255                mediaEntities.add(mediaEntity[j]);
256             }
257          }
258       }
259
260       return (MediaEntityLocal[]) mediaEntities.toArray(
261          new MediaEntityLocal[0]);
262    }
263
264    /**
265     * @see javax.emb.MetaDataEntityLocal#getMediaEntities(java.lang.String, boolean)
266     */

267    public MediaEntityLocal[] getMediaEntities(
268       String JavaDoc mimeType,
269       boolean searchChildren)
270       throws MediaException
271    {
272       if (mimeType == null)
273       {
274          throw new NullPointerException JavaDoc();
275       }
276
277       Collection JavaDoc mediaEntities = new ArrayList JavaDoc();
278       Iterator JavaDoc it = this.getManagedMedias().iterator();
279
280       while (it.hasNext())
281       {
282          MediaEntityLocal mediaEntity = (MediaEntityLocal) it.next();
283          String JavaDoc type = mediaEntity.getMimeType();
284          if (mimeType.equals(type))
285          {
286             mediaEntities.add(mediaEntity);
287          }
288       }
289
290       if (searchChildren)
291       {
292          MetaDataEntityLocal children[] = this.getChildren();
293          for (int i = 0; i < children.length; i++)
294          {
295             MediaEntityLocal mediaEntity[] =
296                children[i].getMediaEntities(mimeType, searchChildren);
297
298             for (int j = 0; j < mediaEntity.length; j++)
299             {
300                mediaEntities.add(mediaEntity[j]);
301             }
302          }
303       }
304
305       return (MediaEntityLocal[]) mediaEntities.toArray(
306          new MediaEntityLocal[0]);
307    }
308
309    /**
310     * @see javax.emb.MetaDataEntityLocal#getName()
311     */

312    public String JavaDoc getName() throws MediaException
313    {
314       return this.getManagedName();
315    }
316
317    /**
318     * @see javax.emb.MetaDataEntityLocal#getNextVersion()
319     */

320    public MetaDataEntityLocal getNextVersion() throws MediaException
321    {
322       return this.getManagedNextVersion();
323    }
324
325    /**
326     * @see javax.emb.MetaDataEntityLocal#getParents()
327     */

328    public MetaDataEntityLocal[] getParents() throws MediaException
329    {
330       return (MetaDataEntityLocal[]) this.getManagedParents().toArray(
331          new MetaDataEntityLocal[0]);
332    }
333
334    /**
335     * @see javax.emb.MetaDataEntityLocal#getPreviousVersion()
336     */

337    public MetaDataEntityLocal getPreviousVersion() throws MediaException
338    {
339       return this.getManagedPreviousVersion();
340    }
341
342    /**
343     * @see javax.emb.MetaDataEntityLocal#getXML()
344     */

345    public String JavaDoc getXML() throws MediaException
346    {
347       String JavaDoc xml = this.getManagedXML();
348
349       if (xml == null)
350       {
351          throw new ContentAccessException();
352       }
353
354       return xml;
355    }
356
357    /**
358     * @see javax.emb.MetaDataEntityLocal#removeChild(javax.emb.MetaDataEntityLocal)
359     */

360    public void removeChild(MetaDataEntityLocal child) throws MediaException
361    {
362       if (child == null)
363       {
364          throw new NullPointerException JavaDoc();
365       }
366
367       if (this.getManagedChildren().contains(child))
368       {
369          this.getManagedChildren().remove(child);
370       }
371    }
372
373    /**
374     * @see javax.emb.MetaDataEntityLocal#removeMediaEntity(javax.emb.MediaEntityLocal)
375     */

376    public void removeMediaEntity(MediaEntityLocal mediaEntity)
377       throws MediaException
378    {
379       if (mediaEntity == null)
380       {
381          throw new NullPointerException JavaDoc();
382       }
383
384       if (this.getManagedMedias().contains(mediaEntity))
385       {
386          this.getManagedMedias().remove(mediaEntity);
387       }
388    }
389
390    /**
391     * @see javax.emb.MetaDataEntityLocal#setName(java.lang.String)
392     */

393    public void setName(String JavaDoc name) throws MediaException
394    {
395       if (name == null)
396       {
397          throw new NullPointerException JavaDoc();
398       }
399
400       this.setManagedName(name);
401       this.updateLastModified();
402    }
403
404    /**
405     * @see javax.emb.MetaDataEntityLocal#setPreviousVersion(javax.emb.MetaDataEntityLocal)
406     */

407    public void setPreviousVersion(MetaDataEntityLocal metadata)
408       throws MediaException
409    {
410       // TODO: Implement
411
throw new UnsupportedOperationException JavaDoc("Not yet implemented!");
412    }
413
414    /**
415     * @see javax.emb.MetaDataEntityLocal#setXML(java.lang.String, boolean)
416     */

417    public void setXML(String JavaDoc xmlContent, boolean validate)
418       throws MediaException
419    {
420       if (xmlContent == null)
421       {
422          throw new NullPointerException JavaDoc();
423       }
424
425       if (validate)
426       {
427          Reader JavaDoc xmlReader = new StringReader JavaDoc(xmlContent);
428
429          try
430          {
431             InputSource JavaDoc xmlSource = new InputSource JavaDoc(xmlReader);
432             DocumentBuilderFactory JavaDoc factory =
433                DocumentBuilderFactory.newInstance();
434
435             DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
436             Document JavaDoc document = builder.parse(xmlSource);
437
438             // Document well formed
439
}
440          catch (Exception JavaDoc e)
441          {
442             // Document NOT well formed
443
throw new MetaDataSyntaxException(e);
444          }
445       }
446
447       this.setManagedXML(xmlContent);
448       this.updateLastModified();
449    }
450
451    // javax.emb.MetaDataEntityLocalHome interface: ----------------------------
452

453    /**
454     * @see javax.emb.MetaDataEntityLocalHome#create()
455     */

456    public String JavaDoc ejbCreate() throws CreateException JavaDoc
457    {
458       String JavaDoc identity = this.generateIdentity();
459
460       this.setManagedIdentity(identity);
461       this.updateLastModified();
462
463       return null;
464    }
465
466    /**
467     * @see javax.emb.MetaDataEntityLocalHome#create()
468     */

469    public void ejbPostCreate() throws CreateException JavaDoc
470    {
471    }
472
473    /**
474     * @see javax.emb.MetaDataEntityLocalHome#query(java.lang.String, java.lang.String, java.util.Map)
475     */

476    public Collection JavaDoc ejbHomeQuery(
477       String JavaDoc query,
478       String JavaDoc queryLanguage,
479       Map JavaDoc options)
480       throws FinderException JavaDoc, MediaException
481    {
482       if ((query == null) || (queryLanguage == null))
483       {
484          throw new NullPointerException JavaDoc();
485       }
486
487       MediaQueryLanguage mediaQueryLanguage = null;
488
489       for (int i = 0; i < MEDIA_QUERY_LANGUAGES.length; i++)
490       {
491          if (queryLanguage.equals(MEDIA_QUERY_LANGUAGES[i].getName()))
492          {
493             mediaQueryLanguage = MEDIA_QUERY_LANGUAGES[i];
494          }
495       }
496
497       // Query language not found
498
if (mediaQueryLanguage == null)
499       {
500          throw new UnsupportedQueryLanguageException();
501       }
502
503       mediaQueryLanguage.setOptions(options);
504
505       return mediaQueryLanguage.query(this, query);
506    }
507
508    /**
509     * @see javax.emb.MetaDataEntityLocalHome#retrieveSupportedOptions(java.lang.String)
510     */

511    public String JavaDoc[] ejbHomeRetrieveSupportedOptions(String JavaDoc queryLanguage)
512       throws MediaException
513    {
514       if (queryLanguage == null)
515       {
516          throw new NullPointerException JavaDoc();
517       }
518
519       for (int i = 0; i < MEDIA_QUERY_LANGUAGES.length; i++)
520       {
521          MediaQueryLanguage mediaQueryLanguage = MEDIA_QUERY_LANGUAGES[i];
522
523          if (queryLanguage.equals(mediaQueryLanguage.getName()))
524          {
525             Map JavaDoc options = mediaQueryLanguage.getOptions();
526             String JavaDoc[] supportedOptions = new String JavaDoc[options.size()];
527
528             Iterator JavaDoc it = options.keySet().iterator();
529             int j = 0;
530
531             while (it.hasNext())
532             {
533                String JavaDoc option = (String JavaDoc) it.next();
534                supportedOptions[j] = option;
535                j++;
536             }
537
538             return supportedOptions;
539          }
540       }
541
542       // Query language not found
543
throw new UnsupportedQueryLanguageException();
544    }
545
546    /**
547     * @see javax.emb.MetaDataEntityLocalHome#retrieveSupportedQueryLanguages()
548     */

549    public String JavaDoc[] ejbHomeRetrieveSupportedQueryLanguages()
550       throws MediaException
551    {
552       String JavaDoc[] supportedQueryLanguages =
553          new String JavaDoc[MEDIA_QUERY_LANGUAGES.length];
554
555       for (int i = 0; i < MEDIA_QUERY_LANGUAGES.length; i++)
556       {
557          supportedQueryLanguages[i] = MEDIA_QUERY_LANGUAGES[i].getName();
558       }
559
560       return supportedQueryLanguages;
561    }
562
563    //--------------------------------------------------------------------------
564

565    /**
566     * @ejb.select query="SELECT DISTINCT OBJECT(metaDataEntity) FROM MetaDataEntity AS metaDataEntity WHERE LOCATE(?1, metaDataEntity.managedXML) > -1"
567     */

568    public abstract Collection JavaDoc ejbSelectByPartialXML(String JavaDoc partialXML)
569       throws FinderException JavaDoc;
570
571    // javax.ejb.EntityBean interface: -----------------------------------------
572

573    public void ejbActivate()
574    
575    {
576    }
577
578    public void ejbPassivate()
579    {
580    }
581
582    public void ejbRemove() throws RemoveException JavaDoc
583    {
584    }
585
586    public void setEntityContext(EntityContext JavaDoc entityContext)
587    {
588       this.entityContext = entityContext;
589    }
590
591    public void unsetEntityContext()
592    {
593       this.entityContext = null;
594    }
595
596    public void ejbLoad()
597    {
598    }
599
600    public void ejbStore()
601    {
602    }
603
604    // Private methods: --------------------------------------------------------
605

606    private void updateLastModified()
607    {
608       this.setManagedLastModified(System.currentTimeMillis());
609    }
610
611    /**
612     * Create the identity (primary key) of this bean using a UUID key
613     * generator.
614     *
615     * @return a String containing the primary key.
616     * @throws CreateException if an error accours when generating the primary
617     * key.
618     */

619    private String JavaDoc generateIdentity() throws CreateException JavaDoc
620    {
621       KeyGenerator keyGenerator = null;
622
623       try
624       {
625          KeyGeneratorFactory keyGeneratorFactory =
626             (KeyGeneratorFactory) new InitialContext JavaDoc().lookup(
627                KEY_GENERATOR_JNDI);
628
629          keyGenerator = keyGeneratorFactory.getKeyGenerator();
630       }
631       catch (NamingException JavaDoc e)
632       {
633          throw new CreateException JavaDoc(
634             "Error: can't find key generator factory: "
635                + KEY_GENERATOR_JNDI
636                + "; "
637                + e.getMessage());
638       }
639       catch (Exception JavaDoc e)
640       {
641          throw new CreateException JavaDoc(
642             "Error: can't create key generator instance; key generator factory: "
643                + KEY_GENERATOR_JNDI
644                + "; "
645                + e.getMessage());
646       }
647
648       return (String JavaDoc) keyGenerator.generateKey();
649    }
650 }
Popular Tags