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