KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mvnforum > admin > importexport > jive > JiveXML


1 /*
2  * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/admin/importexport/jive/JiveXML.java,v 1.6 2006/04/14 17:36:29 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.6 $
5  * $Date: 2006/04/14 17:36:29 $
6  *
7  * ====================================================================
8  *
9  * Copyright (C) 2002-2006 by MyVietnam.net
10  *
11  * All copyright notices regarding mvnForum MUST remain
12  * intact in the scripts and in the outputted HTML.
13  * The "powered by" text/logo with a link back to
14  * http://www.mvnForum.com and http://www.MyVietnam.net in
15  * the footer of the pages MUST remain visible when the pages
16  * are viewed on the internet or intranet.
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31  *
32  * Support can be obtained from support forums at:
33  * http://www.mvnForum.com/mvnforum/index
34  *
35  * Correspondence and Marketing Questions can be sent to:
36  * info at MyVietnam net
37  *
38  * @author: Igor Manic
39  */

40 package com.mvnforum.admin.importexport.jive;
41
42 import org.apache.commons.codec.binary.Base64;
43 import org.apache.commons.codec.binary.Hex;
44 import com.mvnforum.MVNForumConfig;
45 import com.mvnforum.admin.*;
46 import com.mvnforum.auth.MVNForumPermission;
47 import net.myvietnam.mvncore.exception.*;
48
49 /**
50  * @author Igor Manic
51  * @version $Revision: 1.6 $, $Date: 2006/04/14 17:36:29 $
52  * <br/>
53  * <code>JiveXML</code> class encapsulates auxiliary methods for conversion of
54  * <code>Jive</code> permissions and passwords to <code>mvnForum</code> ones.
55  * It also defines some common constants used during the migration from
56  * <code>Jive</code> to <code>mvnForum</code>.<br/>
57  * Instance of this class will be the root object of the <code>digester</code> stack.
58  */

59 public class JiveXML {
60
61     public static String JavaDoc allForumsPassword = "";
62     public static String JavaDoc rootCategoryName = "General";
63     public static String JavaDoc rootCategoryDesc = "Forums imported from Jive";
64     public static String JavaDoc adminName = "Admin";
65     public static String JavaDoc guestName = MVNForumConfig.getDefaultGuestName();
66
67     /** Did we find Jive admin user? If we didn't, default one will be created with password "admin".
68       * If we found Jive admin user, we kept his password, but now he has SYSTEM_ADMIN
69       * permissions, even if he didn't have them in Jive!!!
70       */

71     public static boolean foundAdminUser = false;
72     public static int rootCategoryID = -1;
73
74     public JiveXML() throws DatabaseException, CreateException,
75     DuplicateKeyException, ObjectNotFoundException, ForeignKeyNotFoundException {
76         super();
77         foundAdminUser=false;
78
79         /* First, I'll create guest and admin user, but, if admin already exists in Jive,
80          * it will be rewritten when I come to it. For now, it's only important
81          * that I have it in the database, and that it has ID==1.
82          */

83         ImportJive.createDefaultAdminMember();
84         ImportJive.createDefaultGuestMember();
85         ImportJive.createDefaultRegisteredMembersGroup();
86         ImportJive.createDefaultRanks();
87
88         /* Each MVN category can have any number of it's own forums.
89          * Also, MVN supports (sub)categories in each category.
90          * Jive's forums are the same as MVN's forums, but Jive doesn't have categories at all.
91          * That means that all Jive's forums will be translated into MVN's forums that
92          * have to be put down into the one and only MVN category I must create here.
93          */

94         ImportJive.createRootCategory();
95     }
96
97     public static void addedForum(ForumXML forumXML) {
98         //update the root category - nothing to update
99
}
100
101     public static void addedThread(ThreadXML threadXML) {
102         //update the root category - nothing to update
103
}
104
105     public static void addedPost(PostXML postXML) {
106         //update the root category - nothing to update
107
}
108
109
110 // ================================================================================
111
// ============== UTILITY CONVERSION METHODS FROM JIVE TO MVN FORUM ===============
112
// ================================================================================
113
/**
114      * Conversion of Jive password to MVN Forum password.
115      * Jive and MVN Forum both use MD5:
116      * &nbsp;&nbsp;&nbsp;Jive password -> MD5 encoding -> Hex encoding
117      * &nbsp;&nbsp;&nbsp;MVN Forum password -> MD5 encoding -> Base64 encoding
118      * So to convert from Jive password to MVN Forum password:
119      * Jive:&nbsp;&nbsp; Hex encoding -> Hex decoding -> Base 64 encoding &nbsp;&nbsp;:MVN Forum
120      *
121      * @param jiveEncodedPassword Jive encoded password.
122      * @return MVN Forum encoded password, or <code>null</code> in case of error.
123      *
124      */

125     public static String JavaDoc convertPassword(String JavaDoc jiveEncodedPassword) {
126         String JavaDoc mvnEncodedPassword=null;
127         try {
128             byte[] decodedByteArray = Hex.decodeHex(jiveEncodedPassword.toCharArray());
129             mvnEncodedPassword=new String JavaDoc(Base64.encodeBase64(decodedByteArray));
130         } catch (Exception JavaDoc e) {
131             mvnEncodedPassword=null;
132         }
133         return mvnEncodedPassword;
134     }
135
136     public static int[] convertMemberPermission(String JavaDoc jivePermission) {
137         return convertPermission(jivePermission);
138     }
139     public static int[] convertGroupPermission(String JavaDoc jivePermission) {
140         return convertPermission(jivePermission);
141     }
142     public static int[] convertMemberForumPermission(String JavaDoc jivePermission) {
143         return convertPermission(jivePermission);
144     }
145     public static int[] convertGroupForumPermission(String JavaDoc jivePermission) {
146         return convertPermission(jivePermission);
147     }
148     protected static int[] convertPermission(String JavaDoc jivePermission) {
149         //todo Igor: check again all permission conversions here
150
if ( (jivePermission==null) || (jivePermission.equalsIgnoreCase("NONE")) ) {
151             return new int[]{MVNForumPermission.PERMISSION_NO_PERMISSIONS};
152         } else if (jivePermission.equalsIgnoreCase("SYSTEM_ADMIN")) {
153             return new int[] {MVNForumPermission.PERMISSION_SYSTEM_ADMIN};
154         } else if (jivePermission.equalsIgnoreCase("CATEGORY_ADMIN")) {
155             return new int[] {MVNForumPermission.PERMISSION_CATEGORY_ADMIN,
156                               MVNForumPermission.PERMISSION_CATEGORY_MODERATOR,
157                               MVNForumPermission.PERMISSION_ADD_CATEGORY,
158                               MVNForumPermission.PERMISSION_EDIT_CATEGORY,
159                               MVNForumPermission.PERMISSION_DELETE_CATEGORY};
160         } else if (jivePermission.equalsIgnoreCase("FORUM_ADMIN")) {
161             return new int[] {MVNForumPermission.PERMISSION_FORUM_ADMIN,
162                               MVNForumPermission.PERMISSION_FORUM_MODERATOR,
163                               MVNForumPermission.PERMISSION_ADD_FORUM,
164                               MVNForumPermission.PERMISSION_EDIT_FORUM,
165                               MVNForumPermission.PERMISSION_DELETE_FORUM};
166         } else if (jivePermission.equalsIgnoreCase("GROUP_ADMIN")) {
167             return new int[] {MVNForumPermission.PERMISSION_GROUP_ADMIN,
168                               MVNForumPermission.PERMISSION_GROUP_MODERATOR};
169         } else if (jivePermission.equalsIgnoreCase("USER_ADMIN")) {
170             return new int[] {MVNForumPermission.PERMISSION_USER_ADMIN,
171                               MVNForumPermission.PERMISSION_USER_MODERATOR};
172         } else if (jivePermission.equalsIgnoreCase("MODERATOR")) {
173             return new int[] {MVNForumPermission.PERMISSION_CATEGORY_MODERATOR,
174                               MVNForumPermission.PERMISSION_FORUM_MODERATOR,
175                               MVNForumPermission.PERMISSION_GROUP_MODERATOR,
176                               MVNForumPermission.PERMISSION_USER_MODERATOR};
177         } else if (jivePermission.equalsIgnoreCase("MODERATE_THREADS")) {
178             return new int[] {MVNForumPermission.PERMISSION_READ_POST,
179                               MVNForumPermission.PERMISSION_ADD_POST,
180                               MVNForumPermission.PERMISSION_EDIT_POST,
181                               MVNForumPermission.PERMISSION_DELETE_POST,
182                               MVNForumPermission.PERMISSION_ADD_THREAD};
183         } else if (jivePermission.equalsIgnoreCase("CREATE_THREAD")) {
184             return new int[] {MVNForumPermission.PERMISSION_ADD_THREAD};
185         } else if (jivePermission.equalsIgnoreCase("MODERATE_MESSAGES")) {
186             return new int[] {MVNForumPermission.PERMISSION_READ_POST,
187                               MVNForumPermission.PERMISSION_ADD_POST,
188                               MVNForumPermission.PERMISSION_EDIT_POST,
189                               MVNForumPermission.PERMISSION_DELETE_POST};
190         } else if (jivePermission.equalsIgnoreCase("CREATE_MESSAGE")) {
191             return new int[] {MVNForumPermission.PERMISSION_ADD_POST};
192         } else if (jivePermission.equalsIgnoreCase("CREATE_ATTACHMENT")) {
193             return new int[] {MVNForumPermission.PERMISSION_ADD_ATTACHMENT};
194         } else if (jivePermission.equalsIgnoreCase("READ_FORUM")) {
195             return new int[] {MVNForumPermission.PERMISSION_READ_POST};
196         } else if (jivePermission.equalsIgnoreCase("READ")) {
197             return new int[] {MVNForumPermission.PERMISSION_READ_POST};
198         } else {
199             return new int[] {MVNForumPermission.PERMISSION_NO_PERMISSIONS};
200         }
201     }
202
203
204 // ================================================================================
205
// ==================== METHODS TO BE CALLED FROM THE DIGESTER ====================
206
// ================================================================================
207
public void setJiveXmlVersion(String JavaDoc value) {
208         ImportJive.addMessage("Jive XML version = \""+value+"\"");
209     }
210
211     public void setJiveExportDate(String JavaDoc value) {
212         ImportJive.addMessage("Jive XML export date = \""+value+"\"");
213     }
214
215     public void addJiveUserPermission(String JavaDoc usertype, String JavaDoc username, String JavaDoc jivePermission)
216     throws CreateException, DatabaseException, ObjectNotFoundException,
217     ForeignKeyNotFoundException {
218         if (usertype==null) {
219             throw new CreateException("Not enough data to create a member global permission.");
220
221         } else if (usertype.equalsIgnoreCase("ANONYMOUS")) {
222             int[] perms = JiveXML.convertMemberPermission(jivePermission);
223             for (int j=0; j<perms.length; j++) {
224                 try {
225                     JiveXML.addGuestMemberPermission(Integer.toString(perms[j]));
226                 } catch (DuplicateKeyException e) {
227                     /* Ignore if we doubled some permissions.
228                      * Because we convert each Jive permission into the list of
229                      * MVN Forum permissions (can be more than one), some permissions
230                      * could be generated twice, or more times.
231                      */

232                 }
233             }
234
235         } else if (usertype.equalsIgnoreCase("REGISTERED_USERS")) {
236             int[] perms = JiveXML.convertGroupPermission(jivePermission);
237             for (int j=0; j<perms.length; j++) {
238                 try {
239                     JiveXML.addRegisteredMembersGroupPermission(Integer.toString(perms[j]));
240                 } catch (DuplicateKeyException e) {
241                    /* Ignore if we doubled some permissions.
242                     * Because we convert each Jive permission into the list of
243                     * MVN Forum permissions (can be more than one), some permissions
244                     * could be generated twice, or more times.
245                     */

246                 }
247             }
248
249         } else if (usertype.equalsIgnoreCase("USER")) {
250             int[] perms = JiveXML.convertMemberPermission(jivePermission);
251             for (int j=0; j<perms.length; j++) {
252                 try {
253                     JiveXML.addMemberPermission(username, Integer.toString(perms[j]));
254                 } catch (DuplicateKeyException e) {
255                     /* Ignore if we doubled some permissions.
256                      * Because we convert each Jive permission into the list of
257                      * MVN Forum permissions (can be more than one), some permissions
258                      * could be generated twice, or more times.
259                      */

260                 }
261             }
262
263         } else {
264             throw new CreateException("Invalid usertype. This Jive user global permission is ignored.");
265         }
266     }
267
268     public void addJiveGroupPermission(String JavaDoc groupname, String JavaDoc jivePermission)
269     throws CreateException, DatabaseException, ObjectNotFoundException,
270     ForeignKeyNotFoundException {
271         if ( (groupname==null) || (groupname.equals("")) ) {
272             throw new CreateException("Not enough data to create a group global permission.");
273         } else {
274             int[] perms = JiveXML.convertGroupPermission(jivePermission);
275             for (int j=0; j<perms.length; j++) {
276                 try {
277                     JiveXML.addGroupPermission(groupname, Integer.toString(perms[j]));
278                 } catch (DuplicateKeyException e) {
279                    /* Ignore if we doubled some permissions.
280                     * Because we convert each Jive permission into the list of
281                     * MVN Forum permissions (can be more than one), some permissions
282                     * could be generated twice, or more times.
283                     */

284                 }
285             }
286         }
287     }
288
289
290 // ================================================================================
291
// ====================== UTILITY METHODS ABOUT PERMISSIONS =======================
292
// ================================================================================
293
public static int[] addDefaultPermissionsToGuests = new int[]{
294          //MVNForumPermission.PERMISSION_SEND_MAIL,
295
//MVNForumPermission.PERMISSION_USE_MESSAGE,
296
//MVNForumPermission.PERMISSION_USE_AVATAR,
297
//MVNForumPermission.PERMISSION_ADD_POLL,
298
//MVNForumPermission.PERMISSION_EDIT_POLL,
299
//MVNForumPermission.PERMISSION_DELETE_POLL,
300
//MVNForumPermission.PERMISSION_GET_ATTACHMENT
301
};
302      public static int[] addDefaultPermissionsToMembers = new int[]{
303          //MVNForumPermission.PERMISSION_SEND_MAIL,
304
MVNForumPermission.PERMISSION_USE_MESSAGE,
305          MVNForumPermission.PERMISSION_USE_AVATAR,
306          //MVNForumPermission.PERMISSION_ADD_POLL,
307
//MVNForumPermission.PERMISSION_EDIT_POLL,
308
//MVNForumPermission.PERMISSION_DELETE_POLL,
309
MVNForumPermission.PERMISSION_GET_ATTACHMENT
310      };
311
312      /**
313       * TODO Igor: enter description
314       *
315       * @param username
316       * @param permission
317       */

318      public static void addMemberPermission(String JavaDoc username, String JavaDoc permission)
319      throws ObjectNotFoundException, CreateException, DatabaseException,
320      ForeignKeyNotFoundException, DuplicateKeyException {
321          MemberXML.addMemberPermission(username, permission);
322      }
323
324      /**
325       * TODO Igor: enter description
326       *
327       * @param permission
328       */

329      public static void addRegisteredMembersGroupPermission(String JavaDoc permission)
330      throws CreateException, DatabaseException, DuplicateKeyException,
331      ForeignKeyNotFoundException {
332          GroupXML.addRegisteredMembersGroupPermission(permission);
333      }
334
335      /**
336       * TODO Igor: enter description
337       *
338       * @param permission
339       */

340      public static void addGuestMemberPermission(String JavaDoc permission)
341      throws CreateException, DatabaseException, DuplicateKeyException,
342      ForeignKeyNotFoundException {
343          MemberXML.addGuestMemberPermission(permission);
344      }
345
346      /**
347       * TODO Igor: enter description
348       *
349       * @param groupname
350       * @param permission
351       */

352      public static void addGroupPermission(String JavaDoc groupname, String JavaDoc permission)
353      throws CreateException, DatabaseException, DuplicateKeyException,
354      ForeignKeyNotFoundException, ObjectNotFoundException {
355          GroupXML.addGroupPermission(groupname, permission);
356      }
357
358
359 }
360
361
Popular Tags