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

Quick Search    Search Deep

Source code: com/RuntimeCollective/content/bean/TextComponent.java


1   /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/content/bean/TextComponent.java,v 1.33 2003/10/03 14:47:40 criss Exp $
2    * $Revision: 1.33 $
3    * $Date: 2003/10/03 14:47:40 $
4    *
5    * ====================================================================
6    *
7    * Josephine : http://www.runtime-collective.com/josephine/index.html
8    *
9    * Copyright (C) 2003 Runtime Collective
10   * 
11   * This product includes software developed by the
12   * Apache Software Foundation (http://www.apache.org/).
13   *
14   * This library is free software; you can redistribute it and/or
15   * modify it under the terms of the GNU Lesser General Public
16   * License as published by the Free Software Foundation; either
17   * version 2.1 of the License, or (at your option) any later version.
18   *
19   * This library is distributed in the hope that it will be useful,
20   * but WITHOUT ANY WARRANTY; without even the implied warranty of
21   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22   * Lesser General Public License for more details.
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  
30  package com.RuntimeCollective.content.bean;
31  
32  import com.RuntimeCollective.webapps.RuntimeDataSource;
33  import com.RuntimeCollective.webapps.TextUtils;
34  import com.RuntimeCollective.webapps.RuntimeParameters;
35  
36  import org.apache.struts.util.ResponseUtils;
37  
38  import java.sql.SQLException;
39  
40  /**
41   * This class is a basic Content holding a piece of text.
42   * It doesn't have a title nor a description: those are inferred from the text.
43   *
44   * TextComponent can be used as is to store eg news items.
45   * Alternatively, it can be composed by a more complex Content,
46   * such as a website's Page, or a nice Article.
47   *
48   * @version $Id: TextComponent.java,v 1.33 2003/10/03 14:47:40 criss Exp $
49   */
50  public class TextComponent extends SimpleContent {
51      
52      // ---Inherited from EntityBean---------------------------
53      
54      /** The name of the database table for this bean type. */
55      public static final String DATABASE_TABLE = "content_textcomponent";
56      
57      /** Save this TextComponent to the database. */
58      public void save() throws SQLException {
59          
60          String dbalias = RuntimeDataSource.getDefaultAlias();
61          
62          // Save the TextComponent's Content attributes
63          super.save();
64          
65          // Save the TextComponent's basic attributes
66          RuntimeDataSource.save(id, "content_textcomponent", new String[] { "format", "title", "text" }, new Object[] { Format, Title, Text } );
67          
68          
69          /*
70           * The MS JDBC SQLserver Beta 2 driver is buggy, and cannot read large "ntext" fields properly.
71           * As a workaround, we're saving two versions of the text; one as an "ntext", which is
72           * only ever written to (for reporting purposes), and one as a binary "image",
73           * which we read.
74           *
75           * See ticket #3018 -joe
76           */
77          
78          String databaseType = RuntimeDataSource.getDefaultDatabaseType();
79          if (databaseType.equals("sqlserver")) {
80              
81              /* Start of workaround */
82              RuntimeParameters.logDebug(this, "Trying binary insert...");
83              RuntimeDataSource.saveBinaryText(dbalias, id, "content_textcomponent", "binary_text", Text ) ;
84              RuntimeParameters.logDebug(this, "... done!");
85              /* End of workaround */
86              
87          }
88      }
89      
90      /** Delete this TextComponent from the database. */
91      public void delete() throws SQLException {
92          String[] updates;
93          
94          updates = new String[] { "delete from content_textcomponent where id = "+id };
95          RuntimeDataSource.update( updates);
96          
97          super.delete();
98      }
99      
100     //---Inherited from Content---------------------
101     
102     /** Get the title */
103     public String getTitle() {
104         return Title;
105     }
106     
107     /** The max length of the description. */
108     protected static int MAX_DESCRIPTION_LENGTH = 50;
109     
110     /** Get the description
111      * Returns the first 50 chars of the text, if any.
112      */
113     public String getDescription() {
114         
115         if (getText() == null) {
116             return "";
117             
118         } else {
119             // we'll try to cut after a > (html tag?)
120             int position = Math.max(getText().indexOf('>', MAX_DESCRIPTION_LENGTH-10), MAX_DESCRIPTION_LENGTH-10);
121             return getText().substring(0, Math.min(position + 1, Math.min(MAX_DESCRIPTION_LENGTH, getText().length())));
122         }
123     }
124     
125     /** Set the description.
126      * NB: This does nothing.
127      */
128     public void setDescription(String description) {
129         // nothing
130     }
131     
132     /** Get the content in a certain format.
133      * "html", "htmlfull" and "plaintext" are supported for now.
134      * <p>
135      * If the desired format is "html", and if the init-param <code>convertLinks</code>
136      * is set to <code>true</code>, all links of the form <code>http://...</code>
137      * will be converted to hyperlinks.
138      */
139     public Object viewFormat(String format) {
140         
141         if (format.equals("html")) {
142             String con = null;
143             if (Format.equals("html"))
144                 con = getText();
145             else if (Format.equals("plaintext")) {
146                 con = TextUtils.plainTextToHtml(ResponseUtils.filter(getText()));
147             }
148             
149             if (con != null) {
150                 // Do we have to convert links?
151                 boolean convertLinks = false;
152                 try {
153                     convertLinks = Boolean.valueOf(RuntimeParameters.get("convertLinks")).booleanValue();
154                 } catch (RuntimeException e) {
155                 };
156                 if (convertLinks)
157                     con = TextUtils.convertLinks(con, "external_link");
158                 return con;
159             } else {
160                 return "This content (a TextComponent) cannot be converted from "+Format+" into html.";
161             }
162             
163         } else if (format.equals("plaintext")) {
164             if (Format.equals("plaintext"))
165                 return ResponseUtils.filter(getText());
166             else if (Format.equals("html"))
167                 return TextUtils.removeHtmlTags(getText());
168             else
169                 return "This content (a TextComponent) cannot be converted from "+Format+" into plaintext.";
170             
171         } else if (format.equals("htmlfull")) {
172             StringBuffer sb = new StringBuffer(200);
173             String title= getTitle();
174             String text = (String) viewFormat("html");
175             if (((title != null) && (!title.equals(""))) || (((text != null)) && (!text.equals(""))))
176                 sb.append("<p>");
177             if ((title != null) && (!title.equals("")))
178                 sb.append("<span class=\"texttitle\">").append(title).append("</span><br />");
179             if ((text != null) && (!text.equals("")))
180                 sb.append(text);
181             
182             return sb.toString();
183             
184         } else {
185             return null;
186         }
187     }
188     
189     
190     //---Specific to TextComponent---------------------
191     
192     /** The Title
193      * This shouldn't really be there - TextComponent is only a bit of text.
194      * But it's needed by the SE project for some boxes.
195      */
196     protected String Title;
197     
198     /** Set the Title */
199     public void setTitle(String title) {
200         Title = title;
201     }
202     
203     /** The Format */
204     protected String Format;
205     
206     /** Set the Format */
207     public void setFormat(String format) {
208         Format = format;
209     }
210     
211     /** Get the Format */
212     public String getFormat() {
213         return Format;
214     }
215     
216     /** The Text */
217     protected String Text;
218     
219     /** Set the Text */
220     public void setText(String text) {
221         //RuntimeParameters.logDebug(this, "TC text is now : \n"+text);
222         Text = text;
223     }
224     
225     /** Get the Text */
226     public String getText() {
227         return Text;
228     }
229     
230     
231     /** Upload some plain text, transform it to html, and set the content's type to html */
232     public void setTextAsHtml(String text, String oldFormat) {
233         setFormat("html");
234         
235         if (oldFormat.equals("html")) {
236             setText(text);
237             
238         } else if (oldFormat.equals("plaintext")) {
239             setText(TextUtils.plainTextToHtml(text));
240             
241         } else {
242             setText("Couldn't convert some text in "+oldFormat+" to Html.");
243         }
244     }
245     
246     
247     
248     /** Construct a new blank textcomponent, giving it a new unique ID. */
249     public TextComponent() throws SQLException {
250         
251         super();
252         
253         setFormat("html");
254         setText("");
255     }
256     
257     /** Get a current textcomponent from the RuntimeDataSource, given an id.
258      * @param id ID of the text component.
259      * @exception SQLException is thrown if no such textcomponent exists.
260      */
261     public TextComponent(int id) throws SQLException {
262         
263         super(id);
264         
265         String dbalias = RuntimeDataSource.getDefaultAlias();
266         
267         /* See ticket #3018, and the comment above in "save()" -joe */
268         
269         /* Start of workaround */
270         
271         String databaseType = RuntimeDataSource.getDefaultDatabaseType();
272         if (databaseType.equals("sqlserver")) {
273             
274             Object[] result = RuntimeDataSource.queryRow("select ctc.format, ctc.title from content_textcomponent ctc where ctc.id = "+id);
275             
276             if (result.length != 2) {
277                 throw new SQLException("Cannot load TextComponent "+id+": "+result.length+" fields found in content_textcomponent instead of 3.");
278             }
279             
280             setFormat(result[0].toString());
281             if (result[1] != null)
282                 setTitle(result[1].toString());
283             
284             // Get text from binary
285             //RuntimeParameters.logDebug(this, "Getting binary text...");
286             byte[] binaryText = RuntimeDataSource.readBinary(dbalias, "select ctc.binary_text from content_textcomponent ctc where ctc.id = "+id);
287             setText(new String(binaryText));
288             //RuntimeParameters.logDebug(this, "Got binary text! It's: "+getText());
289             
290         } else {
291             
292             /* When removing this workaround, keep this code! */
293             Object[] result = RuntimeDataSource.queryRow("select ctc.format, ctc.text, ctc.title from content_textcomponent ctc where ctc.id = "+id);
294             
295             if (result.length != 3) {
296                 throw new SQLException("Cannot load TextComponent "+id+": "+result.length+" fields found in content_textcomponent instead of 3.");
297             }
298             
299             setFormat(result[0].toString());
300             if (result[1] != null)
301                 setText(result[1].toString());
302             if (result[2] != null)
303                 setTitle(result[2].toString());
304             /* End of "keep this code" */
305             
306         }
307         
308         /* End of workaround */
309     }
310 }
311 
312 
313 
314