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