Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2010 Västra Götalandsregionen This library is free software; you can redistribute it and/or modify it under the terms of version 2.1 of the GNU Lesser General Public License as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
 
 package se.vgregion.userupdate.hook;
 
 
 import java.util.List;

Created by IntelliJ IDEA. Created: 2011-11-22 23:37

Author(s):
David Rosell
 
 public class UserUpdateAction extends Action {
     private static final Logger LOGGER = LoggerFactory.getLogger(UserUpdateAction.class);
 
     private UserLdapDao userLdapDao;
 
     @Override
     public void run(HttpServletRequest requestHttpServletResponse responsethrows ActionException {
         init();
 
         User user = lookupUser(request);
         if (user == null) {
             return;
         }
 
         try {
             UserLdapAttributes userLdapAttributes = lookupInLdap(user.getScreenName());
 
             if (!user.getScreenName().equals(userLdapAttributes.getUid())) {
                 String msg = String.format("Ldap användaren har felaktigt uid [%s] - [%s]"user.getScreenName(),
                         userLdapAttributes.getUid());
                 throw new RuntimeException(msg);
             }
 
             .updateBirthday(useruserLdapAttributes);
             .updateGender(useruserLdapAttributes);
             .updateEmail(useruserLdapAttributes);
             .updateFullName(useruserLdapAttributes);
             .updateGivenName(useruserLdapAttributes);
             .updateLastName(useruserLdapAttributes);
             .updateTitle(useruserLdapAttributes);
             .updateHsaTitle(useruserLdapAttributes);
             .updatePrescriptionCode(useruserLdapAttributes);
             .updateIsDominoUser(useruserLdapAttributes);
             .updateVgrAdmin(useruserLdapAttributes);
             .updateIsTandvard(useruserLdapAttributes);
             .updateOrganization(useruserLdapAttributes);
 
             List<UnitLdapAttributesunitLdapAttributesList = lookupUserOrganizations(userLdapAttributes);
             .updateVgrLabeledURI(userunitLdapAttributesList);
             .updateIsPrimarvard(userunitLdapAttributesList);
         } catch (Exception e) {
             log(e.getMessage(), e);
         } finally {
             // internal access only check - has to be done last
             .updateInternalAccessOnly(userrequest);
         }
     }
 
     private List<UnitLdapAttributeslookupUserOrganizations(UserLdapAttributes userLdapAttributes) {
         List<UnitLdapAttributesunitLdapAttributesList = null;
         try {
             unitLdapAttributesList = .resolve(userLdapAttributes);
         } catch (Exception ex) {
             String msg = String.format("Failed to lookup user organizations for [%s]",
                    userLdapAttributes.getUid());
            log(msgex);
        }
        return unitLdapAttributesList;
    }
    private UserLdapAttributes lookupInLdap(String uid) {
        List<UserLdapAttributesuserLdapAttributesList = .resolve(uid);
        switch (userLdapAttributesList.size()) {
            case 0: {
                String msg = String.format("Användaren [%s] hittades inte i LDAP"uid);
                throw new RuntimeException(msg);
            }
            case 1:
                return userLdapAttributesList.get(0);
            default: {
                String msg = String.format("Mer än en användaren [%s] hittades i LDAP"uid);
                throw new RuntimeException(msg);
            }
        }
    }
    private User lookupUser(HttpServletRequest request) {
        try {
            Long userId = Long.parseLong(request.getRemoteUser());
            User user = .getUser(userId);
            if (user == null) {
                throw new Exception("Användaren hittades inte");
            }
            return user;
        } catch (Exception ex) {
            String msg = String.format("Användaren med id [%s] finns inte i Liferays användar databas",
                    request.getRemoteUser());
            log(msgex);
        }
        return null;
    }
    private void init() {
        if ( == null) {
             = (UserLocalServicegetApplicationContext().getBean("userLocalService");
        }
        if ( == null) {
             = (UserLdapDaogetApplicationContext().getBean("userLdapDao");
        }
        if ( == null) {
             = (UserUpdateServicegetApplicationContext().getBean("userUpdateService");
        }
    }
        if ( == null) {
             = new ClassPathXmlApplicationContext("applicationContext.xml");
        }
        return ;
    }
    private void log(String msgThrowable ex) {
        if (.isDebugEnabled()) {
            .warn(msgex);
        } else {
            .warn(msg);
        }
    }
New to GrepCode? Check out our FAQ X