KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
11 import java.io.FileInputStream JavaDoc;
12 import java.io.FileNotFoundException JavaDoc;
13 import java.io.InputStream JavaDoc;
14 import java.net.URL JavaDoc;
15
16 import javax.ejb.CreateException JavaDoc;
17 import javax.ejb.FinderException JavaDoc;
18 import javax.ejb.SessionBean JavaDoc;
19 import javax.emb.MediaEntityLocal;
20 import javax.emb.MediaEntityLocalHome;
21 import javax.emb.MediaException;
22 import javax.naming.Context JavaDoc;
23 import javax.naming.InitialContext JavaDoc;
24 import javax.naming.NamingException JavaDoc;
25
26 /**
27  * Facade around the MediaEntity EJB.
28  *
29  * @version <tt>$Revision: 1.3 $</tt>
30  * @author <a HREF="mailto:ricardoarguello@users.sourceforge.net">Ricardo Argüello</a>
31  *
32  * @ejb.bean name="MediaEntityManager"
33  * jndi-name="ejb/media/MediaEntityManager"
34  * local-jndi-name="ejb/media/MediaEntityManagerLocal"
35  * view-type="both"
36  * type="Stateless"
37  *
38  * @ejb.transaction type="Required"
39  * @ejb.transaction-type type="Container"
40  *
41  * @ejb.ejb-ref ejb-name="MediaEntity" view-type="local"
42  */

43 public abstract class MediaEntityManagerBean implements SessionBean JavaDoc
44 {
45    /**
46     * Creates a new MediaEntity.
47     *
48     * @return the primary key of the MediaEntity created.
49     *
50     * @ejb.interface-method
51     */

52    public String JavaDoc createMediaEntity()
53       throws MediaException, CreateException JavaDoc, NamingException JavaDoc
54    {
55       MediaEntityLocalHome mediaEntityLocalHome = getMediaEntityLocalHome();
56       MediaEntityLocal mediaEntityLocal = mediaEntityLocalHome.create();
57       return (String JavaDoc) mediaEntityLocal.getPrimaryKey();
58    }
59
60    public MediaEntityDTO getMediaEntity(String JavaDoc identity)
61       throws MediaException, FinderException JavaDoc, NamingException JavaDoc
62    {
63       MediaEntityLocal mediaEntityLocal = this.findMediaEntity(identity);
64
65       MediaEntityDTO mediaEntityDTO = new MediaEntityDTO();
66       mediaEntityDTO.setContent(mediaEntityLocal.getContent());
67       mediaEntityDTO.setLocation(mediaEntityLocal.getLocation());
68       mediaEntityDTO.setDescription(mediaEntityLocal.getDescription());
69       mediaEntityDTO.setName(mediaEntityLocal.getName());
70       mediaEntityDTO.setMimeType(mediaEntityLocal.getMimeType());
71
72       return mediaEntityDTO;
73    }
74
75    /**
76     * Updates a MediaEntity given its primary key and a DTO.
77     *
78     * <p>If a property is not set in the DTO, it will be set to
79     * <code>null</code> in the MediaEntity.
80     *
81     * @param identity primary key.
82     * @param mediaEntityDTO Data Transfer Object with the properties to update.
83     * @return the primary key of the MediaEntity updated.
84     *
85     * @ejb.interface-method
86     */

87    public String JavaDoc updateMediaEntity(
88       String JavaDoc identity,
89       MediaEntityDTO mediaEntityDTO)
90       throws MediaException, FinderException JavaDoc, NamingException JavaDoc
91    {
92       MediaEntityLocal mediaEntityLocal = this.findMediaEntity(identity);
93
94       mediaEntityLocal.setContent(mediaEntityDTO.getContent());
95       mediaEntityLocal.setLocation(mediaEntityDTO.getLocation());
96       mediaEntityLocal.setDescription(mediaEntityDTO.getDescription());
97       mediaEntityLocal.setName(mediaEntityDTO.getName());
98       mediaEntityLocal.setMimeType(mediaEntityDTO.getMimeType());
99
100       return (String JavaDoc) mediaEntityLocal.getPrimaryKey();
101    }
102
103    /**
104     * Gets a MediaEntity property given its primary key and a property name.
105     *
106     * <p>Posible property names and their types are:
107     *
108     * <p>"content": <code>java.lang.String</code><br>
109     * "location": <code>java.net.URL</code><br>
110     * "description": <code>java.lang.String</code><br>
111     * "name": <code>java.lang.String</code><br>
112     * "mimeType": <code>java.lang.String</code>
113     *
114     * @param identity primary key.
115     * @param property property name.
116     * @param value property value.
117    
118     *
119     * @param identity primary key.
120     * @param property property name.
121     * @return the property value.
122     *
123     * @ejb.interface-method
124     */

125    public Object JavaDoc getMediaEntityProperty(String JavaDoc identity, String JavaDoc property)
126       throws MediaException, FinderException JavaDoc, NamingException JavaDoc
127    {
128       if (identity == null || property == null)
129       {
130          throw new NullPointerException JavaDoc();
131       }
132
133       MediaEntityLocal mediaEntityLocal = this.findMediaEntity(identity);
134
135       if (property.equals("content"))
136       {
137          return mediaEntityLocal.getContent();
138       }
139       else if (property.equals("location"))
140       {
141          return mediaEntityLocal.getLocation();
142       }
143       else if (property.equals("description"))
144       {
145          return mediaEntityLocal.getDescription();
146       }
147       else if (property.equals("name"))
148       {
149          return mediaEntityLocal.getName();
150       }
151       else if (property.equals("mimeType"))
152       {
153          return mediaEntityLocal.getMimeType();
154       }
155       else
156       {
157          throw new IllegalArgumentException JavaDoc(
158             "Invalid property name: " + property);
159       }
160    }
161
162    /**
163     * Sets a property for a MediaEntity given its primary key, a property name
164     * and a value.
165     *
166     * <p>Posible property names and their types are:
167     *
168     * <p>"content": <code>java.lang.String</code><br>
169     * "location": <code>java.net.URL</code><br>
170     * "description": <code>java.lang.String</code><br>
171     * "name": <code>java.lang.String</code><br>
172     * "mimeType": <code>java.lang.String</code>
173     *
174     * @param identity primary key.
175     * @param property property name.
176     * @param value property value.
177     *
178     * @ejb.interface-method
179     */

180    public void setMediaEntityProperty(
181       String JavaDoc identity,
182       String JavaDoc property,
183       Object JavaDoc value)
184       throws MediaException, FinderException JavaDoc, NamingException JavaDoc
185    {
186       if (identity == null || property == null)
187       {
188          throw new NullPointerException JavaDoc();
189       }
190
191       MediaEntityLocal mediaEntityLocal = this.findMediaEntity(identity);
192
193       try
194       {
195          if (property.equals("content"))
196          {
197             mediaEntityLocal.setContent((byte[]) value);
198          }
199          else if (property.equals("location"))
200          {
201             mediaEntityLocal.setLocation((URL JavaDoc) value);
202          }
203          else if (property.equals("description"))
204          {
205             mediaEntityLocal.setDescription((String JavaDoc) value);
206          }
207          else if (property.equals("name"))
208          {
209             mediaEntityLocal.setName((String JavaDoc) value);
210          }
211          else if (property.equals("mimeType"))
212          {
213             mediaEntityLocal.setMimeType((String JavaDoc) value);
214          }
215          else
216          {
217             throw new IllegalArgumentException JavaDoc(
218                "Invalid property name: " + property);
219          }
220       }
221       catch (ClassCastException JavaDoc e)
222       {
223          throw new IllegalArgumentException JavaDoc(
224             "Invalid value for property: " + property);
225       }
226    }
227
228    /**
229     * @ejb.interface-method
230     */

231    public void setMediaEntityContent(String JavaDoc identity, File JavaDoc contentFile)
232       throws MediaException, FinderException JavaDoc, NamingException JavaDoc, FileNotFoundException JavaDoc
233    {
234       if (identity == null)
235       {
236          throw new NullPointerException JavaDoc();
237       }
238
239       MediaEntityLocal mediaEntityLocal = this.findMediaEntity(identity);
240       InputStream JavaDoc contentStream = new FileInputStream JavaDoc(contentFile);
241       mediaEntityLocal.setContent(contentStream);
242    }
243
244    //--------------------------------------------------------------------------
245

246    private MediaEntityLocal findMediaEntity(String JavaDoc identity)
247       throws NamingException JavaDoc, FinderException JavaDoc
248    {
249       MediaEntityLocalHome mediaEntityLocalHome = getMediaEntityLocalHome();
250       return mediaEntityLocalHome.findByPrimaryKey(identity);
251    }
252
253    private MediaEntityLocalHome getMediaEntityLocalHome()
254       throws NamingException JavaDoc
255    {
256       Context JavaDoc initialContext = new InitialContext JavaDoc();
257       return (MediaEntityLocalHome) initialContext.lookup(MediaEntityBean.MEDIA_ENTITY_JNDI);
258    }
259 }
Popular Tags