File Source: Profile.java

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   *  contributor license agreements.  The ASF licenses this file to You
     4   * under the Apache License, Version 2.0 (the "License"); you may not
     5   * 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.  For additional information regarding
    15   * copyright in this work, please see the NOTICE file in the top level
    16   * directory of this distribution.
    17   */
    18  
    19  package org.apache.roller.weblogger.ui.struts2.core;
    20  
    21  import org.apache.commons.lang.StringUtils;
    22  import org.apache.commons.logging.Log;
    23  import org.apache.commons.logging.LogFactory;
    24  import org.apache.roller.weblogger.WebloggerException;
    25  import org.apache.roller.weblogger.business.WebloggerFactory;
    26  import org.apache.roller.weblogger.business.UserManager;
    27  import org.apache.roller.weblogger.pojos.User;
    28  import org.apache.roller.weblogger.ui.struts2.util.UIAction;
    29  import org.apache.struts2.interceptor.validation.SkipValidation;
    30  
    31  
    32  /**
    33   * Allows user to edit his/her profile.
    34   *
    35   * TODO: check on the impact of deleting that cookieLogin stuff
    36   */
    37  public class Profile extends UIAction {
    38      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.ui.struts2.core.Profile__static_init
              * 
              *  Postconditions:
              *    init'ed(log)
              */
    39      private static Log log = LogFactory.getLog(Profile.class);
    40      
    41      private ProfileBean bean = new ProfileBean();
    42      
    43      
             /* 
    P/P       *  Method: void org.apache.roller.weblogger.ui.struts2.core.Profile()
              * 
              *  Postconditions:
              *    this.bean == &new ProfileBean(Profile#1)
              *    this.pageTitle == &"yourProfile.title"
              *    new ProfileBean(Profile#1) num objects == 1
              *    this.bean.emailAddress == null
              *    this.bean.fullName == null
              *    this.bean.id == null
              *    this.bean.locale == null
              *    this.bean.password == null
              *    this.bean.passwordConfirm == null
              *    this.bean.passwordText == null
              *    ...
              */
    44      public Profile() {
    45          this.pageTitle = "yourProfile.title";
    46      }
    47      
    48      // override default security, we do not require an action weblog
    49      public boolean isWeblogRequired() {
                 /* 
    P/P           *  Method: bool isWeblogRequired()
                  * 
                  *  Postconditions:
                  *    return_value == 0
                  */
    50          return false;
    51      }
    52      
    53      
    54      @SkipValidation
    55      public String execute() {
    56          
                 /* 
    P/P           *  Method: String execute()
                  * 
                  *  Preconditions:
                  *    this.bean != null
                  * 
                  *  Presumptions:
                  *    org.apache.roller.weblogger.ui.struts2.core.Profile:getAuthenticatedUser(...)@57 != null
                  * 
                  *  Postconditions:
                  *    return_value == &"input"
                  *    init'ed(this.bean.emailAddress)
                  *    init'ed(this.bean.fullName)
                  *    init'ed(this.bean.id)
                  *    init'ed(this.bean.locale)
                  *    init'ed(this.bean.password)
                  *    this.bean.passwordConfirm == null
                  *    this.bean.passwordText == null
                  *    init'ed(this.bean.screenName)
                  *    init'ed(this.bean.timeZone)
                  *    ...
                  */
    57          User ud = getAuthenticatedUser();
    58          
    59          // load up the form from the users existing profile data
+   60          getBean().copyFrom(ud);
    61          getBean().setPasswordText(null);
    62          getBean().setPasswordConfirm(null);
    63          getBean().setLocale(ud.getLocale());
    64          getBean().setTimeZone(ud.getTimeZone());
    65  
    66          return INPUT;
    67      }
    68      
    69      
    70      public String save() {
    71          
                 /* 
    P/P           *  Method: String save()
                  * 
                  *  Preconditions:
                  *    (soft) log != null
                  *    (soft) this.bean != null
                  *    (soft) init'ed(this.bean.emailAddress)
                  *    (soft) init'ed(this.bean.fullName)
                  *    (soft) init'ed(this.bean.locale)
                  *    (soft) init'ed(this.bean.passwordConfirm)
                  *    (soft) this.bean.passwordText != null
                  *    (soft) init'ed(this.bean.screenName)
                  *    (soft) init'ed(this.bean.timeZone)
                  * 
                  *  Presumptions:
                  *    org.apache.roller.weblogger.business.Weblogger:getUserManager(...)@97 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@97 != null
                  *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@99 != null
                  *    org.apache.roller.weblogger.ui.struts2.core.Profile:getAuthenticatedUser(...)@76 != null
                  * 
                  *  Postconditions:
                  *    return_value in Addr_Set{&"success",&"input"}
                  * 
                  *  Test Vectors:
                  *    org.apache.commons.lang.StringUtils:isEmpty(...)@86: {1}, {0}
                  *    org.apache.roller.weblogger.ui.struts2.core.Profile:hasActionErrors(...)@74: {1}, {0}
                  */
    72          myValidate();
    73          
    74          if (!hasActionErrors()) {
    75              // We ONLY modify the user currently logged in
    76              User existingUser = getAuthenticatedUser();
    77              
    78              // We want to be VERY selective about what data gets updated
    79              existingUser.setScreenName(getBean().getScreenName());
    80              existingUser.setFullName(getBean().getFullName());
    81              existingUser.setEmailAddress(getBean().getEmailAddress());
    82              existingUser.setLocale(getBean().getLocale());
    83              existingUser.setTimeZone(getBean().getTimeZone());
    84              
    85              // If user set both password and passwordConfirm then reset password
    86              if (!StringUtils.isEmpty(getBean().getPasswordText()) && 
    87                      !StringUtils.isEmpty(getBean().getPasswordConfirm())) {
    88                  try {
    89                      existingUser.resetPassword(getBean().getPasswordText());
    90                  } catch (WebloggerException e) {
    91                      addMessage("yourProfile.passwordResetError");
    92                  }
    93              }
    94              
    95              try {
    96                  // save the updated profile
    97                  UserManager mgr = WebloggerFactory.getWeblogger().getUserManager();
    98                  mgr.saveUser(existingUser);
    99                  WebloggerFactory.getWeblogger().flush();
   100                  
   101                  // TODO: i18n
   102                  addMessage("profile updated.");
   103                  
   104                  return SUCCESS;
   105                  
   106              } catch (WebloggerException ex) {
   107                  log.error("ERROR in action", ex);
   108                  // TODO: i18n
   109                  addError("unexpected error doing profile save");
   110              }
   111              
   112          }
   113          
   114          return INPUT;
   115      }
   116      
   117      
   118      public void myValidate() {
   119          
   120          // check that passwords match if they were specified
                 /* 
    P/P           *  Method: void myValidate()
                  * 
                  *  Preconditions:
                  *    this.bean != null
                  *    (soft) init'ed(this.bean.passwordConfirm)
                  *    (soft) this.bean.passwordText != null
                  * 
                  *  Test Vectors:
                  *    java.lang.String:equals(...)@122: {1}, {0}
                  *    org.apache.commons.lang.StringUtils:isEmpty(...)@121: {1}, {0}
                  */
   121          if(!StringUtils.isEmpty(getBean().getPasswordText())) {
   122              if(!getBean().getPasswordText().equals(getBean().getPasswordConfirm())) {
   123                  addError("Register.error.passowordMismatch");
   124              }
   125          }
   126      }
   127      
   128      
   129      public ProfileBean getBean() {
                 /* 
    P/P           *  Method: ProfileBean getBean()
                  * 
                  *  Preconditions:
                  *    init'ed(this.bean)
                  * 
                  *  Postconditions:
                  *    return_value == this.bean
                  *    init'ed(return_value)
                  */
   130          return bean;
   131      }
   132  
   133      public void setBean(ProfileBean bean) {
                 /* 
    P/P           *  Method: void setBean(ProfileBean)
                  * 
                  *  Postconditions:
                  *    this.bean == bean
                  *    init'ed(this.bean)
                  */
   134          this.bean = bean;
   135      }
   136      
   137  }








SofCheck Inspector Build Version : 2.18479
Profile.java 2009-Jan-02 14:24:48
Profile.class 2009-Sep-04 03:12:45