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