Changeset e86f30a573595eeada6b38bebb40261db4066669
- Timestamp:
- 06/24/07 14:36:32
(1 year ago)
- Author:
- Christopher Jung <bktheg@web.de>
- git-committer:
- Christopher Jung <bktheg@web.de> 1182688592 +0200
- git-parent:
[7d349d3ccae8a07bf0da35fb242dce99a9e3a806]
- git-author:
- Christopher Jung <bktheg@web.de> 1182688592 +0200
- Message:
Task in eine Entity umgebaut
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r66bf3c9 |
re86f30a |
|
| 19 | 19 | package net.driftingsouls.ds2.server.tasks; |
|---|
| 20 | 20 | |
|---|
| 21 | | import net.driftingsouls.ds2.server.framework.db.SQLResultRow; |
|---|
| | 21 | import javax.persistence.Column; |
|---|
| | 22 | import javax.persistence.Entity; |
|---|
| | 23 | import javax.persistence.Id; |
|---|
| | 24 | import javax.persistence.Table; |
|---|
| | 25 | |
|---|
| | 26 | import org.apache.commons.lang.math.RandomUtils; |
|---|
| | 27 | |
|---|
| | 28 | import net.driftingsouls.ds2.server.framework.Common; |
|---|
| 22 | 29 | |
|---|
| 23 | 30 | /** |
|---|
| … | … | |
| 26 | 33 | * |
|---|
| 27 | 34 | */ |
|---|
| | 35 | @Entity |
|---|
| | 36 | @Table(name="tasks") |
|---|
| 28 | 37 | public class Task { |
|---|
| | 38 | @Id() |
|---|
| | 39 | @Column(name="taskid") |
|---|
| 29 | 40 | private String taskID; |
|---|
| 30 | | private Taskmanager.Types type; |
|---|
| 31 | | private int time; |
|---|
| | 41 | private int type; |
|---|
| | 42 | private long time; |
|---|
| 32 | 43 | private int timeout; |
|---|
| 33 | | private String data1; |
|---|
| 34 | | private String data2; |
|---|
| 35 | | private String data3; |
|---|
| | 44 | private String data1 = ""; |
|---|
| | 45 | private String data2 = ""; |
|---|
| | 46 | private String data3 = ""; |
|---|
| 36 | 47 | |
|---|
| 37 | | protected Task( SQLResultRow task ) { |
|---|
| 38 | | taskID = task.getString("taskid"); |
|---|
| 39 | | time = task.getInt("time"); |
|---|
| 40 | | timeout = task.getInt("timeout"); |
|---|
| 41 | | data1 = task.getString("data1"); |
|---|
| 42 | | data2 = task.getString("data2"); |
|---|
| 43 | | data3 = task.getString("data3"); |
|---|
| 44 | | type = Taskmanager.Types.getTypeByID(task.getInt("type")); |
|---|
| 45 | | if( type == null ) { |
|---|
| 46 | | throw new RuntimeException("Unbekannter Task-Typ '"+task.getInt("type")+"'"); |
|---|
| 47 | | } |
|---|
| | 48 | /** |
|---|
| | 49 | * Konstruktor |
|---|
| | 50 | * |
|---|
| | 51 | */ |
|---|
| | 52 | public Task() { |
|---|
| | 53 | // EMPTY |
|---|
| | 54 | } |
|---|
| | 55 | |
|---|
| | 56 | /** |
|---|
| | 57 | * Erstellt eine neue Task |
|---|
| | 58 | * @param type Der Typ der Task |
|---|
| | 59 | */ |
|---|
| | 60 | public Task(Taskmanager.Types type) { |
|---|
| | 61 | this.taskID = Common.md5(""+RandomUtils.nextInt(Integer.MAX_VALUE))+Common.time(); |
|---|
| | 62 | this.type = type.ordinal(); |
|---|
| | 63 | this.time = Common.time(); |
|---|
| 48 | 64 | } |
|---|
| 49 | 65 | |
|---|
| … | … | |
| 55 | 71 | return data1; |
|---|
| 56 | 72 | } |
|---|
| | 73 | |
|---|
| | 74 | /** |
|---|
| | 75 | * Setzt den Inhalt des ersten Datenfelds |
|---|
| | 76 | * @param data Der Inhalt |
|---|
| | 77 | */ |
|---|
| | 78 | public void setData1(String data) { |
|---|
| | 79 | this.data1 = data; |
|---|
| | 80 | } |
|---|
| 57 | 81 | |
|---|
| 58 | 82 | /** |
|---|
| … | … | |
| 63 | 87 | return data2; |
|---|
| 64 | 88 | } |
|---|
| | 89 | |
|---|
| | 90 | /** |
|---|
| | 91 | * Setzt den Inhalt des zweiten Datenfelds |
|---|
| | 92 | * @param data Der Inhalt |
|---|
| | 93 | */ |
|---|
| | 94 | public void setData2(String data) { |
|---|
| | 95 | this.data1 = data; |
|---|
| | 96 | } |
|---|
| 65 | 97 | |
|---|
| 66 | 98 | /** |
|---|
| … | … | |
| 70 | 102 | public String getData3() { |
|---|
| 71 | 103 | return data3; |
|---|
| | 104 | } |
|---|
| | 105 | |
|---|
| | 106 | /** |
|---|
| | 107 | * Setzt den Inhalt des dritten Datenfelds |
|---|
| | 108 | * @param data Der Inhalt |
|---|
| | 109 | */ |
|---|
| | 110 | public void setData3(String data) { |
|---|
| | 111 | this.data1 = data; |
|---|
| 72 | 112 | } |
|---|
| 73 | 113 | |
|---|
| … | … | |
| 84 | 124 | * @return Die Timestamp des Erstellungszeitpunkts |
|---|
| 85 | 125 | */ |
|---|
| 86 | | public int getTime() { |
|---|
| | 126 | public long getTime() { |
|---|
| 87 | 127 | return time; |
|---|
| 88 | 128 | } |
|---|
| … | … | |
| 95 | 135 | return timeout; |
|---|
| 96 | 136 | } |
|---|
| | 137 | |
|---|
| | 138 | /** |
|---|
| | 139 | * Setzt den Timeout der Task in Ticks |
|---|
| | 140 | * @param timeout der Timeout |
|---|
| | 141 | */ |
|---|
| | 142 | public void setTimeout(int timeout) { |
|---|
| | 143 | this.timeout = timeout; |
|---|
| | 144 | } |
|---|
| 97 | 145 | |
|---|
| 98 | 146 | /** |
|---|
| … | … | |
| 101 | 149 | */ |
|---|
| 102 | 150 | public Taskmanager.Types getType() { |
|---|
| 103 | | return type; |
|---|
| | 151 | return Taskmanager.Types.getTypeByID(type); |
|---|
| 104 | 152 | } |
|---|
| 105 | 153 | } |
|---|
| r283e6c7 |
re86f30a |
|
| 20 | 20 | |
|---|
| 21 | 21 | import java.util.ArrayList; |
|---|
| | 22 | import java.util.Iterator; |
|---|
| 22 | 23 | import java.util.List; |
|---|
| 23 | 24 | |
|---|
| 24 | | import net.driftingsouls.ds2.server.framework.Common; |
|---|
| 25 | 25 | import net.driftingsouls.ds2.server.framework.ContextMap; |
|---|
| 26 | | import net.driftingsouls.ds2.server.framework.db.Database; |
|---|
| 27 | | import net.driftingsouls.ds2.server.framework.db.PreparedQuery; |
|---|
| 28 | | import net.driftingsouls.ds2.server.framework.db.SQLQuery; |
|---|
| 29 | | import net.driftingsouls.ds2.server.framework.db.SQLResultRow; |
|---|
| 30 | | |
|---|
| 31 | | import org.apache.commons.lang.math.RandomUtils; |
|---|
| | 26 | |
|---|
| | 27 | import org.hibernate.Query; |
|---|
| 32 | 28 | |
|---|
| 33 | 29 | /** |
|---|
| … | … | |
| 154 | 150 | */ |
|---|
| 155 | 151 | public String addTask( Types tasktype, int timeout, String data1, String data2, String data3 ) { |
|---|
| 156 | | String taskid = Common.md5(""+RandomUtils.nextInt(Integer.MAX_VALUE))+Common.time(); |
|---|
| 157 | | |
|---|
| 158 | | Database db = ContextMap.getContext().getDatabase(); |
|---|
| 159 | | |
|---|
| 160 | | db.prepare("INSERT INTO tasks ", |
|---|
| 161 | | "(`taskid`,`type`,`time`,`timeout`,`data1`,`data2`,`data3`) ", |
|---|
| 162 | | "VALUES", |
|---|
| 163 | | "( ?, ?, ?, ?, ?, ?, ?)") |
|---|
| 164 | | .update(taskid, tasktype.getTypeID(), Common.time(), timeout, data1, data2, data3); |
|---|
| 165 | | |
|---|
| 166 | | return taskid; |
|---|
| | 152 | Task task = new Task(tasktype); |
|---|
| | 153 | task.setTimeout(timeout); |
|---|
| | 154 | task.setData1(data1); |
|---|
| | 155 | task.setData2(data2); |
|---|
| | 156 | task.setData3(data3); |
|---|
| | 157 | |
|---|
| | 158 | org.hibernate.Session db = ContextMap.getContext().getDB(); |
|---|
| | 159 | db.persist(task); |
|---|
| | 160 | |
|---|
| | 161 | return task.getTaskID(); |
|---|
| 167 | 162 | } |
|---|
| 168 | 163 | |
|---|
| … | … | |
| 175 | 170 | */ |
|---|
| 176 | 171 | public void modifyTask( String taskid, String data1, String data2, String data3 ) { |
|---|
| 177 | | Database db = ContextMap.getContext().getDatabase(); |
|---|
| 178 | | |
|---|
| 179 | | db.prepare("UPDATE tasks SET ", |
|---|
| 180 | | "`data1`= ?,", |
|---|
| 181 | | "`data2`= ?,", |
|---|
| 182 | | "`data3`= ? ", |
|---|
| 183 | | "WHERE taskid= ? ") |
|---|
| 184 | | .update(data1, data2, data3, taskid); |
|---|
| | 172 | Task task = getTaskByID(taskid); |
|---|
| | 173 | task.setData1(data1); |
|---|
| | 174 | task.setData2(data2); |
|---|
| | 175 | task.setData3(data3); |
|---|
| 185 | 176 | } |
|---|
| 186 | 177 | |
|---|
| … | … | |
| 191 | 182 | */ |
|---|
| 192 | 183 | public void setTimeout( String taskid, int timeout ) { |
|---|
| 193 | | Database db = ContextMap.getContext().getDatabase(); |
|---|
| 194 | | db.prepare("UPDATE tasks SET ", |
|---|
| 195 | | "`timeout`= ? ", |
|---|
| 196 | | "WHERE taskid= ?") |
|---|
| 197 | | .update(taskid, timeout); |
|---|
| | 184 | Task task = getTaskByID(taskid); |
|---|
| | 185 | task.setTimeout(timeout); |
|---|
| 198 | 186 | } |
|---|
| 199 | 187 | |
|---|
| … | … | |
| 203 | 191 | */ |
|---|
| 204 | 192 | public void incTimeout( String taskid ) { |
|---|
| 205 | | Database db = ContextMap.getContext().getDatabase(); |
|---|
| 206 | | db.prepare("UPDATE tasks SET ", |
|---|
| 207 | | "`timeout`= `timeout`+1 ", |
|---|
| 208 | | "WHERE taskid= ?") |
|---|
| 209 | | .update(taskid); |
|---|
| | 193 | Task task = getTaskByID(taskid); |
|---|
| | 194 | task.setTimeout(task.getTimeout()+1); |
|---|
| 210 | 195 | } |
|---|
| 211 | 196 | |
|---|
| … | … | |
| 217 | 202 | */ |
|---|
| 218 | 203 | public Task getTaskByID( String taskid ) { |
|---|
| 219 | | Database db = ContextMap.getContext().getDatabase(); |
|---|
| 220 | | |
|---|
| 221 | | SQLResultRow task = db.prepare("SELECT * FROM tasks WHERE taskid= ?").first(taskid); |
|---|
| 222 | | if( !task.isEmpty() ) { |
|---|
| 223 | | return new Task(task); |
|---|
| 224 | | } |
|---|
| 225 | | return null; |
|---|
| | 204 | org.hibernate.Session db = ContextMap.getContext().getDB(); |
|---|
| | 205 | |
|---|
| | 206 | return (Task)db.get(Task.class, taskid); |
|---|
| 226 | 207 | } |
|---|
| 227 | 208 | |
|---|
| … | … | |
| 232 | 213 | */ |
|---|
| 233 | 214 | public Task[] getTasksByTimeout( int timeout ) { |
|---|
| 234 | | Database db = ContextMap.getContext().getDatabase(); |
|---|
| | 215 | org.hibernate.Session db = ContextMap.getContext().getDB(); |
|---|
| 235 | 216 | List<Task> resultlist = new ArrayList<Task>(); |
|---|
| 236 | 217 | |
|---|
| 237 | | SQLQuery atask = db.prepare("SELECT * FROM tasks WHERE timeout=? ORDER BY `time` ASC").query(timeout); |
|---|
| 238 | | while( atask.next() ) { |
|---|
| 239 | | resultlist.add(new Task(atask.getRow())); |
|---|
| 240 | | } |
|---|
| 241 | | atask.free(); |
|---|
| | 218 | List tasks = db.createQuery("from Task where timeout=? order by time asc") |
|---|
| | 219 | .setInteger(0, timeout) |
|---|
| | 220 | .list(); |
|---|
| | 221 | for( Iterator iter=tasks.iterator(); iter.hasNext(); ) { |
|---|
| | 222 | resultlist.add((Task)iter.next()); |
|---|
| | 223 | } |
|---|
| 242 | 224 | |
|---|
| 243 | 225 | return resultlist.toArray(new Task[resultlist.size()]); |
|---|
| … | … | |
| 254 | 236 | */ |
|---|
| 255 | 237 | public Task[] getTasksByData( Types type, String data1, String data2, String data3 ) { |
|---|
| 256 | | Database db = ContextMap.getContext().getDatabase(); |
|---|
| | 238 | org.hibernate.Session db = ContextMap.getContext().getDB(); |
|---|
| 257 | 239 | List<Task> resultlist = new ArrayList<Task>(); |
|---|
| 258 | 240 | |
|---|
| 259 | | String query = "SELECT * FROM tasks WHERE type=?"; |
|---|
| | 241 | String query = "from Task where type=?"; |
|---|
| 260 | 242 | if( !data1.equals("*") ) { |
|---|
| 261 | | query += " AND data1=?"; |
|---|
| | 243 | query += " and data1=?"; |
|---|
| 262 | 244 | } |
|---|
| 263 | 245 | if( !data2.equals("*") ) { |
|---|
| 264 | | query += " AND data2=?"; |
|---|
| | 246 | query += " and data2=?"; |
|---|
| 265 | 247 | } |
|---|
| 266 | 248 | if( !data3.equals("*")) { |
|---|
| 267 | | query += " AND data3=?"; |
|---|
| 268 | | } |
|---|
| 269 | | query += " ORDER BY `time` ASC"; |
|---|
| 270 | | PreparedQuery pq = db.prepare(query); |
|---|
| 271 | | int index=1; |
|---|
| 272 | | pq.setInt(index++, type.getTypeID()); |
|---|
| | 249 | query += " and data3=?"; |
|---|
| | 250 | } |
|---|
| | 251 | query += " order by time asc"; |
|---|
| | 252 | Query q = db.createQuery(query); |
|---|
| | 253 | int index=0; |
|---|
| | 254 | q.setInteger(index++, type.getTypeID()); |
|---|
| 273 | 255 | if( !data1.equals("*") ) { |
|---|
| 274 | | pq.setString(index++, data1); |
|---|
| | 256 | q.setString(index++, data1); |
|---|
| 275 | 257 | } |
|---|
| 276 | 258 | if( !data2.equals("*") ) { |
|---|
| 277 | | pq.setString(index++, data2); |
|---|
| | 259 | q.setString(index++, data2); |
|---|
| 278 | 260 | } |
|---|
| 279 | 261 | if( !data3.equals("*")) { |
|---|
| 280 | | query += " AND data3=?"; |
|---|
| 281 | | pq.setString(index++, data3); |
|---|
| 282 | | } |
|---|
| 283 | | |
|---|
| 284 | | SQLQuery atask = pq.query(); |
|---|
| 285 | | while( atask.next() ) { |
|---|
| 286 | | resultlist.add(new Task(atask.getRow())); |
|---|
| 287 | | } |
|---|
| 288 | | atask.free(); |
|---|
| | 262 | q.setString(index++, data3); |
|---|
| | 263 | } |
|---|
| | 264 | |
|---|
| | 265 | List tasks = q.list(); |
|---|
| | 266 | for( Iterator iter=tasks.iterator(); iter.hasNext(); ) { |
|---|
| | 267 | resultlist.add((Task)iter.next()); |
|---|
| | 268 | } |
|---|
| 289 | 269 | |
|---|
| 290 | 270 | return resultlist.toArray(new Task[resultlist.size()]); |
|---|
| … | … | |
| 311 | 291 | */ |
|---|
| 312 | 292 | public void removeTask( String taskid ) { |
|---|
| 313 | | Database db = ContextMap.getContext().getDatabase(); |
|---|
| 314 | | db.prepare("DELETE FROM tasks WHERE taskid= ?") |
|---|
| 315 | | .update(taskid); |
|---|
| | 293 | org.hibernate.Session db = ContextMap.getContext().getDB(); |
|---|
| | 294 | db.createQuery("delete from Task where taskid=?") |
|---|
| | 295 | .setString(0, taskid) |
|---|
| | 296 | .executeUpdate(); |
|---|
| 316 | 297 | } |
|---|
| 317 | 298 | |
|---|
| … | … | |
| 321 | 302 | */ |
|---|
| 322 | 303 | public void reduceTimeout( int step ) { |
|---|
| 323 | | Database db = ContextMap.getContext().getDatabase(); |
|---|
| 324 | | db.prepare("UPDATE tasks SET timeout=timeout-? WHERE timeout>?") |
|---|
| 325 | | .update(step, step-1); |
|---|
| | 304 | org.hibernate.Session db = ContextMap.getContext().getDB(); |
|---|
| | 305 | |
|---|
| | 306 | db.createQuery("update Task set timeout=timeout-? where timeout>?") |
|---|
| | 307 | .setInteger(0, step) |
|---|
| | 308 | .setInteger(1, step-1) |
|---|
| | 309 | .executeUpdate(); |
|---|
| 326 | 310 | |
|---|
| 327 | 311 | if( step > 1 ) { |
|---|
| 328 | | db.prepare("UPDATE tasks SET timeout=0 WHERE timeout<=?").update(step-1); |
|---|
| | 312 | db.createQuery("update Task set timeout=0 where timeout<=?") |
|---|
| | 313 | .setInteger(0, step-1) |
|---|
| | 314 | .executeUpdate(); |
|---|
| 329 | 315 | } |
|---|
| 330 | 316 | } |
|---|
| r1edfb5b |
re86f30a |
|
| 73 | 73 | <!-- survey_voted --> |
|---|
| 74 | 74 | <!-- surveys --> |
|---|
| 75 | | <!-- tasks [net.driftingsouls.ds2.server.tasks.Task migrieren] --> |
|---|
| | 75 | <mapping class="net.driftingsouls.ds2.server.tasks.Task" /> |
|---|
| 76 | 76 | <!-- transmissionen [net.driftingsouls.ds2.server.comm.PM umbauen] --> |
|---|
| 77 | 77 | <mapping class="net.driftingsouls.ds2.server.entities.User" /> |
|---|