PageRenderTime 53ms CodeModel.GetById 20ms app.highlight 11ms RepoModel.GetById 20ms app.codeStats 0ms

/src/main/java/com/google/ie/common/builder/ProjectBuilder.java

http://thoughtsite.googlecode.com/
Java | 180 lines | 95 code | 15 blank | 70 comment | 16 complexity | 2b7438ec4d8b2d6a7f46719d98c52513 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.common.builder;
 17
 18import com.google.ie.business.domain.Developer;
 19import com.google.ie.business.domain.Project;
 20import com.google.ie.business.domain.User;
 21import com.google.ie.business.service.DeveloperService;
 22import com.google.ie.business.service.ProjectService;
 23import com.google.ie.business.service.UserService;
 24import com.google.ie.common.comparator.ProjectCreationDateComparator;
 25import com.google.ie.dto.ProjectDetail;
 26import com.google.ie.dto.RetrievalInfo;
 27
 28import org.apache.commons.lang.StringUtils;
 29import org.apache.log4j.Logger;
 30import org.springframework.beans.factory.annotation.Autowired;
 31import org.springframework.stereotype.Component;
 32
 33import java.util.ArrayList;
 34import java.util.Collections;
 35import java.util.HashSet;
 36import java.util.Iterator;
 37import java.util.List;
 38import java.util.Set;
 39
 40/**
 41 * This will be used for converting the complex project dto
 42 * for different request flows like getProjectsForUser, getProjects.
 43 * 
 44 * @author gmaurya
 45 */
 46@Component
 47public class ProjectBuilder {
 48    /**
 49     * logger for logging error and data.
 50     */
 51    private static final Logger LOGGER = Logger.getLogger(ProjectBuilder.class);
 52    private static final int ZERO = 0;
 53    @Autowired
 54    private ProjectService projectService;
 55    @Autowired
 56    private UserService userService;
 57
 58    @Autowired
 59    private DeveloperService developerService;
 60
 61    /**
 62     * Retrieves the list of Projects created by a user.
 63     * 
 64     * @param user the User object.
 65     * @param retrievalInfo the idea list retrieval information.
 66     * @return Returns the list of ProjectDetail objects.
 67     */
 68    public List<ProjectDetail> getProjectsForUser(User user, RetrievalInfo retrievalInfo) {
 69        List<ProjectDetail> projectDtoList = null;
 70        Set<String> projectKeys = new HashSet<String>();
 71        /* Get developers having with the specific user key */
 72        LOGGER.debug("User key =" + user.getUserKey());
 73        List<Developer> developers = developerService.getDeveloperByUserKey(user.getUserKey(),
 74                        retrievalInfo);
 75        /* Get the Set of project keys associated with the developers */
 76        Iterator<Developer> iterator = developers.iterator();
 77        while (iterator.hasNext()) {
 78            Developer developer = iterator.next();
 79            if (!StringUtils.isBlank(developer.getProjectKey())) {
 80                Project proj = projectService.getProjectById(developer.getProjectKey());
 81                if (!proj.getStatus().equals(Project.STATUS_DELETED))
 82                    projectKeys.add(developer.getProjectKey());
 83            }
 84
 85        }
 86        LOGGER.debug("Project keys =" + projectKeys.toString());
 87        /* Get Project list by Set of project keys. */
 88        List<Project> projects = projectService.getProjects(projectKeys, retrievalInfo);
 89        Collections.sort(projects, new ProjectCreationDateComparator());
 90        /* Convert them into ProjectDetails. */
 91        if (projects != null && projects.size() > ZERO) {
 92            LOGGER.debug("Project size =" + projects.size());
 93            projectDtoList = convertToProjectDetailList(projects, true, false, true);
 94        }
 95        return projectDtoList;
 96    }
 97
 98    /**
 99     * Get the detail of a project.
100     * 
101     * @param projectKey the key of the project
102     * @return {@link ProjectDetail} object containing details
103     */
104    public ProjectDetail getProjectDetail(String projectKey) {
105        Project project = projectService.getProjectById(projectKey);
106        return convertToProjectDetail(project, true, true);
107    }
108
109    /**
110     * Converts the list of {@link Project} objects into list of
111     * {@link ProjectDetail} objects.
112     * 
113     * @param projectList the list of Project objects to be transformed into
114     *        ProjectDetail.
115     * @param addUser flag that specifies whether the User object
116     *        associated with the project is to be fetched or not
117     * @param addDevelopers flag that specifies whether the Developer objects
118     *        associated with the project are to be fetched or not
119     * @return the list of ProjectDetail.
120     */
121    public List<ProjectDetail> convertToProjectDetailList(List<Project> projectList,
122                    boolean addUser, boolean addDevelopers, boolean trimLongFields) {
123        List<ProjectDetail> projectDtoList = new ArrayList<ProjectDetail>();
124        ProjectDetail projectDto = null;
125        for (Project project : projectList) {
126            if (trimLongFields) {
127                shortenFields(project);
128            }
129            projectDto = convertToProjectDetail(project, addUser, addDevelopers);
130            projectDtoList.add(projectDto);
131        }
132        return projectDtoList;
133    }
134
135    /**
136     * Shortens the length of title and description fields
137     * 
138     * @param project
139     */
140    private void shortenFields(Project project) {
141        if (null != project) {
142            /* 50 chars for title */
143            project.setName(StringUtils.abbreviate(project.getName(), 50));
144            /* 150 chars for description */
145            project.setDescription(StringUtils.abbreviate(project.getDescription(),
146                            150));
147        }
148    }
149
150    /**
151     * Converts the the Project object to ProjectDetail object and populate data
152     * of developer,user and project.
153     * 
154     * @param project the Project object to be transformed into IdeaDetail.
155     * @param addUser flag that specifies whether the User object
156     *        associated with the project is to be fetched or not
157     * @param addDeveloper flag that specifies whether the Developer objects
158     *        associated with the project are to be fetched or not
159     * @return the list of IdeaDetail.
160     */
161    public ProjectDetail convertToProjectDetail(Project project,
162                    boolean addUser, boolean addDeveloper) {
163        ProjectDetail projectDetail = new ProjectDetail();
164        projectDetail.setProject(project);
165        if (addUser && project != null) {
166            User user = userService.getUserByPrimaryKey(project.getCreatorKey());
167            projectDetail.setUser(user);
168        }
169        if (addDeveloper && project != null) {
170            projectDetail
171                            .setDevelopers(developerService.getDevelopersByProjectKey(project
172                            .getKey()));
173        }
174        projectDetail.setDeveloperCount(projectDetail.getDevelopers().size());
175
176        return projectDetail;
177    }
178
179}
180