exception + misc. cleanup
[mir.git] / source / mircoders / module / ModuleContent.java
1 /*
2  * Copyright (C) 2001, 2002  The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with the com.oreilly.servlet library, any library
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24  * the above that use the same license as the above), and distribute linked
25  * combinations including the two.  You must obey the GNU General Public
26  * License in all respects for all of the code used other than the above
27  * mentioned libraries.  If you modify this file, you may extend this exception
28  * to your version of the file, but you are not obligated to do so.  If you do
29  * not wish to do so, delete this exception statement from your version.
30  */
31
32 package mircoders.module;
33
34 import java.util.Iterator;
35 import java.util.Map;
36 import java.util.Set;
37 import java.util.StringTokenizer;
38
39 import mir.entity.EntityList;
40 import mir.log.LoggerWrapper;
41 import mir.module.AbstractModule;
42 import mir.module.ModuleExc;
43 import mir.module.ModuleFailure;
44 import mir.storage.StorageObject;
45 import mir.storage.StorageObjectFailure;
46 import mir.util.JDBCStringRoutines;
47 import mircoders.entity.EntityUsers;
48 import mircoders.storage.DatabaseContentToMedia;
49
50 /*
51  *  ContentObjekt -
52  *
53  * @version $Id: ModuleContent.java,v 1.17 2003/03/09 03:53:11 zapata Exp $
54  *
55  * @author RK, mir-coders
56  *
57  */
58
59 public class ModuleContent extends AbstractModule
60 {
61   static LoggerWrapper logger = new LoggerWrapper("Module.Content");
62
63   public ModuleContent() {
64     super();
65   }
66
67   public ModuleContent(StorageObject theStorage) {
68     this.theStorage = theStorage;
69   }
70
71 //
72 // various methods to retrieve content entities
73
74 //  public EntityList getFeatures(int offset, int limit) throws ModuleExc, ModuleFailure {
75 //    return getContent("is_published=true AND to_article_type=2", "webdb_create desc",
76 //                      offset, limit);
77 //  }
78
79 //  public EntityList getNewsWire(int offset, int limit) throws ModuleExc, ModuleFailure {
80 //    return getContent("is_published=true AND to_article_type = 1",
81 //                      "webdb_create desc",offset,limit);
82 //  }
83
84 //  public EntityList getStartArticle() throws ModuleExc, ModuleFailure {
85 //    EntityList returnList = getContent("is_published=true AND to_article_type=4",
86 //                                       "webdb_create desc",0,1);
87 //if no startspecial exists
88 //    if (returnList==null || returnList.size()==0)
89 //      returnList = getContent("is_published=true AND to_article_type=3",
90 //                              "webdb_create desc",0,1);
91
92 //    return returnList;
93 //  }
94
95 /*
96       public EntityList getContent(Map searchValues, boolean concat, int offset, EntityUsers user) throws ModuleException {
97
98     try {
99
100       String whereClause ="", aField, aValue;
101       boolean first = true;
102
103       Set set = searchValues.keySet();
104       Iterator it = set.iterator();
105       for (int i=0;i<set.size();i++) {
106         aField = (String)it.next();
107         aValue = (String)searchValues.get(aField);
108
109         if (first == false)
110           whereClause +=  (concat) ? " and " : " or ";
111         else
112           first = false;
113
114         whereClause += "(";
115
116 // default: hier splitten der eintraege und verknupfung mit AND OR NOT
117         StringTokenizer st = new StringTokenizer(aValue);
118         boolean firstToken = true;
119         while(st.hasMoreTokens()) {
120           String notString = "";
121           String tokenConcat = " OR ";
122           String nextToken = st.nextToken();
123
124           if (nextToken.startsWith("+")) {
125             nextToken = nextToken.substring(1);
126             tokenConcat = " AND ";
127           }
128           if (nextToken.startsWith("-")) {
129             nextToken = nextToken.substring(1);
130             tokenConcat = " AND ";
131             notString = " NOT ";
132           }
133           if (firstToken == true) {
134             tokenConcat = "";
135             firstToken = false;
136           }
137
138
139           whereClause += tokenConcat + aField + notString + " like '";
140           whereClause += nextToken + "%'";
141         }
142         whereClause += ") ";
143       }
144       return theStorage.selectByWhereClause(whereClause, offset);
145     }
146     catch (StorageObjectFailure e){
147       throw new ModuleException(e.toString());
148     }
149
150   }
151 */
152 /*
153   public EntityList getContentByField(String aField, String aValue, String orderBy, int offset,
154                                       EntityUsers user) throws ModuleException
155   {
156     String whereClause = "lower("+aField + ") like lower('%" + JDBCStringRoutines.escapeStringLiteral(aValue) + "%')";
157     return getContent(whereClause, orderBy, offset, user);
158   }
159
160
161   public EntityList getContent(String whereClause, String orderBy, int offset,
162                                int limit, EntityUsers user) throws ModuleException {
163
164     try {
165       if (user!=null){
166         if (!user.isAdmin())
167           whereClause += " and to_publisher='" + user.getId()+"'";
168       }
169       return theStorage.selectByWhereClause(whereClause, orderBy, offset, limit);
170     }
171     catch (StorageObjectFailure e){     throw new ModuleException(e.toString()); }
172   }
173 */
174
175   public EntityList getContent(String whereClause, String orderBy,int offset, int limit) throws ModuleExc, ModuleFailure {
176     try {
177       return theStorage.selectByWhereClause(whereClause, orderBy, offset, limit);
178     }
179     catch (Throwable e){
180       throw new ModuleFailure(e);
181     }
182   }
183 /*
184   public EntityList getContent(String whereClause, String orderBy, int offset, EntityUsers user)
185       throws ModuleException
186   {
187     try {
188       if (whereClause !=null) {
189
190 // for the different article_types
191         if(whereClause.equals("newswire")) {
192           whereClause="is_published='1' and to_article_type='1'";
193           orderBy = "webdb_create desc";
194         }
195         if(whereClause.equals("feature")) {
196           whereClause="is_published='1' and to_article_type='2'";
197           orderBy = "webdb_create desc";
198         }
199         if(whereClause.equals("themenspecial")) {
200           whereClause="is_published='1' and to_article_type='3'";
201           orderBy = "webdb_create desc";
202         }
203         if(whereClause.equals("special")) {
204           whereClause="is_published='1' and to_article_type='4'";
205           orderBy = "webdb_create desc";
206         }
207
208         if(whereClause.equals("comments")) {
209           whereClause="not (comment is null or comment like '')";
210           orderBy = "webdb_lastchange desc";
211         }
212
213         if(whereClause.equals("nfrei")) {
214           whereClause="is_published='0'"; orderBy="webdb_create desc";
215         }
216
217         if(whereClause.equals("lastchange")) {
218           whereClause=""; orderBy="webdb_lastchange desc";
219         }
220
221         if(whereClause.equals("media")) {
222           return DatabaseContentToMedia.getInstance().getContent();
223         }
224       }
225       return theStorage.selectByWhereClause(whereClause, orderBy, offset);
226     }
227     catch (StorageObjectFailure e) {
228       throw new ModuleException(e.toString());
229     }
230   }
231 */
232 }
233
234