Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/opencms/defaults/master/CmsMasterMedia.java


1   /*
2   * File   : $Source: /usr/local/cvs/opencms/src/com/opencms/defaults/master/CmsMasterMedia.java,v $
3   * Date   : $Date: 2003/01/20 23:59:27 $
4   * Version: $Revision: 1.4 $
5   *
6   * This library is part of OpenCms -
7   * the Open Source Content Mananagement System
8   *
9   * Copyright (C) 2001  The OpenCms Group
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about OpenCms, please see the
22  * OpenCms Website: http://www.opencms.org 
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27  */
28  
29  package com.opencms.defaults.master;
30  
31  import com.opencms.core.I_CmsConstants;
32  import com.opencms.core.CmsException;
33  import com.opencms.file.CmsObject;
34  
35  /**
36   * An instance of this module describes a modulemedia entry in the database.
37   * It carries a set of data to read and write.
38   *
39   * @author A. Schouten $
40   * $Revision: 1.4 $
41   * $Date: 2003/01/20 23:59:27 $
42   */
43  public class CmsMasterMedia {
44  
45      /** Primary key for the media data */
46      private int m_id;
47  
48      /** Foreign key to the media master */
49      private int m_masterId;
50  
51      /** The position of this media */
52      private int m_position;
53  
54      /** The width of this media */
55      private int m_width;
56  
57      /** The height of this media */
58      private int m_height;
59  
60      /** The size of the media */
61      private int m_size;
62  
63      /** The mimetype of the media */
64      private String m_mimetype;
65  
66      /** The mediatype of the media */
67      private int m_type;
68  
69      /** The title of the media */
70      private String m_title;
71  
72      /** The filename of the media */
73      private String m_name;
74  
75      /** A description for the media */
76      private String m_description;
77  
78      /** The content of the media */
79      private byte[] m_media;
80  
81      /** Constant fro media type */
82      public static final int C_MEDIA_TYPE_IMAGE = 0;
83  
84      /** Constant fro media type */
85      public static final int C_MEDIA_TYPE_FILE = 1;
86  
87      /** The default mimetype */
88      public static final String C_DEFAULT_MIMETYPE = "application/octet-stream";
89  
90      //// constructors ////
91      /**
92       * Constructs a new instance with some default values.
93       */
94      public CmsMasterMedia() {
95          m_id = I_CmsConstants.C_UNKNOWN_ID;
96          m_masterId = I_CmsConstants.C_UNKNOWN_ID;
97          m_position = I_CmsConstants.C_UNKNOWN_ID;
98          m_width = I_CmsConstants.C_UNKNOWN_ID;
99          m_height = I_CmsConstants.C_UNKNOWN_ID;
100         m_size = I_CmsConstants.C_UNKNOWN_ID;
101         m_mimetype = C_DEFAULT_MIMETYPE;
102         m_type = I_CmsConstants.C_UNKNOWN_ID;
103         m_title = "";
104         m_name = "";
105         m_description ="";
106         m_media = new byte[0];
107     }
108 
109     /**
110      * Constructs a new instance with some default values.
111      */
112     public CmsMasterMedia(int position, int width, int height, int size,
113                           String mimetype, int type, String title, String name,
114                           String description, byte[] media) {
115         this();
116         m_position = position;
117         m_width = width;
118         m_height = height;
119         m_size = size;
120         m_mimetype = mimetype;
121         m_type = type;
122         m_title = title;
123         m_name = name;
124         m_description = description;
125         m_media = media;
126     }
127 
128     /**
129      * Constructs a new instance with some default values.
130      */
131     public CmsMasterMedia(int id, int masterId, int position, int width,
132                           int height, int size, String mimetype, int type,
133                           String title, String name, String description,
134                           byte[] media) {
135         this(position, width, height, size, mimetype, type, title, name,
136              description, media);
137         m_id = id;
138         m_masterId = masterId;
139     }
140 
141     /**
142      * Convenience method to compute the mimetype with the extension of the
143      * filename (e.g. mypic.gif -> image/gif)
144      * @param cms - the CmsObject to get access to cms ressources.
145      * @param filename - the filename to extract the extension from
146      * @return the computed mimetype.
147      */
148     public static String computeMimetype(CmsObject cms, String filename) {
149         String mimetype = null;
150         // get the extension of the filename to compute mimetype
151         int lastIndex = filename.lastIndexOf('.');
152         if(lastIndex > 0) {
153             String extension = filename.substring(lastIndex + 1);
154             try {
155                 mimetype = (String)(cms.readMimeTypes().get(extension));
156             } catch(CmsException exc) {
157                 // no mimetypes available -> use the default one
158             }
159         }
160         if(mimetype == null) {
161             return C_DEFAULT_MIMETYPE;
162         } else {
163             return mimetype;
164         }
165     }
166 
167     //// get and set methods ////
168 
169     public int getId() {
170         return m_id;
171     }
172 
173     public int getMasterId() {
174         return m_masterId;
175     }
176 
177     public int getPosition() {
178         return m_position;
179     }
180 
181     public int getWidth() {
182         return m_width;
183     }
184 
185     public int getHeight() {
186         return m_height;
187     }
188 
189     public int getSize() {
190         return m_size;
191     }
192 
193     public String getMimetype() {
194         return m_mimetype;
195     }
196 
197     public int getType() {
198         return m_type;
199     }
200 
201     public String getTitle() {
202         return m_title;
203     }
204 
205     public String getName() {
206         return m_name;
207     }
208 
209     public String getDescription() {
210         return m_description;
211     }
212 
213     public byte[] getMedia() {
214         return m_media;
215     }
216 
217     /**
218      * Never set this on yourself!
219      * It will be computed by the mastermodule
220      */
221     public void setId(int id) {
222         m_id = id;
223     }
224 
225     /**
226      * Never set this on yourself!
227      * It will be computed by the mastermodule
228      */
229     public void setMasterId(int id) {
230         m_masterId = id;
231     }
232 
233     public void setPosition(int pos) {
234         m_position = pos;
235     }
236 
237     public void setWidth(int width) {
238         m_width = width;
239     }
240 
241     public void setHeight(int height) {
242         m_height = height;
243     }
244 
245     public void setSize(int size) {
246         m_size = size;
247     }
248 
249     public void setMimetype(String mimetype) {
250         m_mimetype = mimetype;
251     }
252 
253     public void setType(int type) {
254         m_type = type;
255     }
256 
257     public void setTitle(String title) {
258         m_title = title;
259     }
260 
261     public void setName(String name) {
262         m_name = name;
263     }
264 
265     public void setDescription(String desc) {
266         m_description = desc;
267     }
268 
269     public void setMedia(byte[] media) {
270         m_media = media;
271         // now set the size
272         setSize(m_media.length);
273     }
274 
275     /**
276      * Returns a string representation of this instance.
277      * Can be used for debugging.
278      */
279     public String toString() {
280         StringBuffer retValue = new StringBuffer();
281         retValue
282             .append(getClass().getName() + "{")
283             .append("m_id:"+m_id+",")
284             .append("m_masterId:"+m_masterId+",")
285             .append("m_position:"+m_position+",")
286             .append("m_width:"+m_width+",")
287             .append("m_height:"+m_height+",")
288             .append("m_size:"+m_size+",")
289             .append("m_mimetype:"+m_mimetype+",")
290             .append("m_type:"+m_type+",")
291             .append("m_title:"+m_title+",")
292             .append("m_name:"+m_name+",")
293             .append("m_id:"+m_id+",")
294             .append("m_description:"+m_description+"}");
295         return retValue.toString();
296     }
297 }