Codestructure, first few lines
[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
43         public EntityList getContent(HashMap searchValues, boolean concat, int offset, EntityUsers user)
44                 throws ModuleException {
45
46                 try {
47
48                         String whereClause ="", aField, aValue;
49                         boolean first = true;
50
51                         Set set = searchValues.keySet();
52                         Iterator it = set.iterator();
53                                                 for (int i=0;i<set.size();i++) {
54                         aField = (String)it.next();
55                         aValue = (String)searchValues.get(aField);
56
57                         if (first == false)
58                                 whereClause +=  (concat) ? " and " : " or ";
59                         else
60                                 first = false;
61
62                         whereClause += "(";
63
64                         // default: hier splitten der eintraege und verknupfung mit AND OR NOT
65                         StringTokenizer st = new StringTokenizer(aValue);
66                         boolean firstToken = true;
67                         while(st.hasMoreTokens()) {
68                                 String notString = "";
69                                 String tokenConcat = " OR ";
70                                 String nextToken = st.nextToken();
71
72                                 if (nextToken.startsWith("+")) {
73                                         nextToken = nextToken.substring(1);
74                                         tokenConcat = " AND ";
75                                 }
76                                 if (nextToken.startsWith("-")) {
77                                                 nextToken = nextToken.substring(1);
78                                                 tokenConcat = " AND ";
79                                                 notString = " NOT ";
80                                 }
81                                 if (firstToken == true) {
82                                         tokenConcat = "";
83                                         firstToken = false;
84                                 }
85
86
87                                 whereClause += tokenConcat + aField + notString + " like '";
88                                 whereClause += nextToken + "%'";
89                         }
90                         whereClause += ") ";
91                         }
92                         return theStorage.selectByWhereClause(whereClause, offset);
93         }
94         catch (StorageObjectException e){
95                         throw new ModuleException(e.toString());
96         }
97
98                 }
99
100         public EntityList getContentByField(String aField, String aValue, String orderBy, int offset,
101                                 EntityUsers user) throws ModuleException
102         {
103                 String whereClause = "lower("+aField + ") like lower('%" + StringUtil.quote(aValue) + "%')";
104                 return getContent(whereClause, orderBy, offset, user);
105         }
106
107         public EntityList getContent(String whereClause, String orderBy, int offset, int limit, EntityGruppen user)
108                 throws ModuleException {
109
110                 try {
111                         if (user!=null){
112                         //theLog.printInfo("user!=null admin: "+ user.getValue("admin"));
113                                 if (user.getValue("is_admin").equals("0"))
114                                         whereClause += " and to_publisher='" + user.getId()+"'";
115                                 }
116                                 return theStorage.selectByWhereClause(whereClause, orderBy, offset, limit);
117                         }
118                 catch (StorageObjectException e){       throw new ModuleException(e.toString()); }
119         }
120
121         public EntityList getContent(String whereClause, String orderBy,int offset, int limit)
122                 throws ModuleException {
123                 try {
124                         return theStorage.selectByWhereClause(whereClause, orderBy, offset, limit);
125                 } catch (StorageObjectException e){
126                         throw new ModuleException(e.toString());
127                 }
128         }
129
130         public EntityList getContent(String whereClause, String orderBy, int offset, EntityUsers user)
131                 throws ModuleException
132         {
133                 try {
134                         if (whereClause !=null) {
135
136                                 // for the different article_types
137                                 if(whereClause.equals("newswire")) {
138                                         whereClause="is_published='1' and to_article_type='1'";
139                                         orderBy = "webdb_create desc";
140                                 }
141                                 if(whereClause.equals("feature")) {
142                                         whereClause="is_published='1' and to_article_type='2'";
143                                         orderBy = "webdb_create desc";
144                                 }
145                                 if(whereClause.equals("themenspecial")) {
146                                         whereClause="is_published='1' and to_article_type='3'";
147                                         orderBy = "webdb_create desc";
148                                 }
149                                 if(whereClause.equals("special")) {
150                                         whereClause="is_published='1' and to_article_type='4'";
151                                         orderBy = "webdb_create desc";
152                                 }
153
154                                 if(whereClause.equals("comments")) {
155                                         whereClause="not (comment is null or comment like '')";
156                                         orderBy = "webdb_lastchange desc";
157                                 }
158
159                                 if(whereClause.equals("nfrei")) {
160                                         whereClause="is_published='0'"; orderBy="date desc";
161                                 }
162
163                                 if(whereClause.equals("lastchange")) {
164                                         whereClause=""; orderBy="webdb_lastchange desc";
165                                 }
166
167                                 if(whereClause.equals("media")) {
168                                         return DatabaseContentToMedia.getInstance().getContent();
169                                 }
170                         }
171                         return theStorage.selectByWhereClause(whereClause, orderBy, offset);
172                 }
173                 catch (StorageObjectException e){       throw new ModuleException(e.toString()); }
174         }
175
176
177 }
178
179