roller-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agillil...@apache.org
Subject svn commit: r393931 - in /incubator/roller/branches/roller-newbackend/src/org/roller: business/hibernate/HibernateUserManagerImpl.java model/UserManager.java
Date Thu, 13 Apr 2006 20:54:04 GMT
Author: agilliland
Date: Thu Apr 13 13:54:02 2006
New Revision: 393931

URL: http://svn.apache.org/viewcvs?rev=393931&view=rev
Log:
adding new methods for bulk user/weblog/permissions operations.


Modified:
    incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernateUserManagerImpl.java
    incubator/roller/branches/roller-newbackend/src/org/roller/model/UserManager.java

Modified: incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernateUserManagerImpl.java
URL: http://svn.apache.org/viewcvs/incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernateUserManagerImpl.java?rev=393931&r1=393930&r2=393931&view=diff
==============================================================================
--- incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernateUserManagerImpl.java
(original)
+++ incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernateUserManagerImpl.java
Thu Apr 13 13:54:02 2006
@@ -4,6 +4,7 @@
 package org.roller.business.hibernate;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Date;
 import java.util.Iterator;
 import java.util.List;
@@ -59,7 +60,7 @@
     }
     
     
-       /**
+    /**
      * @see org.roller.model.UserManager#storeWebsite(org.roller.pojos.WebsiteData)
      */
     public void storeWebsite(WebsiteData data) throws RollerException {
@@ -67,6 +68,11 @@
     }
     
     
+    public void storeWebsites(Collection weblogs) throws RollerException {
+        this.strategy.storeAndCommit(weblogs);
+    }
+    
+    
     public void removeWebsite(String id) throws RollerException {
         
         try {
@@ -82,11 +88,11 @@
             
             // commit changes
             this.strategy.getSession().getTransaction().commit();
-        } catch (Exception ex) {
+        } catch (Throwable ex) {
             
             try {
                 this.strategy.getSession().getTransaction().rollback();
-            } catch(HibernateException he) {
+            } catch(Throwable he) {
                 log.error("Error doing rollback", he);
             }
             
@@ -109,11 +115,11 @@
             
             // commit changes
             this.strategy.getSession().getTransaction().commit();
-        } catch (Exception ex) {
+        } catch (Throwable ex) {
             
             try {
                 this.strategy.getSession().getTransaction().rollback();
-            } catch(HibernateException he) {
+            } catch(Throwable he) {
                 log.error("Error doing rollback", he);
             }
             
@@ -217,6 +223,14 @@
     }
     
     
+    /**
+     * Store a collection of users in a single transaction.
+     */
+    public void storeUsers(Collection users) throws RollerException {
+        this.strategy.storeAndCommit(users);
+    }
+    
+    
     public void removeUser(UserData user) throws RollerException {
         this.strategy.removeAndCommit(user);
     }
@@ -232,6 +246,11 @@
     }
     
     
+    public void storePermissions(Collection permissions) throws RollerException {
+        this.strategy.storeAndCommit(permissions);
+    }
+    
+    
     public void removePermissions(PermissionsData perms) throws RollerException {
         this.strategy.removeAndCommit(perms);
     }
@@ -250,23 +269,28 @@
     }
     
     
-    /**
-     * Add a new Roller user.
-     *
-     * TODO BACKEND: do we really need this method?  probably better if roles are added
-     *       elsewhere and then we just use storeUser().
-     */
     public void addUser(UserData ud) throws RollerException {
         
-        if(getUser(ud.getUserName()) != null || 
-                getUser(ud.getUserName().toLowerCase()) != null) {
-            throw new RollerException("error.add.user.userNameInUse");
-        }
+        if(ud == null)
+            throw new RollerException("cannot add null user");
+        
+        // simplification, just add user to a collection and reuse the method
+        Collection userList = new ArrayList();
+        userList.add(ud);
+        
+        this.addUsers(userList);
+    }
+    
+    
+    public void addUsers(Collection users) throws RollerException {
+        
+        if(users == null || users.size() < 1)
+            throw new RollerException("no users to add");
         
         // TODO BACKEND: we must do this in a better fashion, like getUserCnt()?
         boolean adminUser = false;
-        List users = this.getUsers();
-        if (users.size() == 0) {
+        List existingUsers = this.getUsers();
+        if(existingUsers.size() == 0) {
             // Make first user an admin
             adminUser = true;
         }
@@ -275,25 +299,31 @@
             // begin transaction
             this.strategy.getSession().beginTransaction();
             
-            // add user
-            this.strategy.store(ud);
-            
-            // and roles
-            RoleData editorRole = new RoleData(null, ud, "editor");
-            this.strategy.store(editorRole);
-            
-            if (adminUser) {
-                RoleData adminRole = new RoleData(null, ud, "admin");
-                this.strategy.store(adminRole);
+            UserData newUser = null;
+            Iterator items = users.iterator();
+            while(items.hasNext()) {
+                newUser = (UserData) items.next();
+                
+                if(getUser(newUser.getUserName()) != null ||
+                        getUser(newUser.getUserName().toLowerCase()) != null) {
+                    throw new RollerException("error.add.user.userNameInUse");
+                }
+                
+                newUser.grantRole("editor");
+                if(adminUser) {
+                    newUser.grantRole("admin");
+                }
+                
+                this.strategy.store(newUser);
             }
 
             // commit changes
             this.strategy.getSession().getTransaction().commit();
-        } catch (HibernateException ex) {
+        } catch (Throwable ex) {
             
             try {
                 this.strategy.getSession().getTransaction().rollback();
-            } catch(HibernateException he) {
+            } catch(Throwable he) {
                 log.error("Error doing rollback", he);
             }
             
@@ -301,91 +331,33 @@
             
             throw new RollerException(ex);
         }
-        
     }
     
     
-    public void createWebsite(WebsiteData newWebsite) throws RollerException {
-        
-        UserManager umgr = RollerFactory.getRoller().getUserManager();
-        WeblogManager wmgr = RollerFactory.getRoller().getWeblogManager();
+    public void addWebsites(Collection weblogs) throws RollerException {
         
         try {
             // begin transaction
             this.strategy.getSession().beginTransaction();
             
-            // store the new weblog
-            this.strategy.store(newWebsite);
-            
-            // grant weblog creator ADMIN permissions
-            PermissionsData perms = new PermissionsData();
-            perms.setUser(newWebsite.getCreator());
-            perms.setWebsite(newWebsite);
-            perms.setPending(false);
-            perms.setPermissionMask(PermissionsData.ADMIN);
-            this.strategy.store(perms);
-            
-            // add default categories
-            WeblogCategoryData rootCat = wmgr.createWeblogCategory(
-                    newWebsite, // newWebsite
-                    null,   // parent
-                    "root",  // name
-                    "root",  // description
-                    null ); // image
-            this.strategy.store(rootCat);
-            
-            String cats = RollerConfig.getProperty("newuser.categories");
-            if (cats != null) {
-                String[] splitcats = cats.split(",");
-                for (int i=0; i<splitcats.length; i++) {
-                    WeblogCategoryData c = wmgr.createWeblogCategory(
-                            newWebsite,         // newWebsite
-                            rootCat,         // parent
-                            splitcats[i],    // name
-                            splitcats[i],    // description
-                            null );          // image
-                    this.strategy.store(c);
-                }
-            }
-            newWebsite.setBloggerCategory(rootCat);
-            newWebsite.setDefaultCategory(rootCat);
-            this.strategy.store(newWebsite);
-            
-            // add default bookmarks
-            BookmarkManager bmgr = RollerFactory.getRoller().getBookmarkManager();
-            FolderData root = bmgr.createFolder(
-                    null, "root", "root", newWebsite);
-            this.strategy.store(root);
-            
-            Integer zero = new Integer(0);
-            String blogroll = RollerConfig.getProperty("newuser.blogroll");
-            if (blogroll != null) {
-                String[] splitroll = blogroll.split(",");
-                for (int i=0; i<splitroll.length; i++) {
-                    String[] rollitems = splitroll[i].split("\\|");
-                    if (rollitems != null && rollitems.length > 1) {
-                        BookmarkData b = bmgr.createBookmark(
-                                root,                // parent
-                                rollitems[0],        // name
-                                "",                  // description
-                                rollitems[1].trim(), // url
-                                null,                // feedurl
-                                zero,                // weight
-                                zero,                // priority
-                                null);               // image
-                        this.strategy.store(b);
-                    }
-                }
+            // store the new weblogs
+            WebsiteData newWeblog = null;
+            Iterator items = weblogs.iterator();
+            while(items.hasNext()) {
+                newWeblog = (WebsiteData) items.next();
+                
+                this.strategy.store(newWeblog);
+                this.addWeblogContents(newWeblog);
             }
             
             // commit changes
             this.strategy.getSession().getTransaction().commit();
             
-        } catch (HibernateException ex) {
-
+        } catch (Throwable ex) {
+            
             try {
                 this.strategy.getSession().getTransaction().rollback();
-            } catch(HibernateException he) {
+            } catch(Throwable he) {
                 log.error("Error doing rollback", he);
             }
             
@@ -393,6 +365,85 @@
             
             throw new RollerException(ex);
         }
+    }
+    
+    
+    public void createWebsite(WebsiteData newWeblog) throws RollerException {
+        
+        // simplification, just use addWebsites(collection)
+        Collection newWeblogs = new ArrayList();
+        newWeblogs.add(newWeblog);
+        
+        this.addWebsites(newWeblogs);
+    }
+    
+    
+    private void addWeblogContents(WebsiteData newWeblog) throws Exception {
+        
+        UserManager umgr = RollerFactory.getRoller().getUserManager();
+        WeblogManager wmgr = RollerFactory.getRoller().getWeblogManager();
+        
+        // grant weblog creator ADMIN permissions
+        PermissionsData perms = new PermissionsData();
+        perms.setUser(newWeblog.getCreator());
+        perms.setWebsite(newWeblog);
+        perms.setPending(false);
+        perms.setPermissionMask(PermissionsData.ADMIN);
+        this.strategy.store(perms);
+        
+        // add default categories
+        WeblogCategoryData rootCat = wmgr.createWeblogCategory(
+                newWeblog, // newWeblog
+                null,   // parent
+                "root",  // name
+                "root",  // description
+                null ); // image
+        this.strategy.store(rootCat);
+        
+        String cats = RollerConfig.getProperty("newuser.categories");
+        if (cats != null) {
+            String[] splitcats = cats.split(",");
+            for (int i=0; i<splitcats.length; i++) {
+                WeblogCategoryData c = wmgr.createWeblogCategory(
+                        newWeblog,         // newWeblog
+                        rootCat,         // parent
+                        splitcats[i],    // name
+                        splitcats[i],    // description
+                        null );          // image
+                this.strategy.store(c);
+            }
+        }
+        newWeblog.setBloggerCategory(rootCat);
+        newWeblog.setDefaultCategory(rootCat);
+        this.strategy.store(newWeblog);
+        
+        // add default bookmarks
+        BookmarkManager bmgr = RollerFactory.getRoller().getBookmarkManager();
+        FolderData root = bmgr.createFolder(
+                null, "root", "root", newWeblog);
+        this.strategy.store(root);
+        
+        Integer zero = new Integer(0);
+        String blogroll = RollerConfig.getProperty("newuser.blogroll");
+        if (blogroll != null) {
+            String[] splitroll = blogroll.split(",");
+            for (int i=0; i<splitroll.length; i++) {
+                String[] rollitems = splitroll[i].split("\\|");
+                if (rollitems != null && rollitems.length > 1) {
+                    BookmarkData b = bmgr.createBookmark(
+                            root,                // parent
+                            rollitems[0],        // name
+                            "",                  // description
+                            rollitems[1].trim(), // url
+                            null,                // feedurl
+                            zero,                // weight
+                            zero,                // priority
+                            null);               // image
+                    this.strategy.store(b);
+                }
+            }
+        }
+
     }
     
     

Modified: incubator/roller/branches/roller-newbackend/src/org/roller/model/UserManager.java
URL: http://svn.apache.org/viewcvs/incubator/roller/branches/roller-newbackend/src/org/roller/model/UserManager.java?rev=393931&r1=393930&r2=393931&view=diff
==============================================================================
--- incubator/roller/branches/roller-newbackend/src/org/roller/model/UserManager.java (original)
+++ incubator/roller/branches/roller-newbackend/src/org/roller/model/UserManager.java Thu
Apr 13 13:54:02 2006
@@ -2,6 +2,7 @@
 package org.roller.model;
 
 import java.io.Serializable;
+import java.util.Collection;
 import java.util.List;
 import java.util.Map;
 import org.roller.RollerException;
@@ -28,11 +29,23 @@
     
     
     /**
-     * Store user.
+     * Add a collection of users in a single transaction.
+     */
+    public void addUsers(Collection users) throws RollerException;
+    
+    
+    /**
+     * Store a single user.
      */
     public void storeUser(UserData data) throws RollerException;
     
     
+    /**
+     * Store a set of users in a single transaction.
+     */
+    public void storeUsers(Collection users) throws RollerException;
+    
+    
     public void removeUser(UserData user) throws RollerException;
     
     
@@ -116,11 +129,23 @@
     
     
     /**
-     * Store website
+     * Add a collection of weblogs in a single transaction.
+     */
+    public void addWebsites(Collection websites) throws RollerException;
+    
+    
+    /**
+     * Store a single weblog.
      */
     public void storeWebsite(WebsiteData data) throws RollerException;
     
     
+    /**
+     * Store a collection of weblogs in a single transaction.
+     */
+    public void storeWebsites(Collection weblogs) throws RollerException;
+    
+    
     public void removeWebsite(WebsiteData website) throws RollerException;
     
     
@@ -155,6 +180,12 @@
     
     
     public void storePermissions(PermissionsData perms) throws RollerException;
+    
+    
+    /**
+     * Store a collection of permissions in a single transaction.
+     */
+    public void storePermissions(Collection permissions) throws RollerException;
     
     
     public void removePermissions(PermissionsData perms) throws RollerException;



Mime
View raw message