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 Connection con=null;Statement stmt=null;
60 String value = (yesno) ? "1":"0";
61 String sql = "update content set is_produced='" + value + "' where id='" + getId()+"'";
63 con = theStorageObject.getPooledCon();
64 /** @todo should be preparedStatement: faster!! */
65 stmt = con.createStatement();
66 theStorageObject.executeUpdate(stmt,sql);
67 } catch (StorageObjectException e) {
68 throwStorageObjectException(e, "\n -- set produced failed");
69 } catch (SQLException e) {
70 throwStorageObjectException(e, "\n -- set produced failed");
72 theStorageObject.freeConnection(con,stmt);
78 * make openposting to newswire
81 public void newswire() throws StorageObjectException
83 String sql = "update content set to_article_type='1', is_produced='0' where id='" + getId()+"'";
85 theStorageObject.executeUpdate(sql);
86 } catch (StorageObjectException e) {
87 throwStorageObjectException(e, "\n -- newswire failed");
88 } catch (SQLException e) {
89 throwStorageObjectException(e, "\n -- newswire failed");
97 public void dettach(String cid,String mid) throws StorageObjectException
101 DatabaseContentToMedia.getInstance().delete(cid,mid);
102 } catch (Exception e){
103 throwStorageObjectException(e, "\n -- failed to get instance");
105 //set Content to unproduced
114 public void attach(String mid) throws StorageObjectException
117 //write media-id mid and content-id in table content_x_media
119 DatabaseContentToMedia.getInstance().addMedia(getId(),mid);
120 } catch(StorageObjectException e){
121 throwStorageObjectException(e, "attach: could not get the instance");
123 //set Content to unproduced
126 theLog.printError("EntityContent: attach without mid");
131 * overridden method getValue to include formatted date into every
135 public String getValue(String field)
137 String returnField = null;
140 if (field.equals("date_formatted"))
142 if (hasValueForField("date"))
143 returnField = StringUtil.webdbDate2readableDate(getValue("date"));
145 else if (field.equals("description_parsed"))
146 returnField = getDescriptionParsed();
147 else if (field.equals("content_data_parsed"))
148 returnField = getContentDataParsed();
150 return super.getValue(field);
155 public TemplateModel get(java.lang.String key) throws TemplateModelException
158 if (key.equals("to_comments")) {
160 return getComments();
162 catch (Exception ex) {
163 theLog.printWarning("-- getComments: could not fetch data " + ex.toString());
164 throw new TemplateModelException(ex.toString());
167 if (key.equals("to_media")) {
169 return getUploadedMediaForContent();
171 catch (Exception ex) {
172 theLog.printWarning("-- getUploadedMediaForContent: could not fetch data " + ex.toString());
173 throw new TemplateModelException(ex.toString());
176 else if (key.equals("to_topics")) {
178 HTMLTemplateProcessor.makeSimpleList(DatabaseContentToTopics.getInstance().getTopics(this));
180 catch (Exception ex) {
181 theLog.printWarning("-- getTopics: could not fetch data " + ex.toString());
182 throw new TemplateModelException(ex.toString());
185 else return new SimpleScalar(getValue(key));
195 * overridden method setValues to patch creator_main_url
197 public void setValues(HashMap theStringValues) {
198 if (theStringValues != null) {
199 if (theStringValues.containsKey("creator_main_url")){
200 if (((String)theStringValues.get("creator_main_url")).equalsIgnoreCase("http://")){
201 theStringValues.remove("creator_main_url");
202 } else if (!((String)theStringValues.get("creator_main_url")).startsWith("http://")){
203 theStringValues.put("creator_main_url","http://"+((String)theStringValues.get("creator_main_url")));
207 super.setValues(theStringValues);
211 private String getContentDataParsed() {
212 String returnField = getValue("content_data");
213 if (returnField!=null & returnField.length()>0 ) {
214 returnField=StringUtil.deleteForbiddenTags(returnField);
215 //create http-links and email-links
216 if (getValue("is_html").equals("0")) {
217 returnField = StringUtil.createHTML(returnField,mirconf_imageRoot,
218 mirconf_mailLinkName,mirconf_extLinkName,
219 mirconf_intLinkName);
221 returnField = StringUtil.decodeHTMLinTags(returnField);
227 private String getDescriptionParsed() {
228 String returnField = getValue("description");
229 if (returnField != null && returnField.length()>0) {
230 returnField = StringUtil.deleteForbiddenTags(returnField);
231 if (getValue("is_html").equals("0")) {
232 returnField = StringUtil.createHTML(returnField,mirconf_imageRoot,
233 mirconf_mailLinkName,mirconf_extLinkName,
234 mirconf_intLinkName);
236 returnField = StringUtil.decodeHTMLinTags(returnField);
242 * fetches all the comments belonging to an article
244 * @return freemarker.template.SimpleList
246 public SimpleList getComments() throws StorageObjectException {
247 return ((DatabaseContent)theStorageObject).getComments(this);
250 private SimpleList getUploadedMediaForList() throws StorageObjectException
253 private SimpleList getUploadedMediaForContent()
254 throws StorageObjectException, TemplateModelException
256 SimpleList returnList=null;
257 EntityList currentMediaList = DatabaseContentToMedia.getInstance().getUploadedMedia(this);
258 if (currentMediaList!=null && currentMediaList.getCount()>=1)
260 Entity upMedia,mediaType;
261 SimpleHash upMediaSimpleHash;
262 Class mediaHandlerClass,mediaStorageClass;
263 String mediaStorageName,mediaHandlerName=null;
264 MirMedia mediaHandler=null;
265 Database mediaStorage=null;
267 returnList = new SimpleList();
269 for (int n=0; n < currentMediaList.size();n++) {
270 upMedia = currentMediaList.elementAt(n);
271 upMediaSimpleHash = HTMLTemplateProcessor.makeSimpleHash(upMedia);
272 mediaType = ((EntityMedia)upMedia).getMediaType();
273 //must be a non-existant to_media_type entry..
274 if (mediaType != null) {
275 mediaHandlerName = mediaType.getValue("classname");
276 mediaStorageName = mediaType.getValue("tablename");
278 mediaStorageClass = Class.forName("mircoders.storage.Database"+mediaStorageName);
279 mediaHandlerClass = Class.forName("mir.media.MediaHandler"+mediaHandlerName);
280 mediaHandler = (MirMedia)mediaHandlerClass.newInstance();
281 Method m = mediaStorageClass.getMethod("getInstance", null);
282 mediaStorage = (Database)m.invoke(null, null);
284 catch (Exception ex) {
285 throw new TemplateModelException(ex.toString());
288 //we most likely need further info
289 upMedia = mediaStorage.selectById(upMedia.getId());
291 upMediaSimpleHash.put("url", mediaHandler.getURL(upMedia, mediaType));
293 catch (Exception ex) {
294 throw new TemplateModelException(ex.toString());
296 upMediaSimpleHash.put("type",mediaType.getValue("classname"));
297 returnList.add(upMediaSimpleHash);
298 } //end if media_type != null
300 } //end if currentMediaList != null