PageRenderTime 2ms CodeModel.GetById 525ms app.highlight 398ms RepoModel.GetById 114ms app.codeStats 1ms

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

http://thoughtsite.googlecode.com/
Java | 273 lines | 135 code | 20 blank | 118 comment | 27 complexity | 698d8a003cb31f6a995cedd98660e5e3 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.Idea;
 19import com.google.ie.business.domain.Project;
 20import com.google.ie.business.domain.Tag;
 21import com.google.ie.business.domain.User;
 22import com.google.ie.business.service.IdeaService;
 23import com.google.ie.business.service.ProjectService;
 24import com.google.ie.business.service.ShardedCounterService;
 25import com.google.ie.business.service.TagService;
 26import com.google.ie.business.service.UserService;
 27import com.google.ie.common.exception.IdeasExchangeException;
 28import com.google.ie.dto.IdeaDetail;
 29import com.google.ie.dto.RetrievalInfo;
 30
 31import org.apache.commons.lang.StringUtils;
 32import org.springframework.beans.factory.annotation.Autowired;
 33import org.springframework.stereotype.Component;
 34
 35import java.util.ArrayList;
 36import java.util.HashMap;
 37import java.util.Iterator;
 38import java.util.List;
 39import java.util.Map;
 40
 41/**
 42 * This will be used for converting the complex idea dto
 43 * for different request flows like getIdeaForUser, getFeatured idea.
 44 * 
 45 * @author Charanjeet singh
 46 */
 47@Component
 48public class IdeaBuilder {
 49
 50    private static final int ONE = 1;
 51    private static final int ZERO = 0;
 52    @Autowired
 53    private IdeaService ideaService;
 54    @Autowired
 55    private ProjectService projectService;
 56    @Autowired
 57    private TagService tagService;
 58    @Autowired
 59    private UserService userService;
 60    @Autowired
 61    private ShardedCounterService shardedCounterService;
 62
 63    /**
 64     * Retrieves the list of published ideas along with their tags and teams by
 65     * tag names.
 66     * 
 67     * @param retrievalInfo the idea list retrieval information
 68     * @return Returns the list of IdeaDetail objects
 69     */
 70    public List<IdeaDetail> getIdeasByTagName(String tagName, RetrievalInfo retrievalInfo) {
 71        List<IdeaDetail> ideaDtoList = null;
 72        /* Fetch one more record than what is required */
 73        retrievalInfo.setNoOfRecords(retrievalInfo.getNoOfRecords() + ONE);
 74        List<Idea> ideaList = ideaService.getIdeasByTagName(tagName, retrievalInfo);
 75        if (ideaList != null && ideaList.size() > ZERO) {
 76            ideaDtoList = convertToIdeaDetailList(ideaList, true, true, false, true);
 77        }
 78        return ideaDtoList;
 79    }
 80
 81    /**
 82     * Retrieves the list of published ideas along with their tags and teams.
 83     * 
 84     * @param retrievalInfo the idea list retrieval information
 85     * @return Returns the list of IdeaDetail objects
 86     */
 87    public List<IdeaDetail> getIdeasForListing(RetrievalInfo retrievalInfo) {
 88        List<IdeaDetail> ideaDtoList = null;
 89        /* Fetch one more record than what is required */
 90        retrievalInfo.setNoOfRecords(retrievalInfo.getNoOfRecords() + ONE);
 91        List<Idea> ideaList = ideaService.getIdeas(retrievalInfo);
 92        if (ideaList != null && ideaList.size() > 0) {
 93            ideaDtoList = convertToIdeaDetailList(ideaList, true, true, true, true);
 94        }
 95        return ideaDtoList;
 96    }
 97
 98    /**
 99     * Retrieves the list of Ideas published or saved by user.
100     * 
101     * @param user the {@link User} object.
102     * @param retrievalInfo the idea list retrieval information
103     * @return Returns the list of IdeaDetail objects
104     */
105    public List<IdeaDetail> getIdeasForUser(User user, RetrievalInfo retrievalInfo) {
106        List<IdeaDetail> ideaDtoList = null;
107        /* Fetch one more record than what is required for paging purpose */
108        retrievalInfo.setNoOfRecords(retrievalInfo.getNoOfRecords() + ONE);
109        List<Idea> ideaList = ideaService.getIdeasForUser(user, retrievalInfo);
110        if (ideaList != null && ideaList.size() > ZERO) {
111            ideaDtoList = convertToIdeaDetailList(ideaList, true, true, false, true);
112        }
113        return ideaDtoList;
114    }
115
116    /**
117     * Gets idea details for the given idea key. For non admin request, only
118     * published and duplicate ideas are fetched.
119     * For admin requests, ideas with all statuses are fetched.
120     * 
121     * @param ideaKey the key of the idea whose details are to be fetched
122     * @param isAdmin boolean that indicates the request is for admin
123     * @return {@link IdeaDetail} object containing the idea details
124     * @throws IdeasExchangeException
125     */
126    public IdeaDetail getIdeaDetail(String ideaKey, boolean isAdmin) throws IdeasExchangeException {
127        Idea idea = ideaService.getIdeaByKey(ideaKey);
128        if (idea == null)
129            return null;
130        if (Idea.STATUS_PUBLISHED.equalsIgnoreCase(idea.getStatus())
131                        || Idea.STATUS_DUPLICATE.equalsIgnoreCase(idea.getStatus()) || isAdmin) {
132            return convertToIdeaDetail(idea, true, true, true, true);
133        }
134        throw new IdeasExchangeException("", "Idea is marked as objectionable or deleted");
135
136    }
137
138    /**
139     * @param shardedCounterService the shardedCounterService to set
140     */
141    public void setShardedCounterService(ShardedCounterService shardedCounterService) {
142        this.shardedCounterService = shardedCounterService;
143    }
144
145    /**
146     * @return the shardedCounterService
147     */
148    public ShardedCounterService getShardedCounterService() {
149        return shardedCounterService;
150    }
151
152    /**
153     * Converts the the list of Idea objects into list of IdeaDetail objects.
154     * 
155     * @param ideaList The list of Idea objects to be transformed into
156     *        IdeaDetail.
157     * @return Returns the list of IdeaDetail.
158     */
159    public List<IdeaDetail> convertToIdeaDetailList(List<Idea> ideaList) {
160        return convertToIdeaDetailList(ideaList, true, true, true, true);
161    }
162
163    /**
164     * Converts the the list of Idea objects into list of IdeaDetail objects.
165     * 
166     * @param ideaList The list of Idea objects to be transformed into
167     *        IdeaDetail.
168     * @return Returns the list of IdeaDetail.
169     */
170    public List<IdeaDetail> convertToIdeaDetailList(List<Idea> ideaList, boolean addUser) {
171        return convertToIdeaDetailList(ideaList, true, true, addUser, true);
172    }
173
174    /**
175     * Converts the the list of Idea objects into list of IdeaDetail objects.
176     * 
177     * @param ideaList The list of Idea objects to be transformed into
178     *        IdeaDetail.
179     * @param addTags Flag which means that also fetch Tag objects associated
180     *        with
181     *        the ideas, when set true.
182     * @param addTeams Flag which means that also fetch Team objects associated
183     *        with the ideas, when set true.
184     * @param trimLongFields
185     * @return Returns the list of IdeaDetail.
186     */
187    public List<IdeaDetail> convertToIdeaDetailList(List<Idea> ideaList, boolean addTags,
188                    boolean addTeams, boolean addUser, boolean trimLongFields) {
189        List<IdeaDetail> ideaDetailList = new ArrayList<IdeaDetail>();
190        IdeaDetail ideaDetail = null;
191        for (Idea idea : ideaList) {
192            if (trimLongFields) {
193                shortenFields(idea);
194            }
195            ideaDetail = convertToIdeaDetail(idea, addTags, addTeams, addUser, false);
196            ideaDetailList.add(ideaDetail);
197        }
198        return ideaDetailList;
199    }
200
201    /**
202     * Shortens the length of title and description fields
203     * 
204     * @param idea
205     */
206    private void shortenFields(Idea idea) {
207        if (null != idea) {
208            /* 50 chars for title */
209            idea.setTitle(StringUtils.abbreviate(idea.getTitle(), 50));
210            /* 100 chars for description */
211            idea.setDescription(StringUtils.abbreviate(idea.getDescription(), 150));
212        }
213    }
214
215    /**
216     * Converts the the Idea object to IdeaDetail object.
217     * 
218     * @param idea The Idea object to be transformed into IdeaDetail.
219     * @param addTag Flag which means that also fetch Tag objects associated
220     *        with
221     *        the idea, when set true.
222     * @param addTeam Flag which means that also fetch Team objects associated
223     *        with the ideas, when set true.
224     * @param addProjects Flag which means also fetch the Project objects
225     *        associated with the idea
226     * @return Returns the list of IdeaDetail.
227     */
228    public IdeaDetail convertToIdeaDetail(Idea idea, boolean addTag, boolean addTeam,
229                    boolean addUser, boolean addProjects) {
230        IdeaDetail ideaDetail = new IdeaDetail();
231        ideaDetail.setIdea(idea);
232        if (addTag) {
233            /*
234             * Get all tags using tag keys in idea and set them to the
235             * corresponding property in ideaDto.
236             */
237            List<Tag> tagList = tagService.getTagsByKeys(idea.getTagKeys());
238            ideaDetail.setTags(tagList);
239        }
240        if (addUser) {
241            User user = userService.getUserByPrimaryKey(idea.getCreatorKey());
242            if (user != null) {
243                /* Get reputation points for user */
244                long userPoints = shardedCounterService.getTotalPoint(idea.getCreatorKey());
245                user.setReputationPoints(userPoints);
246            }
247            ideaDetail.setUser(user);
248        }
249        /* If true add the projects associated with the idea */
250        if (addProjects) {
251            List<Project> projects = projectService.getProjectsByIdeaKey(idea.getKey());
252            /*
253             * If the list is not empty,iterate it and create a map with key of
254             * the project object as the key in the map and project name as the
255             * value.Only these two attributes are required on the idea detail
256             * page
257             */
258            if (projects != null && projects.size() > ZERO) {
259                Iterator<Project> iterator = projects.iterator();
260                Map<String, String> mapOfProjectIdAndName = new HashMap<String, String>();
261                Project project;
262                while (iterator.hasNext()) {
263                    project = iterator.next();
264                    mapOfProjectIdAndName.put(project.getKey(), project.getName());
265                }
266                ideaDetail.setMapOfProjectIdAndName(mapOfProjectIdAndName);
267            }
268        }
269        return ideaDetail;
270    }
271
272}
273