1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
package YalpInputs.YalpPGSqlInput;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.*;
import java.sql.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import YalpInterfaces.*;
public class YalpInputPluginImpl extends InputPluginInterfacePOA {
private String db;
private String dbuser;
private String dbpasswd;
private Statement stat;
private Connection con;
private PluginInfo pluginInfo;
private String log4jFile = "log4j_input_plugin.conf";
private static Logger logger =
Logger.getLogger("Yalp.InputPlugins.PGSqlInput.YalpInputPluginImpl");
private ORB orb;
public YalpInputPluginImpl()
{
PropertyConfigurator.configureAndWatch(log4jFile);
logger.debug("YalpInputPluginImpl()");
}
public void setInfo(PluginInfo info)
{
logger.debug("interfaceImpl - setInfo(): "+info.id);
pluginInfo = info;
}
public void setORB(ORB _orb)
{
orb = _orb;
String db = "jdbc:postgresql://localhost:5432/yalp";
String dbYalpUser = "yalp";
String dbPasswd = "yalp";
try{
dbuser = dbYalpUser;
dbpasswd = dbPasswd;
Class.forName("org.postgresql.Driver");
con = DriverManager.getConnection(db,dbuser,dbpasswd);
System.out.println("YalpPGSqlInput: db connection established");
stat= con.createStatement();
} catch (SQLException e) {
System.out.println("Exception in PGSqlInput Constructor: "+e);
} catch (ClassNotFoundException e) {
System.out.println("Exception in PGSqlInput Constructor: "+e);
}
}
/*
* cuts Strings and returns an ArrayList of the cutted Strings
*
* @param str
* String to cut
* @return ArrayList<String>
* ArrayList with cutted Strings
*/
private ArrayList <String> stringCut (String str) {
int i=0,j=0;
ArrayList<String> list= new ArrayList<String>();
while( j != -1 ) {
j=str.indexOf(" ", i);
if (j!=-1) {
list.add(new String (str.substring(i,j)));
i=j+1;
} else
list.add( new String( str.substring( i, str.length() ) ) );
}
return list;
}
public void search( String str, MediaType[] types, MediasHolder result,
YalpErrorHolder err) {
logger.debug("search("+str+")");
YalpError error = new YalpError();
error.descr = "";
error.msg = "";
error.level = YalpErrorLevel.ERROR_LEVEL_INFO;
error.code = YalpErrorCode.OK;
err.value = error;
ArrayList <String> searchWords = stringCut(str);
String query = new String();
Boolean first, doIntersect, sound, video, image;
doIntersect = false;
sound = false;
video = false;
image = false;
for( int i = 0; i < types.length; i++ )
{
switch( types[i].value() )
{
case MediaType._SOUND:
sound = true;
break;
case MediaType._VIDEO:
video = true;
break;
case MediaType._IMAGE:
image = true;
break;
}
}
for( String pattern : searchWords ) {
first = true;
if( doIntersect )
query = query+") intersect ";
else
doIntersect = true;
query += "select * from \"Medias\" where (";
if( sound ) {
first = false;
query += "\"type\" = 'a' ";
}
if( image ) {
if( first )
first = false;
else
query += "or ";
query += "\"type\" = 'i' ";
}
if( video ) {
if( first )
first = false;
else
query += "or ";
query += "\"type\" = 'v' ";
}
if( !first )
query += ") and (";
query += "\"name\" Ilike '%";
query += pattern;
query += "%' or \"tags\" Ilike '%";
query += pattern;
/*
query += "%' or \"album\" Ilike '%";
query += pattern;
query += "%'or \"year\" Ilike '%";
query += pattern;
query += "%'or \"category\" Ilike '%";
query += pattern;
*/
query += "%'";
}
query += ")order by \"id\";";
logger.debug("sending SQL request: "+query);
ArrayList<Media> foundMedias = new ArrayList<Media>();
try {
Statement stat = con.createStatement();
ResultSet sqlResult = stat.executeQuery(query);
System.out.println("found: ");
MediaType type;
while( sqlResult.next() ) {
System.out.print(sqlResult.getString(3)+": ");
System.out.print(sqlResult.getString(4)+"\t| ");
System.out.println(sqlResult.getString(6));
switch( sqlResult.getString(2).charAt(0) ) {
case 's':
type = MediaType.SOUND;
break;
case 'v':
type = MediaType.VIDEO;
break;
case 'i':
type = MediaType.IMAGE;
break;
default:
type = MediaType.OTHER;
}
StringProperty[] strProp = new StringProperty[1];
strProp[0] = new StringProperty("","");
IntProperty[] intProp = new IntProperty[1];
intProp[0] = new IntProperty("", 0);
String[] tags = new String[1];
tags[0] = "blubb";
YalpUser owner = new YalpUser( 0, // user id
"huhu", // name
"manut", // real name
AccessRights.ADMIN );
Media newMedia = new Media( sqlResult.getString(6),
sqlResult.getInt(1),
type,
pluginInfo.id,
owner,
"", // last edit
sqlResult.getString(3),
sqlResult.getString(4),
sqlResult.getString(5),
strProp,
intProp,
tags );
foundMedias.add(newMedia);
logger.debug( sqlResult.getString(6) + "added to foundMedias");
}
} catch( SQLException e ) {
System.out.println("Exception in PGSqlInput.search: "+e);
}
Media[] m = new Media[foundMedias.size()];
result.value = foundMedias.toArray(m);
logger.debug("psql search done");
}
// == REIMPLEMENTATION NEEDED : == //
/*
* submits changes to yalpMediaDatabase
*
* @param change
* describes the change to commit
* @return int
* -1 if failed
*/
public void changeMedia (Media toChange, Action todo, YalpErrorHolder err) {
/* t.b.d. alter this to new database design
try{
String sql1,sql2,sql3;
int Return;
switch (change.updateType){
// if updateType is UPDATE
case UPDATE:
sql1="update \"medien\" set \"type\"='"+change.type+"', \"title\"='"+change.title+"', \"author\"='"+change.author+"', \"album\"='"+change.album+"', \"category\"='"+change.category+"', \"year\"='"+change.year+"', \"duration\"='"+change.duration+"', \"aBitrate\"="+change.aBitrate+", \"vBitrate\"="+change.vBitrate+", \"resolution\"='"+change.resolution+"', \"lastEdit\"='"+new java.util.Date(System.currentTimeMillis()).toString()+"' where \"id\"= "+change.id+" ;";
System.out.println(sql1);
Return=stat.executeUpdate(sql1);
break;
// if updateType is INSERT INTO
case INSERT:
sql1="insert into \"medien\" values(nextval('medienId'),'"+change.type+"','"+change.title+"','"+change.author+"','"+change.album+"','"+change.category+"','"+change.year+"','"+change.duration+"',"+change.aBitrate+","+change.vBitrate+",'"+change.resolution+"',"+change.ownerId+",'"+new java.util.Date(System.currentTimeMillis()).toString()+"','"+change.path+"','"+change.name+"');";
System.out.println(sql1);
Return=stat.executeUpdate(sql1);
break;
// if updateType is DELETE
case DELETE:
sql1="delete from \"medien\" where \"id\"= "+change.id+" ;";
System.out.println(sql1);
Return=stat.executeUpdate(sql1);
break;
default : return -1;
}
// perform operation on table an return number of updated rows
}catch(SQLException e){
System.out.println("Exception in PGSqlInput.changeMedia: "+e);
}
*/
}
/*
* returns number of medias in database
*
* @return String
* Number of medias in database
*/
public void getNumOfMedias(IntHolder num, YalpErrorHolder err) {
try{
ResultSet result=this.stat.executeQuery("select count (\"id\") from \"medien\";");
result.next();
num = new IntHolder(new Integer(result.getString(1)));
}catch(SQLException e){
System.out.println("exception in PGSql Input getNumOfMedias: "+e);
YalpError error = new YalpError();
error.code = YalpErrorCode.ERROR_SQL;
error.level= YalpErrorLevel.ERROR_LEVEL_ERROR;
error.descr= e.toString();
err = new YalpErrorHolder(error);
}
}
/*
* returns plugin information
* @param PluginInfoHolder info holder for PluginInformation
* @param YalpErrorHolder err holder for error information
*/
public void getInfo(PluginInfoHolder info, YalpErrorHolder err)
{
info = new PluginInfoHolder(pluginInfo);
}
}
|