first cut of merge of STABLE-pre1_0 into HEAD. I won't even guarantee that it
[mir.git] / source / mircoders / module / ModuleContent.java
1 package mircoders.module;
2
3 import java.io.*;
4 import java.util.*;
5 import java.sql.*;
6 import javax.servlet.*;
7 import javax.servlet.http.*;
8
9 import mir.servlet.*;
10 import mir.module.*;
11 import mir.entity.*;
12 import mir.misc.*;
13 import mir.storage.*;
14
15 import mircoders.entity.*;
16 import mircoders.storage.*;
17
18 /*
19  *  ContentObjekt -
20  *
21  *
22  * @author RK
23  */
24
25 public class ModuleContent extends AbstractModule
26 {
27         static Logfile     theLog;
28
29         public ModuleContent() {
30                 super();
31                 if (theLog == null) theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("Module.Content.Logfile"));
32         }
33
34         public ModuleContent(StorageObject theStorage) {
35                 this.theStorage = theStorage;
36                 if (theLog == null) theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("Module.Content.Logfile"));
37         }
38
39         //
40         // methoden, um an ContentEntities zu kommen
41
42   public EntityList getFeatures(int offset, int limit) throws ModuleException
43   {
44     return getContent("is_published=true AND to_article_type=2", "date desc, webdb_create desc",
45                       offset, limit);
46   }
47
48   public EntityList getNewsWire(int offset, int limit) throws ModuleException
49   {
50     return getContent("is_published=true AND to_article_type = 1",
51                                     "date desc, webdb_create desc",offset,limit);
52   }
53
54   public EntityList getStartArticle() throws ModuleException
55   {
56     EntityList returnList = getContent("is_published=true AND to_article_type=4",
57                                         "date desc, webdb_create desc",0,1);
58     //if no startspecial exists
59     if (returnList==null || returnList.size()==0)
60       returnList = getContent("is_published=true AND to_article_type=3",
61                               "date desc, webdb_create desc",0,1);
62
63     return returnList;
64   }
65
66         public EntityList getContent(HashMap searchValues, boolean concat, int offset, EntityUsers user)
67                 throws ModuleException {
68
69                 try {
70
71                         String whereClause ="", aField, aValue;
72                         boolean first = true;
73
74                         Set set = searchValues.keySet();
75                         Iterator it = set.iterator();
76                                                 for (int i=0;i<set.size();i++) {
77                         aField = (String)it.next();
78                         aValue = (String)searchValues.get(aField);
79
80                         if (first == false)
81                                 whereClause +=  (concat) ? " and " : " or ";
82                         else
83                                 first = false;
84
85                         whereClause += "(";
86
87                         // default: hier splitten der eintraege und verknupfung mit AND OR NOT
88                         StringTokenizer st = new StringTokenizer(aValue);
89                         boolean firstToken = true;
90                         while(st.hasMoreTokens()) {
91                                 String notString = "";
92                                 String tokenConcat = " OR ";
93                                 String nextToken = st.nextToken();
94
95                                 if (nextToken.startsWith("+")) {
96                                         nextToken = nextToken.substring(1);
97                                         tokenConcat = " AND ";
98                                 }
99                                 if (nextToken.startsWith("-")) {
100                                                 nextToken = nextToken.substring(1);
101                                                 tokenConcat = " AND ";
102                                                 notString = " NOT ";
103                                 }
104                                 if (firstToken == true) {
105                                         tokenConcat = "";
106                                         firstToken = false;
107                                 }
108
109
110                                 whereClause += tokenConcat + aField + notString + " like '";
111                                 whereClause += nextToken + "%'";
112                         }
113                         whereClause += ") ";
114                         }
115                         return theStorage.selectByWhereClause(whereClause, offset);
116         }
117         catch (StorageObjectException e){
118                         throw new ModuleException(e.toString());
119         }
120
121                 }
122
123         public EntityList getContentByField(String aField, String aValue, String orderBy, int offset,
124                                 EntityUsers user) throws ModuleException
125         {
126                 String whereClause = "lower("+aField + ") like lower('%" + StringUtil.quote(aValue) + "%')";
127                 return getContent(whereClause, orderBy, offset, user);
128         }
129
130         public EntityList getContent(String whereClause, String orderBy, int offset,
131     int limit, EntityUsers user) throws ModuleException {
132
133                 try {
134                         if (user!=null){
135                                 if (!user.isAdmin())
136                                         whereClause += " and to_publisher='" + user.getId()+"'";
137                                 }
138                                 return theStorage.selectByWhereClause(whereClause, orderBy, offset, limit);
139                         }
140                 catch (StorageObjectException e){       throw new ModuleException(e.toString()); }
141         }
142
143         public EntityList getContent(String whereClause, String orderBy,int offset, int limit)
144                 throws ModuleException {
145                 try {
146                         return theStorage.selectByWhereClause(whereClause, orderBy, offset, limit);
147                 } catch (StorageObjectException e){
148                         throw new ModuleException(e.toString());
149                 }
150         }
151
152         public EntityList getContent(String whereClause, String orderBy, int offset, EntityUsers user)
153                 throws ModuleException
154         {
155                 try {
156                         if (whereClause !=null) {
157
158                                 // for the different article_types
159                                 if(whereClause.equals("newswire")) {
160                                         whereClause="is_published='1' and to_article_type='1'";
161                                         orderBy = "webdb_create desc";
162                                 }
163                                 if(whereClause.equals("feature")) {
164                                         whereClause="is_published='1' and to_article_type='2'";
165                                         orderBy = "webdb_create desc";
166                                 }
167                                 if(whereClause.equals("themenspecial")) {
168                                         whereClause="is_published='1' and to_article_type='3'";
169                                         orderBy = "webdb_create desc";
170                                 }
171                                 if(whereClause.equals("special")) {
172                                         whereClause="is_published='1' and to_article_type='4'";
173                                         orderBy = "webdb_create desc";
174                                 }
175
176                                 if(whereClause.equals("comments")) {
177                                         whereClause="not (comment is null or comment like '')";
178                                         orderBy = "webdb_lastchange desc";
179                                 }
180
181                                 if(whereClause.equals("nfrei")) {
182                                         whereClause="is_published='0'"; orderBy="date desc";
183                                 }
184
185                                 if(whereClause.equals("lastchange")) {
186                                         whereClause=""; orderBy="webdb_lastchange desc";
187                                 }
188
189                                 if(whereClause.equals("media")) {
190                                         return DatabaseContentToMedia.getInstance().getContent();
191                                 }
192                         }
193                         return theStorage.selectByWhereClause(whereClause, orderBy, offset);
194                 }
195                 catch (StorageObjectException e){       throw new ModuleException(e.toString()); }
196         }
197
198
199 }
200
201