1 package mircoders.entity;
7 import java.lang.reflect.*;
9 import freemarker.template.*;
16 import mircoders.storage.*;
19 * this class implements mapping of one line of the database table content
27 public class EntityContent extends Entity
30 String mirconf_extLinkName = MirConfig.getProp("Producer.ExtLinkName");
31 String mirconf_intLinkName = MirConfig.getProp("Producer.IntLinkName");
32 String mirconf_mailLinkName = MirConfig.getProp("Producer.MailLinkName");
33 String mirconf_imageRoot = MirConfig.getProp("Producer.ImageRoot");
37 public EntityContent()
40 //content_data is now filed-type "text"
41 //streamedInput = new ArrayList();
42 //streamedInput.add("content_data");
45 public EntityContent(StorageObject theStorage) {
47 setStorage(theStorage);
54 * set is_produced flag for the article
57 public void setProduced(boolean yesno) throws StorageObjectException
59 String value = (yesno) ? "1":"0";
60 if (value.equals( getValue("is_produced") )) return;
62 Connection con=null;Statement stmt=null;
63 String sql = "update content set is_produced='" + value + "' where id='" + getId()+"'";
65 con = theStorageObject.getPooledCon();
66 /** @todo should be preparedStatement: faster!! */
67 stmt = con.createStatement();
68 theStorageObject.executeUpdate(stmt,sql);
69 } catch (StorageObjectException e) {
70 throwStorageObjectException(e, "\n -- set produced failed");
71 } catch (SQLException e) {
72 throwStorageObjectException(e, "\n -- set produced failed");
74 theStorageObject.freeConnection(con,stmt);
80 * make openposting to newswire
83 public void newswire() throws StorageObjectException
85 String sql = "update content set to_article_type='1', is_produced='0' where id='" + getId()+"'";
87 theStorageObject.executeUpdate(sql);
88 } catch (StorageObjectException e) {
89 throwStorageObjectException(e, "\n -- newswire failed");
90 } catch (SQLException e) {
91 throwStorageObjectException(e, "\n -- newswire failed");
99 public void dettach(String cid,String mid) throws StorageObjectException
103 DatabaseContentToMedia.getInstance().delete(cid,mid);
104 } catch (Exception e){
105 throwStorageObjectException(e, "\n -- failed to get instance");
107 //set Content to unproduced
116 public void attach(String mid) throws StorageObjectException
119 //write media-id mid and content-id in table content_x_media
121 DatabaseContentToMedia.getInstance().addMedia(getId(),mid);
122 } catch(StorageObjectException e){
123 throwStorageObjectException(e, "attach: could not get the instance");
125 //set Content to unproduced
128 theLog.printError("EntityContent: attach without mid");
133 * overridden method getValue to include formatted date into every
137 public String getValue(String field)
139 String returnField = null;
142 if (field.equals("date_formatted"))
144 if (hasValueForField("date"))
145 returnField = StringUtil.webdbDate2readableDate(getValue("date"));
147 else if (field.equals("description_parsed"))
148 returnField = getDescriptionParsed();
149 else if (field.equals("content_data_parsed"))
150 returnField = getContentDataParsed();
152 return super.getValue(field);
157 public TemplateModel get(java.lang.String key) throws TemplateModelException
160 if (key.equals("to_comments")) {
162 return getComments();
164 catch (Exception ex) {
165 theLog.printWarning("-- getComments: could not fetch data " + ex.toString());
166 throw new TemplateModelException(ex.toString());
169 if (key.equals("to_media_images")) {
171 return getImagesForContent();
173 catch (Exception ex) {
174 theLog.printWarning("-- getImagesForContent: could not fetch data " + ex.toString());
175 throw new TemplateModelException(ex.toString());
178 if (key.equals("to_media_audio")) {
180 return getAudioForContent();
182 catch (Exception ex) {
183 theLog.printWarning("-- getAudioForContent: could not fetch data " + ex.toString());
184 throw new TemplateModelException(ex.toString());
187 if (key.equals("to_media_video")) {
189 return getVideoForContent();
191 catch (Exception ex) {
192 theLog.printWarning("-- getVideoForContent: could not fetch data " + ex.toString());
193 throw new TemplateModelException(ex.toString());
196 if (key.equals("to_media_other")) {
198 return getOtherMediaForContent();
200 catch (Exception ex) {
201 theLog.printWarning("-- getOtherMediaForContent: could not fetch data " + ex.toString());
202 throw new TemplateModelException(ex.toString());
205 else if (key.equals("to_media_icon")) {
207 return getUploadedMediaForNewswire();
209 catch (Exception ex) {
210 theLog.printWarning("-- getUploadedMediaForNewswire: could not fetch data " + ex.toString());
211 throw new TemplateModelException(ex.toString());
214 else if (key.equals("to_topics")) {
216 DatabaseContentToTopics.getInstance().getTopics(this);
218 catch (Exception ex) {
219 theLog.printWarning("-- getTopics: could not fetch data " + ex.toString());
220 throw new TemplateModelException(ex.toString());
224 return new SimpleScalar(getValue(key));
235 * overridden method setValues to patch creator_main_url
237 public void setValues(HashMap theStringValues) {
238 if (theStringValues != null) {
239 if (theStringValues.containsKey("creator_main_url")){
240 if (((String)theStringValues.get("creator_main_url")).equalsIgnoreCase("http://")){
241 theStringValues.remove("creator_main_url");
242 } else if (!((String)theStringValues.get("creator_main_url")).startsWith("http://")){
243 theStringValues.put("creator_main_url","http://"+((String)theStringValues.get("creator_main_url")));
247 super.setValues(theStringValues);
251 private String getContentDataParsed() {
252 String returnField = getValue("content_data");
253 if (returnField!=null & returnField.length()>0 ) {
254 returnField=StringUtil.deleteForbiddenTags(returnField);
255 //create http-links and email-links
256 if (getValue("is_html").equals("0")) {
257 returnField = StringUtil.createHTML(returnField,mirconf_imageRoot,
258 mirconf_mailLinkName,mirconf_extLinkName,
259 mirconf_intLinkName);
261 returnField = StringUtil.decodeHTMLinTags(returnField);
267 private String getDescriptionParsed() {
268 String returnField = getValue("description");
269 if (returnField != null && returnField.length()>0) {
270 returnField = StringUtil.deleteForbiddenTags(returnField);
271 if (getValue("is_html").equals("0")) {
272 returnField = StringUtil.createHTML(returnField,mirconf_imageRoot,
273 mirconf_mailLinkName,mirconf_extLinkName,
274 mirconf_intLinkName);
276 returnField = StringUtil.decodeHTMLinTags(returnField);
282 * fetches all the comments belonging to an article
284 * @return freemarker.template.SimpleList
286 private SimpleList getComments() throws StorageObjectException {
287 return ((DatabaseContent)theStorageObject).getComments(this);
290 // @todo this needs to optimized. expensive SQL
291 private SimpleHash getUploadedMediaForNewswire()
292 throws StorageObjectException, TemplateModelException
294 // fetching/setting the images
295 // return to_media_icons
296 String tinyIcon = null, iconAlt = null;
297 MirMedia mediaHandler = null;
298 EntityUploadedMedia uploadedMedia;
300 SimpleHash returnHash = new SimpleHash();
302 EntityList upMediaEntityList =
303 DatabaseContentToMedia.getInstance().getUploadedMedia(this);
304 if (upMediaEntityList!=null && upMediaEntityList.getCount()>=1) {
306 for (int n=0; n < upMediaEntityList.size();n++) {
307 uploadedMedia = (EntityUploadedMedia)upMediaEntityList.elementAt(n);
308 mediaType = uploadedMedia.getMediaType();
310 mediaHandler = MediaHelper.getHandler( mediaType );
311 } catch (MirMediaException ex) {
312 throw new TemplateModelException(ex.toString());
314 //the "best" media type to show
315 if (mediaHandler.isVideo()) {
316 tinyIcon = MirConfig.getProp("Producer.Icon.TinyVideo");
319 } else if (mediaHandler.isAudio()) {
320 tinyIcon = MirConfig.getProp("Producer.Icon.TinyAudio");
322 } else if (tinyIcon == null && !mediaHandler.isImage()) {
323 tinyIcon = mediaHandler.getTinyIcon();
324 iconAlt = mediaHandler.getIconAlt();
327 //it only has image(s)
328 if (tinyIcon == null) {
329 tinyIcon = MirConfig.getProp("Producer.Icon.TinyImage");
332 // uploadedMedia Entity list is empty.
335 tinyIcon = MirConfig.getProp("Producer.Icon.TinyText");
338 returnHash.put("tiny_icon", mirconf_imageRoot+"/"+tinyIcon);
339 returnHash.put("icon_alt", iconAlt);
343 //######## @todo all of the following getBlahForContent should have
344 // and optimized version where LIMIT=1 sql for list view.
345 private EntityList getImagesForContent()
346 throws StorageObjectException, TemplateModelException
348 return DatabaseContentToMedia.getInstance().getImages(this);
351 private EntityList getAudioForContent()
352 throws StorageObjectException, TemplateModelException
354 return DatabaseContentToMedia.getInstance().getAudio(this) ;
357 private EntityList getVideoForContent()
358 throws StorageObjectException, TemplateModelException
360 return DatabaseContentToMedia.getInstance().getVideo(this) ;
363 private EntityList getOtherMediaForContent()
364 throws StorageObjectException, TemplateModelException
366 return DatabaseContentToMedia.getInstance().getOther(this);