PageRenderTime 327ms CodeModel.GetById 115ms app.highlight 15ms RepoModel.GetById 106ms app.codeStats 0ms

/src/main/java/com/google/ie/business/service/impl/UserServiceImpl.java

http://thoughtsite.googlecode.com/
Java | 207 lines | 130 code | 22 blank | 55 comment | 30 complexity | 02e594d8288154b895809f687b02b560 MD5 | raw file
  1/* Copyright 2010 Google Inc.
  2 * 
  3 * Licensed under the Apache License, Version 2.0 (the "License");
  4 * you may not use this file except in compliance with the License.
  5 * You may obtain a copy of the License at
  6 * 
  7 *      http://www.apache.org/licenses/LICENSE-2.0
  8 * 
  9 * Unless required by applicable law or agreed to in writing, software
 10 * distributed under the License is distributed on an "AS IS" BASIS.
 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12 * See the License for the specific language governing permissions and
 13 * limitations under the License
 14 */
 15
 16package com.google.ie.business.service.impl;
 17
 18import com.google.ie.business.dao.UserDao;
 19import com.google.ie.business.dao.impl.DaoConstants;
 20import com.google.ie.business.domain.User;
 21import com.google.ie.business.service.ServiceConstants;
 22import com.google.ie.business.service.UserService;
 23import com.google.ie.common.util.SearchUtility;
 24import com.google.ie.dto.RetrievalInfo;
 25
 26import org.apache.commons.lang.StringUtils;
 27import org.apache.log4j.Logger;
 28import org.springframework.beans.factory.annotation.Autowired;
 29
 30import java.util.HashSet;
 31import java.util.List;
 32import java.util.Set;
 33
 34/**
 35 * A service implementation of the UserService
 36 * 
 37 * @author Charanjeet singh
 38 */
 39public class UserServiceImpl implements UserService {
 40    private static final Logger LOGGER = Logger.getLogger(UserServiceImpl.class);
 41    @Autowired
 42    private UserDao userDao;
 43
 44    @Override
 45    public User saveUser(User user) {
 46        User savedUser = null;
 47        if (user == null) {
 48            return null;
 49        }
 50        savedUser = userDao.saveUser(user);
 51        return savedUser;
 52    }
 53
 54    /**
 55     * Getter method for user dao.
 56     * 
 57     * @return
 58     */
 59    public UserDao getUserDao() {
 60        return userDao;
 61    }
 62
 63    /**
 64     * Setter method for user dao.
 65     * 
 66     * @param userDao
 67     */
 68    public void setUserDao(UserDao userDao) {
 69        this.userDao = userDao;
 70    }
 71
 72    @Override
 73    public User getUserById(String id) {
 74        if (id == null) {
 75            return null;
 76        }
 77        return userDao.getUserById(id);
 78    }
 79
 80    @Override
 81    public User addOrUpdateUser(User user) {
 82        /* Get the user from datastore */
 83        User userFromDatastore = getUserById(user.getId());
 84        if (userFromDatastore != null) {
 85            LOGGER.info("User found. Updating it with user info from GFC");
 86            /* In case the user is banned,return null */
 87            if (userFromDatastore.getStatus() != null
 88                            && userFromDatastore.getStatus().equalsIgnoreCase(User.STATUS_BANNED)) {
 89                return null;
 90            }
 91            userFromDatastore.setDisplayName(user.getDisplayName());
 92            userFromDatastore.setThumbnailUrl(user.getThumbnailUrl());
 93            if (!StringUtils.isBlank(user.getEmailId())) {
 94                userFromDatastore.setEmailId(user.getEmailId());
 95            }
 96            /* Set of permitted status for the user entity */
 97            Set<String> setOfUserStatus = new HashSet<String>();
 98            setOfUserStatus.add(User.STATUS_ACTIVE);
 99            setOfUserStatus.add(User.STATUS_BANNED);
100            /* Check if the status is the one permissible */
101            if (!StringUtils.isBlank(user.getStatus())
102                            && setOfUserStatus.contains(user.getStatus())) {
103                userFromDatastore.setStatus(user.getStatus());
104            } else {
105                userFromDatastore.setStatus(User.STATUS_ACTIVE);
106            }
107            userFromDatastore = userDao.saveUser(userFromDatastore);
108
109            /* Make user object searchable by adding index */
110            SearchUtility.indexEntity(userFromDatastore);
111
112        } else {
113            /*
114             * Since the user does not exist in data store ,save the details
115             * of the new user.
116             */
117            if (StringUtils.isBlank(user.getRoleName())) {
118                user.setRoleName(User.ROLE_USER);
119            }
120            user.setStatus(User.STATUS_ACTIVE);
121            LOGGER.info("Saving new user");
122            userFromDatastore = userDao.saveUser(user);
123        }
124        return userFromDatastore;
125    }
126
127    @Override
128    public User getUserByPrimaryKey(String key) {
129        return userDao.findEntityByPrimaryKey(User.class, key);
130    }
131
132    @Override
133    public User banUser(User user) {
134        User savedUser = null;
135        if (user != null) {
136            /* Get the user from datastore */
137            User userFromDatastore = userDao.findEntityByPrimaryKey(User.class, user.getUserKey());
138            userFromDatastore.setStatus(User.STATUS_BANNED);
139            savedUser = userDao.saveUser(userFromDatastore);
140            /* Delete index of user object */
141            SearchUtility.deleteEntityIndex(userFromDatastore);
142
143        }
144        return savedUser;
145    }
146
147    @Override
148    public User activate(User user) {
149        User savedUser = null;
150        if (user != null) {
151            /* Get the user from datastore */
152            User userFromDatastore = userDao.findEntityByPrimaryKey(User.class, user.getUserKey());
153            userFromDatastore.setStatus(User.STATUS_ACTIVE);
154            savedUser = userDao.saveUser(userFromDatastore);
155
156            /* Make user object searchable by adding index */
157            SearchUtility.indexEntity(userFromDatastore);
158
159        }
160        return savedUser;
161    }
162
163    @Override
164    public List<User> getUsers(RetrievalInfo retrievalInfo, String role) {
165        retrievalInfo = prepareRetrievalInfoForQuery(retrievalInfo);
166        return userDao.getUsers(retrievalInfo, role, null);
167    }
168
169    /**
170     * Prepares the {@link RetrievalInfo} object with values to be used as query
171     * parameters.
172     * Checks the received RetrievalInfo object attributes for valid
173     * data.Updates the attributes if they contain garbage values.If the
174     * received {@link RetrievalInfo} object is null,sets it to a new instance
175     * with its attributes set to default values.
176     * 
177     * @param retrievalInfo the {@link RetrievalInfo} object containing the
178     *        values to be used as query parameters
179     * @return the {@link RetrievalInfo} object containing the query parameters
180     */
181
182    private RetrievalInfo prepareRetrievalInfoForQuery(RetrievalInfo retrievalInfo) {
183        if (retrievalInfo == null) {
184            retrievalInfo = new RetrievalInfo();
185            retrievalInfo.setStartIndex(ServiceConstants.ZERO);
186            retrievalInfo.setNoOfRecords(ServiceConstants.USERS_LIST_DEFAULT_SIZE);
187            retrievalInfo.setOrderType(ServiceConstants.DEFAULT_USER_ORDERING_TYPE);
188            retrievalInfo.setOrderBy(ServiceConstants.USER_ORDERING_FIELD);
189        } else {
190            // Handle garbage values if any.
191            String orderBY = retrievalInfo.getOrderBy();
192            String orderType = retrievalInfo.getOrderType();
193            if (retrievalInfo.getStartIndex() < ServiceConstants.ZERO)
194                retrievalInfo.setStartIndex(ServiceConstants.ZERO);
195            if (retrievalInfo.getNoOfRecords() <= ServiceConstants.ZERO)
196                retrievalInfo.setNoOfRecords(ServiceConstants.USERS_LIST_DEFAULT_SIZE);
197            if (orderType == null || !((orderType.equals(DaoConstants.ORDERING_ASCENDING)
198                            || orderType.equals(DaoConstants.ORDERING_DESCENDING))))
199                retrievalInfo.setOrderType(ServiceConstants.DEFAULT_USER_ORDERING_TYPE);
200            if (orderBY == null) {
201                retrievalInfo.setOrderBy(ServiceConstants.USER_ORDERING_FIELD);
202            }
203        }
204        return retrievalInfo;
205    }
206}
207