small fixes yet again
[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.io.*;
35 import java.util.*;
36 import java.sql.*;
37
38 import javax.servlet.*;
39 import javax.servlet.http.*;
40
41 import mir.servlet.*;
42 import mir.module.*;
43 import mir.entity.*;
44 import mir.misc.*;
45 import mir.util.*;
46 import mir.storage.*;
47 import mir.log.*;
48
49 import mircoders.entity.*;
50 import mircoders.storage.*;
51
52 /*
53  *  ContentObjekt -
54  *
55  * @version $Id: ModuleContent.java,v 1.13 2002/12/14 01:37:43 zapata Exp $
56  *
57  * @author RK, mir-coders
58  *
59  */
60
61 public class ModuleContent extends AbstractModule
62 {
63   static LoggerWrapper logger = new LoggerWrapper("Module.Content");
64
65   public ModuleContent() {
66     super();
67   }
68
69   public ModuleContent(StorageObject theStorage) {
70     this.theStorage = theStorage;
71   }
72
73 //
74 // various methods to retrieve content entities
75
76   public EntityList getFeatures(int offset, int limit) throws ModuleException
77   {
78     return getContent("is_published=true AND to_article_type=2", "webdb_create desc",
79                       offset, limit);
80   }
81
82   public EntityList getNewsWire(int offset, int limit) throws ModuleException
83   {
84     return getContent("is_published=true AND to_article_type = 1",
85                       "webdb_create desc",offset,limit);
86   }
87
88   public EntityList getStartArticle() throws ModuleException
89   {
90     EntityList returnList = getContent("is_published=true AND to_article_type=4",
91                                        "webdb_create desc",0,1);
92 //if no startspecial exists
93     if (returnList==null || returnList.size()==0)
94       returnList = getContent("is_published=true AND to_article_type=3",
95                               "webdb_create desc",0,1);
96
97     return returnList;
98   }
99
100   public EntityList getContent(HashMap searchValues, boolean concat, int offset, EntityUsers user)
101       throws ModuleException {
102
103     try {
104
105       String whereClause ="", aField, aValue;
106       boolean first = true;
107
108       Set set = searchValues.keySet();
109       Iterator it = set.iterator();
110       for (int i=0;i<set.size();i++) {
111         aField = (String)it.next();
112         aValue = (String)searchValues.get(aField);
113
114         if (first == false)
115           whereClause +=  (concat) ? " and " : " or ";
116         else
117           first = false;
118
119         whereClause += "(";
120
121 // default: hier splitten der eintraege und verknupfung mit AND OR NOT
122         StringTokenizer st = new StringTokenizer(aValue);
123         boolean firstToken = true;
124         while(st.hasMoreTokens()) {
125           String notString = "";
126           String tokenConcat = " OR ";
127           String nextToken = st.nextToken();
128
129           if (nextToken.startsWith("+")) {
130             nextToken = nextToken.substring(1);
131             tokenConcat = " AND ";
132           }
133           if (nextToken.startsWith("-")) {
134             nextToken = nextToken.substring(1);
135             tokenConcat = " AND ";
136             notString = " NOT ";
137           }
138           if (firstToken == true) {
139             tokenConcat = "";
140             firstToken = false;
141           }
142
143
144           whereClause += tokenConcat + aField + notString + " like '";
145           whereClause += nextToken + "%'";
146         }
147         whereClause += ") ";
148       }
149       return theStorage.selectByWhereClause(whereClause, offset);
150     }
151     catch (StorageObjectException e){
152       throw new ModuleException(e.toString());
153     }
154
155   }
156
157   public EntityList getContentByField(String aField, String aValue, String orderBy, int offset,
158                                       EntityUsers user) throws ModuleException
159   {
160     String whereClause = "lower("+aField + ") like lower('%" + JDBCStringRoutines.escapeStringLiteral(aValue) + "%')";
161     return getContent(whereClause, orderBy, offset, user);
162   }
163
164   public EntityList getContent(String whereClause, String orderBy, int offset,
165                                int limit, EntityUsers user) throws ModuleException {
166
167     try {
168       if (user!=null){
169         if (!user.isAdmin())
170           whereClause += " and to_publisher='" + user.getId()+"'";
171       }
172       return theStorage.selectByWhereClause(whereClause, orderBy, offset, limit);
173     }
174     catch (StorageObjectException e){   throw new ModuleException(e.toString()); }
175   }
176
177   public EntityList getContent(String whereClause, String orderBy,int offset, int limit)
178       throws ModuleException {
179     try {
180       return theStorage.selectByWhereClause(whereClause, orderBy, offset, limit);
181     } catch (StorageObjectException e){
182       throw new ModuleException(e.toString());
183     }
184   }
185
186   public EntityList getContent(String whereClause, String orderBy, int offset, EntityUsers user)
187       throws ModuleException
188   {
189     try {
190       if (whereClause !=null) {
191
192 // for the different article_types
193         if(whereClause.equals("newswire")) {
194           whereClause="is_published='1' and to_article_type='1'";
195           orderBy = "webdb_create desc";
196         }
197         if(whereClause.equals("feature")) {
198           whereClause="is_published='1' and to_article_type='2'";
199           orderBy = "webdb_create desc";
200         }
201         if(whereClause.equals("themenspecial")) {
202           whereClause="is_published='1' and to_article_type='3'";
203           orderBy = "webdb_create desc";
204         }
205         if(whereClause.equals("special")) {
206           whereClause="is_published='1' and to_article_type='4'";
207           orderBy = "webdb_create desc";
208         }
209
210         if(whereClause.equals("comments")) {
211           whereClause="not (comment is null or comment like '')";
212           orderBy = "webdb_lastchange desc";
213         }
214
215         if(whereClause.equals("nfrei")) {
216           whereClause="is_published='0'"; orderBy="webdb_create desc";
217         }
218
219         if(whereClause.equals("lastchange")) {
220           whereClause=""; orderBy="webdb_lastchange desc";
221         }
222
223         if(whereClause.equals("media")) {
224           return DatabaseContentToMedia.getInstance().getContent();
225         }
226       }
227       return theStorage.selectByWhereClause(whereClause, orderBy, offset);
228     }
229     catch (StorageObjectException e) {
230       throw new ModuleException(e.toString());
231     }
232   }
233 }
234
235