File Source: PlanetGroups.java

     1  /*
     2   * Copyright 2005 Sun Microsystems, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package org.apache.roller.weblogger.planet.ui;
    18  
    19  import java.util.ArrayList;
    20  import java.util.Collections;
    21  import java.util.Iterator;
    22  import java.util.List;
    23  import org.apache.commons.lang.StringUtils;
    24  import org.apache.commons.logging.Log;
    25  import org.apache.commons.logging.LogFactory;
    26  import org.apache.roller.RollerException;
    27  import org.apache.roller.planet.business.PlanetFactory;
    28  import org.apache.roller.planet.business.PlanetManager;
    29  import org.apache.roller.planet.pojos.PlanetGroup;
    30  
    31  
    32  /**
    33   * Manage planet groups.
    34   */
    35  public class PlanetGroups extends PlanetUIAction {
    36      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.planet.ui.PlanetGroups__static_init
              * 
              *  Postconditions:
              *    init'ed(log)
              */
    37      private static Log log = LogFactory.getLog(PlanetGroups.class);
    38      
    39      // a bean to manage submitted data
    40      private PlanetGroupsBean bean = new PlanetGroupsBean();
    41      
    42      // the planet group we are working on
    43      private PlanetGroup group = null;
    44      
    45      
             /* 
    P/P       *  Method: void org.apache.roller.weblogger.planet.ui.PlanetGroups()
              * 
              *  Postconditions:
              *    this.actionName == &"planetGroups"
              *    this.bean == &new PlanetGroupsBean(PlanetGroups#1)
              *    this.desiredMenu == &"admin"
              *    this.group == null
              *    this.planet == null
              *    this.bean.handle == null
              *    this.bean.id == null
              *    this.bean.title == null
              *    this.pageTitle == &"planetGroups.pagetitle"
              *    new PlanetGroupsBean(PlanetGroups#1) num objects == 1
              */
    46      public PlanetGroups() {
    47          this.actionName = "planetGroups";
    48          this.desiredMenu = "admin";
    49          this.pageTitle = "planetGroups.pagetitle";
    50      }
    51      
    52      
    53      @Override
    54      public String requiredUserRole() {
                 /* 
    P/P           *  Method: String requiredUserRole()
                  * 
                  *  Postconditions:
                  *    return_value == &"admin"
                  */
    55          return "admin";
    56      }
    57      
    58      @Override
    59      public boolean isWeblogRequired() {
                 /* 
    P/P           *  Method: bool isWeblogRequired()
                  * 
                  *  Postconditions:
                  *    return_value == 0
                  */
    60          return false;
    61      }
    62      
    63      
    64      @Override
    65      public void myPrepare() {
    66          
                 /* 
    P/P           *  Method: void myPrepare()
                  * 
                  *  Preconditions:
                  *    init'ed(this.planet)
                  *    (soft) log != null
                  *    (soft) org/apache/roller/weblogger/planet/ui/PlanetUIAction.log != null
                  *    (soft) this.bean != null
                  *    (soft) init'ed(this.bean.id)
                  * 
                  *  Presumptions:
                  *    org.apache.roller.planet.business.Planet:getPlanetManager(...)@68 != null
                  *    org.apache.roller.planet.business.PlanetFactory:getPlanet(...)@68 != null
                  * 
                  *  Postconditions:
                  *    possibly_updated(this.group)
                  *    init'ed(this.planet)
                  * 
                  *  Test Vectors:
                  *    this.bean.id: Addr_Set{null}, Inverse{null}
                  */
    67          if(getPlanet() != null && getBean().getId() != null) try {
    68              PlanetManager pmgr = PlanetFactory.getPlanet().getPlanetManager();
    69              setGroup(pmgr.getGroupById(getBean().getId()));
    70          } catch(Exception ex) {
    71              log.error("Error looking up planet group - "+getBean().getId(), ex);
    72          }
    73      }
    74  
    75      
    76      /** 
    77       * Show planet groups page.
    78       */
    79      public String execute() {
    80          
    81          // if we are loading an existing group then populate the bean
                 /* 
    P/P           *  Method: String execute()
                  * 
                  *  Preconditions:
                  *    init'ed(this.group)
                  *    (soft) this.bean != null
                  * 
                  *  Postconditions:
                  *    return_value == &"list"
                  *    possibly_updated(this.bean.handle)
                  *    possibly_updated(this.bean.id)
                  *    possibly_updated(this.bean.title)
                  * 
                  *  Test Vectors:
                  *    this.group: Addr_Set{null}, Inverse{null}
                  */
    82          if(getGroup() != null) {
    83              getBean().copyFrom(getGroup());
    84          }
    85          
    86          return LIST;
    87      }
    88      
    89      
    90      /** 
    91       * Save group.
    92       */
    93      public String save() {
    94          
                 /* 
    P/P           *  Method: String save()
                  * 
                  *  Preconditions:
                  *    this.bean != null
                  *    init'ed(this.bean.handle)
                  *    init'ed(this.bean.title)
                  *    (soft) log != null
                  *    (soft) init'ed(this.planet)
                  *    (soft) org/apache/roller/weblogger/planet/ui/PlanetUIAction.log != null
                  *    (soft) init'ed(this.bean.id)
                  *    (soft) init'ed(this.group)
                  * 
                  *  Presumptions:
                  *    org.apache.roller.planet.business.Planet:getPlanetManager(...)@112 != null
                  *    org.apache.roller.planet.business.PlanetFactory:getPlanet(...)@112 != null
                  *    org.apache.roller.planet.business.PlanetFactory:getPlanet(...)@114 != null
                  * 
                  *  Postconditions:
                  *    return_value == &"list"
                  *    init'ed(this.planet)
                  * 
                  *  Test Vectors:
                  *    this.group: Inverse{null}, Addr_Set{null}
                  *    org.apache.roller.weblogger.planet.ui.PlanetGroups:hasActionErrors(...)@97: {1}, {0}
                  */
    95          myValidate();
    96          
    97          if (!hasActionErrors()) try {
    98              
    99              PlanetGroup group = getGroup();
   100              if(group == null) {
   101                  log.debug("Adding New Group");
   102                  group = new PlanetGroup();
   103                  group.setPlanet(getPlanet());
   104              } else {
   105                  log.debug("Updating Existing Group");
   106              }
   107              
   108              // copy in submitted data
   109              getBean().copyTo(group);
   110              
   111              // save and flush
   112              PlanetManager pmgr = PlanetFactory.getPlanet().getPlanetManager();
   113              pmgr.saveGroup(group);
   114              PlanetFactory.getPlanet().flush();
   115              
   116              addMessage("planetGroups.success.saved");
   117              
   118          } catch(Exception ex) {
   119              log.error("Error saving planet group - "+getBean().getId(), ex);
   120              // TODO: i18n
   121              addError("Error saving planet group");
   122          }
   123          
   124          return LIST;
   125      }
   126  
   127      
   128      /** 
   129       * Delete group, reset form  
   130       */
   131      public String delete() {
   132          
                 /* 
    P/P           *  Method: String delete()
                  * 
                  *  Preconditions:
                  *    init'ed(this.group)
                  *    (soft) log != null
                  *    (soft) this.bean != null
                  *    (soft) init'ed(this.bean.id)
                  * 
                  *  Presumptions:
                  *    org.apache.roller.planet.business.Planet:getPlanetManager(...)@135 != null
                  *    org.apache.roller.planet.business.PlanetFactory:getPlanet(...)@135 != null
                  *    org.apache.roller.planet.business.PlanetFactory:getPlanet(...)@137 != null
                  * 
                  *  Postconditions:
                  *    return_value == &"list"
                  * 
                  *  Test Vectors:
                  *    this.group: Addr_Set{null}, Inverse{null}
                  */
   133          if(getGroup() != null) {
   134              try {
   135                  PlanetManager pmgr = PlanetFactory.getPlanet().getPlanetManager();
   136                  pmgr.deleteGroup(getGroup());
   137                  PlanetFactory.getPlanet().flush();
   138                  
   139                  addMessage("planetSubscription.success.deleted");
   140              } catch(Exception ex) {
   141                  log.error("Error deleting planet group - "+getBean().getId());
   142                  // TODO: i18n
   143                  addError("Error deleting planet group");
   144              }
   145          }
   146          
   147          return LIST;
   148      }
   149      
   150      
   151      /** 
   152       * Validate posted group 
   153       */
   154      private void myValidate() {
   155          
                 /* 
    P/P           *  Method: void myValidate()
                  * 
                  *  Preconditions:
                  *    this.bean != null
                  *    init'ed(this.bean.handle)
                  *    init'ed(this.bean.title)
                  * 
                  *  Test Vectors:
                  *    this.bean.handle: Addr_Set{null}, Inverse{null}
                  *    java.lang.String:equals(...)@164: {0}, {1}
                  *    org.apache.commons.lang.StringUtils:isEmpty(...)@156: {0}, {1}
                  *    org.apache.commons.lang.StringUtils:isEmpty(...)@160: {0}, {1}
                  */
   156          if(StringUtils.isEmpty(getBean().getTitle())) {
   157              addError("planetGroups.error.title");
   158          }
   159          
   160          if(StringUtils.isEmpty(getBean().getHandle())) {
   161              addError("planetGroups.error.handle");
   162          }
   163          
   164          if(getBean().getHandle() != null && "all".equals(getBean().getHandle())) {
   165              addError("planetGroups.error.nameReserved");
   166          }
   167          
   168          // make sure duplicate group handles are prevented
   169      }
   170      
   171      
   172      public List<PlanetGroup> getGroups() {
   173          
                 /* 
    P/P           *  Method: List getGroups()
                  * 
                  *  Preconditions:
                  *    init'ed(this.planet)
                  *    (soft) org/apache/roller/weblogger/planet/ui/PlanetUIAction.log != null
                  * 
                  *  Presumptions:
                  *    java.util.Iterator:next(...)@178 != null
                  *    org.apache.roller.planet.pojos.Planet:getGroups(...)@176 != null
                  *    org.apache.roller.planet.pojos.PlanetGroup:getHandle(...)@182 != null
                  * 
                  *  Postconditions:
                  *    return_value == &new ArrayList(getGroups#1)
                  *    this.planet != null
                  *    new ArrayList(getGroups#1) num objects == 1
                  * 
                  *  Test Vectors:
                  *    java.lang.String:equals(...)@182: {1}, {0}
                  *    java.util.Iterator:hasNext(...)@177: {0}, {1}
                  */
   174          List<PlanetGroup> displayGroups = new ArrayList();
   175          
+  176          Iterator allgroups = getPlanet().getGroups().iterator();
   177          while (allgroups.hasNext()) {
   178              PlanetGroup agroup = (PlanetGroup) allgroups.next();
   179              
   180              // The "all" group is considered a special group and cannot be
   181              // managed independently
   182              if (!agroup.getHandle().equals("all")) {
   183                  displayGroups.add(agroup);
   184              }
   185          }
   186          
   187          return displayGroups;
   188      }
   189      
   190      
   191      public PlanetGroupsBean getBean() {
                 /* 
    P/P           *  Method: PlanetGroupsBean getBean()
                  * 
                  *  Preconditions:
                  *    init'ed(this.bean)
                  * 
                  *  Postconditions:
                  *    return_value == this.bean
                  *    init'ed(return_value)
                  */
   192          return bean;
   193      }
   194  
   195      public void setBean(PlanetGroupsBean bean) {
                 /* 
    P/P           *  Method: void setBean(PlanetGroupsBean)
                  * 
                  *  Postconditions:
                  *    this.bean == bean
                  *    init'ed(this.bean)
                  */
   196          this.bean = bean;
   197      }
   198      
   199      public PlanetGroup getGroup() {
                 /* 
    P/P           *  Method: PlanetGroup getGroup()
                  * 
                  *  Preconditions:
                  *    init'ed(this.group)
                  * 
                  *  Postconditions:
                  *    return_value == this.group
                  *    init'ed(return_value)
                  */
   200          return group;
   201      }
   202  
   203      public void setGroup(PlanetGroup group) {
                 /* 
    P/P           *  Method: void setGroup(PlanetGroup)
                  * 
                  *  Postconditions:
                  *    this.group == group
                  *    init'ed(this.group)
                  */
   204          this.group = group;
   205      }
   206      
   207  }








SofCheck Inspector Build Version : 2.18479
PlanetGroups.java 2009-Jan-02 14:25:26
PlanetGroups.class 2009-Sep-04 03:12:46