KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cofax > cms > CofaxToolsLockUnlock


1 /*
2  * CofaxToolsLockUnlock is part of the Cofax content management system library.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Please see http://www.cofax.org for contact information and other related informaion.
19  *
20  * $Header: /cvsroot/cofax/cofax/src/org/cofax/cms/CofaxToolsLockUnlock.java,v 1.4.2.1 2006/12/11 16:28:24 fxrobin Exp $
21  */

22
23 package org.cofax.cms;
24
25 import org.cofax.*;
26 import java.util.*;
27
28 /**
29  * CofaxToolsLockUnlock This module is used to lock and unlock articles by
30  * authors
31  *
32  * @author - Badr Chentouf - Smile - www.smile.fr
33  */

34
35 public class CofaxToolsLockUnlock {
36
37     /*
38      * this must be done before launch the database must me initialized with the
39      * following script
40      *
41      * create table tblarticlelock ( itemID int(11) not null, userID int(11) not
42      * null, lockDate datetime not null);
43      *
44      * insert into tblpermmodes (modes,type) select 'unlock_article' as ua,
45      * groupTypeID from tblpermgrouptype ;
46      */

47
48     /**
49      * Returns the list of articles locked by the current user
50      *
51      */

52     public static String JavaDoc getMyArticlesLocked(DataStore db, String JavaDoc pubname, String JavaDoc userID) {
53         StringBuffer JavaDoc listMyArticlesLocked = new StringBuffer JavaDoc("");
54         try {
55             if ((userID != null) && !(userID.equals(""))) {
56                 String JavaDoc tag = "select AL.itemID, A.workflow_state, A.headline from tblarticlelock AS AL, tblarticles as A ";
57                 tag += "where A.itemID=AL.itemID and AL.userID='" + userID + "' and A.pubName='" + pubname + "' ";
58                 tag += "and lockDate >= DATE_ADD(Now(), INTERVAL -30 minute)";
59                 HashMap htTmp = new HashMap();
60                 List lstUsers = CofaxToolsDbUtils.getPackageData(db, htTmp, tag);
61                 if (lstUsers.size() <= 0) {
62                     listMyArticlesLocked.append(CofaxToolsUtil.getI18NMessage(CofaxToolsServlet.lcl, "myaccount_noarticle") + "<br>\n");
63                 }
64                 while (lstUsers.size() > 0) {
65                     StringBuffer JavaDoc sbTemp = new StringBuffer JavaDoc();
66                     HashMap hashInfo = (HashMap) lstUsers.get(0);
67                     String JavaDoc itemID = (String JavaDoc) hashInfo.get("ITEMID");
68                     String JavaDoc headline = (String JavaDoc) hashInfo.get("HEADLINE");
69                     String JavaDoc validated = (String JavaDoc) hashInfo.get("WORKFLOW_STATE");
70                     String JavaDoc state = "";
71                     if (validated.equals("0"))
72                         state = "<font color='silver'>";
73                     else if (validated.equals("1"))
74                         state = "<font color='#C1C307'>";
75                     else if (validated.equals("2"))
76                         state = "<font color='#0066CC'>";
77                     listMyArticlesLocked.append("&gt;&nbsp;<a HREF='?mode=article_edit_article_by_itemID&ITEMID=" + itemID + "&hl=article_create_article'>"
78                             + state + headline + "</font></a>&nbsp;&nbsp;");
79                     listMyArticlesLocked.append("[<a HREF='?mode=unlock_article&ITEMID=" + itemID + "'>"
80                             + CofaxToolsUtil.getI18NMessage(CofaxToolsServlet.lcl, "myaccount_freearticle") + "</a>]<br>");
81                     lstUsers.remove(0);
82                 }
83             }
84         } catch (Exception JavaDoc e) {
85             listMyArticlesLocked.append("An error occured<br>\n");
86         }
87         return (listMyArticlesLocked.toString());
88     }
89
90     /**
91      * Lock an article to not allow to other users to edit the same article at
92      * the same time
93      *
94      * @param db
95      * @param userID
96      * The userID from the user logged
97      * @param itemID
98      * The itemID of the article edited
99      * @return 0=error or 1=success
100      */

101     public static int lockArticle(DataStore db, String JavaDoc userID, String JavaDoc itemID) {
102         // test the input values
103
if ((itemID == null) || (itemID.equals("0")) || (userID == null) || (userID.equals("0"))) {
104             return 0;
105         }
106
107         // check if the article is not already locked by another user
108
// normally, it can't
109
HashMap ht = new HashMap();
110         StringBuffer JavaDoc v = new StringBuffer JavaDoc();
111         v.append("SELECT itemID FROM tblarticlelock WHERE itemID=" + itemID + " and userID<>" + userID);
112         v.append(" and lockDate >= DATE_ADD(Now(), INTERVAL -30 minute)");
113         Vector u_articles = CofaxToolsDbUtils.getPackageVector(db, ht, v.toString());
114         if (u_articles.size() > 0 && u_articles != null) {
115             return (0);
116         }
117
118         // lock the article in the database
119
StringBuffer JavaDoc v_lock = new StringBuffer JavaDoc();
120         v_lock.append("delete from tblarticlelock where itemID=" + itemID + " and userID=" + userID);
121         List lock = CofaxToolsDbUtils.getPackageData(db, ht, v_lock.toString());
122
123         StringBuffer JavaDoc v_lock2 = new StringBuffer JavaDoc();
124         v_lock2.append("insert into tblarticlelock(itemID, userID, lockDate) ");
125         v_lock2.append("values(" + itemID + ", " + userID + ", now())");
126         List lock2 = CofaxToolsDbUtils.getPackageData(db, ht, v_lock2.toString());
127
128         return 1;
129
130     }
131
132     /**
133      * Unlock an article
134      *
135      * @param db
136      * @param userID
137      * The userID from the user logged
138      * @param itemID
139      * The itemID of the article edited
140      * @return 0=error or 1=success
141      */

142     public static int unlockArticle(DataStore db, String JavaDoc userID, String JavaDoc itemID) {
143         // test the input values
144
if ((itemID == null) || (itemID.equals("0")) || (userID == null) || (userID.equals("0"))) {
145             return 0;
146         }
147
148         // unlock the article in the database
149
HashMap ht = new HashMap();
150         StringBuffer JavaDoc v_lock = new StringBuffer JavaDoc();
151         v_lock.append("delete from tblarticlelock ");
152         v_lock.append("where itemID=" + itemID + " ");
153         v_lock.append("and userID=" + userID + " ");
154         List lock = CofaxToolsDbUtils.getPackageData(db, ht, v_lock.toString());
155
156         return 1;
157
158     }
159
160     /**
161      * Unlock an article
162      *
163      * @param db
164      * @param userID
165      * The userID from the user logged
166      * @param itemID
167      * The itemID of the article edited
168      * @return 0=error or 1=success
169      */

170     public static int unlockArticlesByUserID(DataStore db, String JavaDoc userID) {
171         // test the input values
172
if ((userID == null) || (userID.equals("0"))) {
173             return 0;
174         }
175
176         // unlock the article in the database for a userID
177
HashMap ht = new HashMap();
178         StringBuffer JavaDoc v_lock = new StringBuffer JavaDoc();
179         v_lock.append("delete from tblarticlelock ");
180         v_lock.append("where userID=" + userID + " ");
181         List lock = CofaxToolsDbUtils.getPackageData(db, ht, v_lock.toString());
182
183         return 1;
184
185     }
186
187     /**
188      * Unlock an article
189      *
190      * @param db
191      * @param userID
192      * The userID from the user logged
193      * @param itemID
194      * The itemID of the article edited
195      * @return 0=error or 1=success
196      */

197     public static int testLockArticle(DataStore db, String JavaDoc userID, String JavaDoc itemID) {
198         // test the input values
199
if ((itemID == null) || (itemID.equals("0")) || (userID == null) || (userID.equals("0"))) {
200             return (1);
201         }
202
203         // check if the article is not already locked by another user
204
HashMap ht = new HashMap();
205         StringBuffer JavaDoc v = new StringBuffer JavaDoc();
206         v.append("SELECT userID FROM tblarticlelock WHERE itemID=" + itemID + " and userID<>" + userID);
207         v.append(" and lockDate >= DATE_ADD(Now(), INTERVAL -30 minute)");
208         Vector u_articles = CofaxToolsDbUtils.getPackageVector(db, ht, v.toString());
209         if (u_articles.size() > 0 && u_articles != null) {
210             return (0);
211         }
212
213         return (1);
214     }
215
216     /**
217      * Returns the mail of the user who's locking an article
218      *
219      * @param db
220      * @param itemID
221      * The itemID of the article locked
222      * @return 0=error or 1=success
223      */

224     public static String JavaDoc getMailLockedArticle(DataStore db, String JavaDoc itemID) {
225         String JavaDoc email = "";
226         try {
227             // test the input values
228
if ((itemID == null) || (itemID.equals("0"))) {
229                 return ("");
230             }
231             HashMap ht = new HashMap();
232             String JavaDoc tag = "SELECT email FROM tblpermusers AS PU, tblarticlelock as AL ";
233             tag += "WHERE AL.itemID=" + itemID + " and AL.userID=PU.userID ";
234             tag += "and lockDate >= DATE_ADD(Now(), INTERVAL -30 minute)";
235             HashMap hashinfo = CofaxToolsDbUtils.getNameValuePackageHash(db, ht, tag);
236             email = (String JavaDoc) hashinfo.get("EMAIL");
237         } catch (Exception JavaDoc e) {
238             CofaxToolsUtil.log("CofaxToolsDbUtils.getMailLockedArticle : error " + e);
239         }
240         return (email);
241
242     }
243
244 }
245
Popular Tags