rebuilding head
[mir.git] / source / mircoders / module / ModuleContent.java
index e3cb48f..d3877d5 100755 (executable)
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  * In addition, as a special exception, The Mir-coders gives permission to link
- * the code of this program with  any library licensed under the Apache Software License, 
- * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library 
- * (or with modified versions of the above that use the same license as the above), 
- * and distribute linked combinations including the two.  You must obey the 
- * GNU General Public License in all respects for all of the code used other than 
- * the above mentioned libraries.  If you modify this file, you may extend this 
- * exception to your version of the file, but you are not obligated to do so.  
+ * the code of this program with  any library licensed under the Apache Software License,
+ * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
+ * (or with modified versions of the above that use the same license as the above),
+ * and distribute linked combinations including the two.  You must obey the
+ * GNU General Public License in all respects for all of the code used other than
+ * the above mentioned libraries.  If you modify this file, you may extend this
+ * exception to your version of the file, but you are not obligated to do so.
  * If you do not wish to do so, delete this exception statement from your version.
  */
 
 package mircoders.module;
 
-import mir.entity.EntityList;
 import mir.log.LoggerWrapper;
 import mir.module.AbstractModule;
-import mir.module.ModuleExc;
-import mir.module.ModuleFailure;
-import mir.storage.StorageObject;
+import mir.util.JDBCStringRoutines;
+import mircoders.global.MirGlobal;
+import mircoders.storage.DatabaseContent;
 
 /**
  *
@@ -52,19 +51,84 @@ public class ModuleContent extends AbstractModule
   static LoggerWrapper logger = new LoggerWrapper("Module.Content");
 
   public ModuleContent() {
-    super();
+    super(DatabaseContent.getInstance());
   }
 
-  public ModuleContent(StorageObject theStorage) {
-    this.theStorage = theStorage;
+  public void expireArticleLock(String anId, String aUserId) {
+    if (!MirGlobal.isUserLoggedIn(anId)) {
+      unlockArticle(anId, aUserId, false);
+    }
   }
 
-  public EntityList getContent(String whereClause, String orderBy,int offset, int limit) throws ModuleExc, ModuleFailure {
+  /**
+   * Locks an article. Forced (that is, without checking wether it's already locked)
+   * if necessary.
+   *
+   * @param anId The article number
+   * @param aUserId The locking user
+   * @param aForce Should it e forced?
+   * @return
+   */
+  public boolean lockArticle(String anId, String aUserId, boolean aForce) {
     try {
-      return theStorage.selectByWhereClause(whereClause, orderBy, offset, limit);
+      String query =
+          "update content set to_locking_user=" + JDBCStringRoutines.escapeStringLiteral(aUserId) +
+          "  where id = " + JDBCStringRoutines.escapeStringLiteral(anId);
+
+      if (!aForce)
+        query = query + " and to_locking_user is null";
+
+      return storage.executeUpdate(query) > 0;
+    }
+    catch (Throwable t) {
+      return false;
+    }
+  }
+
+  /**
+   * Unlocks an article. Forced (that is, without checking wether it's locked by
+   *     the person that unlocks it) if necessary.
+   *
+   * @param anId The article number
+   * @param aUserId The user that unlocks
+   * @param aForce Should it e forced?
+   * @return <code>true</code> if successfull, <code>false</code> if not
+   */
+  public boolean unlockArticle(String anId, String aUserId, boolean aForce)  {
+    try {
+      String query =
+          "update content set to_locking_user=null" +
+          "  where id = " + JDBCStringRoutines.escapeStringLiteral(anId);
+
+      if (!aForce)
+        query = query + " and to_locking_user = "+JDBCStringRoutines.escapeStringLiteral(aUserId);
+
+      return storage.executeUpdate(query) > 0;
+    }
+    catch (Throwable t) {
+      return false;
+    }
+  }
+
+  /**
+   * Returns the id of the locking user of an article. <code>null</code> otherwise.
+   *
+   * @param anId
+   * @return
+   */
+  public String queryArticleLock(String anId)  {
+    try {
+      String result = storage.executeFreeSingleValueSql("select to_locking_user from content where id = " + JDBCStringRoutines.escapeStringLiteral(anId));
+
+      if (result!=null && !MirGlobal.isUserLoggedIn(result)) {
+        expireArticleLock(anId, result);
+        result = null;
+      }
+
+      return result;
     }
-    catch (Throwable e){
-      throw new ModuleFailure(e);
+    catch (Throwable t) {
+      return null;
     }
   }
 }