/src/main/java/com/google/ie/web/controller/ProjectController.java

http://thoughtsite.googlecode.com/ · Java · 694 lines · 459 code · 36 blank · 199 comment · 118 complexity · e4f0b8a8638baae4319374c57cd5e615 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. package com.google.ie.web.controller;
  16. import static com.google.ie.web.controller.WebConstants.ERROR;
  17. import static com.google.ie.web.controller.WebConstants.SUCCESS;
  18. import static com.google.ie.web.controller.WebConstants.VIEW_STATUS;
  19. import com.google.appengine.api.datastore.Blob;
  20. import com.google.ie.business.domain.Developer;
  21. import com.google.ie.business.domain.Idea;
  22. import com.google.ie.business.domain.Project;
  23. import com.google.ie.business.domain.User;
  24. import com.google.ie.business.service.DeveloperService;
  25. import com.google.ie.business.service.IdeaService;
  26. import com.google.ie.business.service.ProjectService;
  27. import com.google.ie.business.service.UserService;
  28. import com.google.ie.common.builder.ProjectBuilder;
  29. import com.google.ie.common.editor.StringEditor;
  30. import com.google.ie.common.email.EmailManager;
  31. import com.google.ie.common.util.ReCaptchaUtility;
  32. import com.google.ie.dto.ProjectDetail;
  33. import com.google.ie.dto.RetrievalInfo;
  34. import com.google.ie.dto.ViewStatus;
  35. import org.apache.commons.lang.StringUtils;
  36. import org.apache.log4j.Logger;
  37. import org.springframework.beans.factory.annotation.Autowired;
  38. import org.springframework.beans.factory.annotation.Qualifier;
  39. import org.springframework.beans.propertyeditors.CustomBooleanEditor;
  40. import org.springframework.stereotype.Controller;
  41. import org.springframework.ui.Model;
  42. import org.springframework.validation.BindingResult;
  43. import org.springframework.validation.Validator;
  44. import org.springframework.web.bind.WebDataBinder;
  45. import org.springframework.web.bind.annotation.InitBinder;
  46. import org.springframework.web.bind.annotation.ModelAttribute;
  47. import org.springframework.web.bind.annotation.PathVariable;
  48. import org.springframework.web.bind.annotation.RequestMapping;
  49. import org.springframework.web.bind.annotation.RequestMethod;
  50. import org.springframework.web.bind.annotation.RequestParam;
  51. import org.springframework.web.bind.annotation.SessionAttributes;
  52. import org.springframework.web.multipart.MultipartFile;
  53. import java.io.IOException;
  54. import java.util.ArrayList;
  55. import java.util.HashMap;
  56. import java.util.HashSet;
  57. import java.util.Iterator;
  58. import java.util.List;
  59. import java.util.Map;
  60. import java.util.Set;
  61. import javax.servlet.http.HttpServletRequest;
  62. import javax.servlet.http.HttpSession;
  63. /**
  64. * A controller that handles request for Project entities.
  65. *
  66. * @author Charanjeet singh
  67. *
  68. */
  69. @Controller
  70. @RequestMapping("/projects")
  71. @SessionAttributes("user")
  72. public class ProjectController {
  73. private static Logger logger = Logger.getLogger(ProjectController.class);
  74. private static final String PROJECT_EMAIL_TYPE_INVITE_TO_JOIN = "joinProject";
  75. private static final String SEMICOLON = ";";
  76. private static final String NAME = "name";
  77. private static final String EMAILID = "emailId";
  78. @Autowired
  79. private ProjectService projectService;
  80. @Autowired
  81. private ProjectBuilder projectBuilder;
  82. @Autowired
  83. private IdeaService ideaService;
  84. @Autowired
  85. private DeveloperService developerService;
  86. @Autowired
  87. private UserService userService;
  88. @Autowired
  89. private ReCaptchaUtility reCaptchaUtility;
  90. @Autowired
  91. @Qualifier("projectValidator")
  92. private Validator projectValidator;
  93. /**
  94. * Register custom binders for Spring. Needed to run on app engine
  95. *
  96. * @param binder
  97. * @param request
  98. */
  99. @InitBinder
  100. public void initBinder(WebDataBinder binder) {
  101. binder.registerCustomEditor(Boolean.class, new CustomBooleanEditor(true));
  102. binder.registerCustomEditor(String.class, new StringEditor(true));
  103. }
  104. /**
  105. * Handles the request for search/list idea.
  106. *
  107. * @param model Carries data for the view
  108. * @return View name.
  109. */
  110. @RequestMapping(value = "/list", method = RequestMethod.GET)
  111. public String listProjects() {
  112. return "projects/list";
  113. }
  114. /**
  115. * Creates project for the idea identified by ideaKey.
  116. *
  117. * @param ideaKey for the {@link Idea}
  118. * @param model Carries data for the view
  119. * @return
  120. */
  121. @RequestMapping("/showForm/{ideaKey}")
  122. public String edit(@PathVariable String ideaKey, Model model) {
  123. ProjectDetail projectDetail = new ProjectDetail();
  124. projectDetail.setProject(new Project());
  125. ViewStatus viewStatus = new ViewStatus();
  126. viewStatus.addData(WebConstants.PROJECT_DETAIL, projectDetail);
  127. viewStatus.setStatus(SUCCESS);
  128. Idea idea = ideaService.getIdeaByKey(ideaKey);
  129. viewStatus.addData(WebConstants.IDEA_TITLE, idea.getTitle());
  130. model.addAttribute(VIEW_STATUS, viewStatus);
  131. model.addAttribute("ideaKey", ideaKey);
  132. return "projects/edit";
  133. }
  134. /**
  135. * Displays the project details identified by projectKey
  136. *
  137. * @param projectKey the id of {@link Project} to display
  138. * @param model Carries data for the view
  139. * @param req reference of the {@link HttpServletRequest}
  140. * @return
  141. */
  142. @RequestMapping("/show/{projectKey}")
  143. public String show(@PathVariable String projectKey, Map<String, Object> model,
  144. HttpServletRequest req) {
  145. ViewStatus viewStatus = new ViewStatus();
  146. User user = null;
  147. if (req.getSession(true).getAttribute(WebConstants.USER) != null)
  148. user = (User) req.getSession(true).getAttribute(WebConstants.USER);
  149. ProjectDetail projectDetail = projectBuilder.getProjectDetail(projectKey);
  150. if (null != projectDetail && null != projectDetail.getProject()) {
  151. Project project = projectDetail.getProject();
  152. Idea associatedIdea = ideaService.getIdeaByKey(project.getIdeaKey());
  153. /*
  154. * Set containing the status for which the idea should not be
  155. * displayed
  156. */
  157. Set<String> setOfStatus = new HashSet<String>();
  158. setOfStatus.add(Idea.STATUS_DELETED);
  159. setOfStatus.add(Idea.STATUS_OBJECTIONABLE);
  160. /*
  161. * If the idea has status contained in the set created above,set the
  162. * idea key for the project to null
  163. */
  164. if (setOfStatus.contains(associatedIdea.getStatus())) {
  165. project.setIdeaKey(null);
  166. logger.debug("The idea is either deleted or objectionable," +
  167. "hence setting the idea key for project to null");
  168. } else {
  169. projectDetail.setIdeaTitle(associatedIdea.getTitle());
  170. }
  171. if (user != null)
  172. logger.debug("Checking if project is editable to the user with userkey ="
  173. + user.getUserKey());
  174. projectDetail.setProjectEditable(isProjectEditable(projectDetail, user));
  175. logger.info("projectDetail.projectEditable is set to ="
  176. + projectDetail.isProjectEditable());
  177. viewStatus.addData(WebConstants.PROJECT_DETAIL, projectDetail);
  178. viewStatus.setStatus(SUCCESS);
  179. } else {
  180. viewStatus.setStatus(ERROR);
  181. viewStatus.addMessage(ERROR, WebConstants.RECORD_NOT_FOUND);
  182. }
  183. model.put(VIEW_STATUS, viewStatus);
  184. return "projects/show";
  185. }
  186. /**
  187. * Check for project edit-able attribute on the basis of logged in user.
  188. *
  189. * @param projectDetail {@link ProjectDetail} to edit
  190. * @return boolean status of the action
  191. */
  192. private boolean isProjectEditable(ProjectDetail projectDetail, User user) {
  193. boolean isEditable = false;
  194. if (projectDetail.getProject() != null) {
  195. if (user != null) {
  196. if (projectDetail.getUser() != null) {
  197. if (user.getUserKey() != null
  198. && user.getUserKey().equalsIgnoreCase(
  199. projectDetail.getUser().getUserKey())) {
  200. isEditable = true;
  201. }
  202. }
  203. for (Developer developer : projectDetail.getDevelopers()) {
  204. logger.debug("developer's status =" + developer.getStatus());
  205. if (user.getUserKey().equalsIgnoreCase(developer.getUserKey())) {
  206. isEditable = true;
  207. break;
  208. }
  209. }
  210. }
  211. }
  212. return isEditable;
  213. }
  214. /**
  215. * Handles request for editing an project. Validates the Project details
  216. * and saves the project if no validation errors are found.
  217. *
  218. * @return name of project detail JSP to be shown.
  219. */
  220. @RequestMapping("/editProject/{projectKey}")
  221. public String editProject(@PathVariable String projectKey,
  222. HttpSession session,
  223. Map<String, Object> model) {
  224. User user = (User) session.getAttribute(WebConstants.USER);
  225. ViewStatus viewStatus = new ViewStatus();
  226. ProjectDetail projectDetail = projectBuilder.getProjectDetail(projectKey);
  227. projectDetail.setProjectEditable(isProjectEditable(projectDetail, user));
  228. if (null != projectDetail.getProject()) {
  229. projectDetail.setIdeaTitle(ideaService.getIdeaByKey(
  230. projectDetail.getProject().getIdeaKey()).getTitle());
  231. viewStatus.addData(WebConstants.PROJECT_DETAIL, projectDetail);
  232. viewStatus.setStatus(SUCCESS);
  233. } else {
  234. viewStatus.setStatus(ERROR);
  235. viewStatus.addMessage(ERROR, WebConstants.RECORD_NOT_FOUND);
  236. }
  237. Idea idea = ideaService.getIdeaByKey(projectDetail.getProject().getIdeaKey());
  238. model.put(VIEW_STATUS, viewStatus);
  239. viewStatus.addData(WebConstants.IDEA_TITLE, idea.getTitle());
  240. model.put("ideaKey", projectDetail.getProject().getIdeaKey());
  241. model.put("projectKey", projectDetail.getProject().getKey());
  242. model.put("editProject", true);
  243. return "projects/edit";
  244. }
  245. /**
  246. * Handles request for creating an project. Validates the Project details
  247. * and saves the project if no validation errors are found.
  248. *
  249. * @return name of project detail JSP to be shown.
  250. */
  251. @RequestMapping(value = "/create", method = RequestMethod.POST)
  252. public String createProject(HttpServletRequest request,
  253. @RequestParam(required = true) String ideaKey,
  254. @RequestParam(required = false) String projectKey,
  255. @RequestParam(required = false, value = "devName") String[] devNames,
  256. @RequestParam(required = false, value = "email") String[] emails,
  257. @RequestParam(required = false, value = "status") String[] statusArr,
  258. @RequestParam(required = false, value = "devKey") String[] devKeys,
  259. @RequestParam(required = false, value = "logoFile") MultipartFile logoFile,
  260. @ModelAttribute Project project,
  261. BindingResult errors,
  262. Map<String, Object> model,
  263. @RequestParam String recaptchaChallengeField,
  264. @RequestParam String recaptchaResponseField,
  265. HttpSession session) throws IOException {
  266. ViewStatus viewStatus = new ViewStatus();
  267. Boolean captchaValidation = reCaptchaUtility.verifyCaptcha(request.getRemoteAddr(),
  268. recaptchaChallengeField,
  269. recaptchaResponseField);
  270. User user = (User) session.getAttribute(WebConstants.USER);
  271. ProjectDetail projectDetail = new ProjectDetail();
  272. /* create developer list. */
  273. projectDetail.setDevelopers(createDeveloperList(devNames, emails, statusArr,
  274. devKeys, user));
  275. projectDetail.setProject(project);
  276. String ideaTitle = ideaService.getIdeaByKey(ideaKey).getTitle();
  277. projectDetail.getProject().setIdeaKey(ideaKey);
  278. if (projectKey != null) {
  279. projectDetail.getProject().setKey(projectKey);
  280. }
  281. if (!captchaValidation) {
  282. getViewStatusForInvalidCaptcha(model, viewStatus, projectDetail, ideaTitle, user);
  283. return "projects/edit";
  284. }
  285. uploadLogo(projectKey, logoFile, projectDetail);
  286. /* call projectValidator to validate projectDetail object */
  287. projectValidator.validate(projectDetail, errors);
  288. /* If the errors exist in the data being posted, return to edit page */
  289. if (errors.hasErrors()) {
  290. viewStatus = ViewStatus.createProjectErrorViewStatus(errors);
  291. model.put(WebConstants.VIEW_STATUS, viewStatus);
  292. return "projects/edit";
  293. }
  294. projectDetail = createProjectAndGetDetail(user, projectDetail, ideaTitle);
  295. if (null != projectDetail.getProject()) {
  296. viewStatus.addData(WebConstants.PROJECT_DETAIL, projectDetail);
  297. viewStatus.setStatus(SUCCESS);
  298. } else {
  299. viewStatus.setStatus(ERROR);
  300. viewStatus.addMessage(ERROR, WebConstants.RECORD_NOT_FOUND);
  301. }
  302. model.put("editProject", "");
  303. model.put(VIEW_STATUS, viewStatus);
  304. // redirect to project detail page
  305. if (null != projectKey) {
  306. return "redirect:show/" + projectKey;
  307. } else if (null != projectDetail.getProject()) {
  308. return "redirect:show/" + projectDetail.getProject().getKey();
  309. } else {
  310. return "projects/show";
  311. }
  312. }
  313. /**
  314. * Get View Status For Captcha Invalid
  315. *
  316. * @param model Carries data for the view
  317. * @param viewStatus
  318. * @param projectDetail
  319. * @param ideaTitle
  320. */
  321. private void getViewStatusForInvalidCaptcha(Map<String, Object> model, ViewStatus viewStatus,
  322. ProjectDetail projectDetail, String ideaTitle, User user) {
  323. viewStatus.addMessage(WebConstants.ERROR, WebConstants.CAPTCHA_MISMATCH);
  324. viewStatus.addData(WebConstants.IDEA_TITLE, ideaTitle);
  325. model.put("ideaKey", projectDetail.getProject().getIdeaKey());
  326. model.put("projectKey", projectDetail.getProject().getKey());
  327. /*
  328. * If project is not created then delete user information from developer
  329. * list.
  330. */
  331. if (projectDetail.getProject().getKey() == null) {
  332. Developer developerToDelete = null;
  333. for (Developer developer : projectDetail.getDevelopers()) {
  334. if (user.getDisplayName() != null && user.getEmailId() != null) {
  335. if (user.getDisplayName().equalsIgnoreCase(developer.getName())
  336. && user.getEmailId().equalsIgnoreCase(developer.getEmailId())) {
  337. developerToDelete = developer;
  338. break;
  339. }
  340. }
  341. }
  342. if (developerToDelete != null) {
  343. projectDetail.getDevelopers().remove(developerToDelete);
  344. }
  345. } else {
  346. model.put("editProject", true);
  347. }
  348. viewStatus.addData(WebConstants.PROJECT_DETAIL, projectDetail);
  349. model.put(VIEW_STATUS, viewStatus);
  350. }
  351. /**
  352. * Creates the project and retrieves the project details.
  353. *
  354. * @param user creating the project.
  355. * @param projectDetail
  356. * @param ideaTitle
  357. * @return
  358. */
  359. private ProjectDetail createProjectAndGetDetail(User user, ProjectDetail projectDetail,
  360. String ideaTitle) {
  361. Project project = projectService.createOrUpdateProject(projectDetail.getProject(), user);
  362. // handle developers functionality.
  363. handleDeveloper(projectDetail.getDevelopers(), user, project);
  364. projectDetail = projectBuilder.getProjectDetail(project.getKey());
  365. projectDetail.setIdeaTitle(ideaTitle);
  366. projectDetail.setProjectEditable(isProjectEditable(projectDetail, user));
  367. return projectDetail;
  368. }
  369. /**
  370. * @param projectKey
  371. * @param logoFile
  372. * @param projectDetail
  373. * @throws IOException
  374. */
  375. private void uploadLogo(String projectKey, MultipartFile logoFile, ProjectDetail projectDetail)
  376. throws IOException {
  377. if (logoFile != null && logoFile.getBytes().length > 0) {
  378. try {
  379. Blob file = new Blob(logoFile.getBytes());
  380. projectDetail.getProject().setLogo(file);
  381. } catch (IOException e) {
  382. logger.error("couldn't find an Image at " + logoFile, e);
  383. }
  384. } else {
  385. Project project1 = new Project();
  386. if (projectService.getProjectById(projectKey) != null)
  387. project1 = projectService.getProjectById(projectKey);
  388. if (project1.getLogo() != null && project1.getLogo().getBytes().length > 0) {
  389. Blob file = new Blob(project1.getLogo().getBytes());
  390. projectDetail.getProject().setLogo(file);
  391. }
  392. }
  393. }
  394. /**
  395. * create Developer List
  396. *
  397. * @param devName
  398. * @param email
  399. * @param status
  400. * @param devKey
  401. * @param user
  402. * @return List<Developer>
  403. */
  404. private List<Developer> createDeveloperList(String[] devName, String[] email, String[] status,
  405. String[] devKey, User user) {
  406. List<Developer> developers = new ArrayList<Developer>();
  407. Set<String> emailsSet = new HashSet<String>();
  408. if ((devName != null && devName.length > 0) && (email != null && email.length > 0)) {
  409. Developer developer = null;
  410. for (int i = 0; i < devName.length; i++) {
  411. if (validDeveloperData(devName, email, i)) {
  412. // Check if email ids are unique
  413. boolean unique = emailsSet.add(email[i]);
  414. if (unique) {
  415. developer = new Developer();
  416. developer.setEmailId(email[i]);
  417. developer.setName(devName[i]);
  418. // If devKey is not present, it is a create request
  419. if (devKey[i] != null && devKey[i].trim().length() > 0) {
  420. developer.setKey(devKey[i]);
  421. }
  422. // If status is not present, it is a create request
  423. if (status[i] != null && status[i].trim().length() > 0) {
  424. developer.setStatus(status[i]);
  425. }
  426. // While editing, set user key into developer object, if
  427. // developer's email id is same as users email id.
  428. if (developer.getEmailId().equals(user.getEmailId())) {
  429. // If user explicitly adding himself as a developer
  430. // while creating the project then set its user key
  431. // and status as accepted.
  432. developer.setUserKey(user.getUserKey());
  433. developer.setStatus(Developer.STATUS_REQUEST_ACCEPTED);
  434. }
  435. developers.add(developer);
  436. }
  437. }
  438. }
  439. }
  440. // Check if creator has not already added himself as developer.
  441. boolean unique = emailsSet.add(user.getEmailId());
  442. if (unique) {
  443. Developer developer = new Developer();
  444. developer.setEmailId(user.getEmailId());
  445. developer.setName(user.getDisplayName());
  446. developer.setUserKey(user.getUserKey());
  447. developer.setStatus(Developer.STATUS_REQUEST_ACCEPTED);
  448. logger.debug("Added project creator to it's developer list.");
  449. developers.add(developer);
  450. }
  451. return developers;
  452. }
  453. /**
  454. * check for valid Developer Data.
  455. *
  456. * @param devName
  457. * @param email
  458. * @param index
  459. * @return boolean
  460. */
  461. private boolean validDeveloperData(String[] devName, String[] email, int index) {
  462. if (devName.length > index && email.length > index) {
  463. String name = devName[index];
  464. String emailId = email[index];
  465. if ((name != null && name.trim().length() > 0 && !name.trim().equals(NAME))
  466. && (emailId != null && emailId.trim().length() > 0 && !emailId.trim()
  467. .equals(EMAILID))) {
  468. return true;
  469. }
  470. }
  471. return false;
  472. }
  473. /**
  474. * This method check for new user and save them and send mail for joining
  475. * the project.
  476. *
  477. * @param developers list of developer
  478. * @param user
  479. * @param project
  480. * @param isUpdateRequest
  481. * @return List<Developer>
  482. */
  483. private List<Developer> handleDeveloper(List<Developer> developers, User user, Project project) {
  484. List<Developer> updatedDevelopers = new ArrayList<Developer>();
  485. List<String> emailList = new ArrayList<String>();
  486. for (Developer developer : developers) {
  487. logger.debug("Developer status =" + developer.getStatus() + " , user.userkey="
  488. + user.getUserKey() + " ,developer.userkey =" + developer.getUserKey());
  489. if (developer.getStatus().equalsIgnoreCase(Developer.STATUS_JOIN_REQUEST)) {
  490. developer.setStatus(Developer.STATUS_REQUEST_ALLREADY_SENT);
  491. developer.setProjectKey(project.getKey());
  492. developer = developerService.saveDeveloper(developer);
  493. updatedDevelopers.add(developer);
  494. emailList.add(developer.getEmailId() + SEMICOLON + developer.getName() + SEMICOLON
  495. + developer.getKey());
  496. } else if (developer.getStatus().equalsIgnoreCase(Developer.STATUS_REQUEST_ACCEPTED)) {
  497. developer.setProjectKey(project.getKey());
  498. developer = developerService.saveDeveloper(developer);
  499. updatedDevelopers.add(developer);
  500. } else {
  501. updatedDevelopers.add(developer);
  502. }
  503. }
  504. // /* Send email to developer */
  505. if (emailList.size() > 0) {
  506. List<String> otherInfoList = new ArrayList<String>();
  507. otherInfoList.add(user.getDisplayName());
  508. otherInfoList.add(project.getName());
  509. otherInfoList.add(project.getKey());
  510. EmailManager.sendMail(PROJECT_EMAIL_TYPE_INVITE_TO_JOIN, emailList, otherInfoList);
  511. }
  512. return updatedDevelopers;
  513. }
  514. @RequestMapping("/joinProject/{projectKey}/{developerKey}/{emailId}")
  515. public String joinProject(@PathVariable String projectKey, @PathVariable String developerKey,
  516. @PathVariable String emailId,
  517. Map<String, Object> model, HttpServletRequest req) {
  518. User user = null;
  519. ProjectDetail projectDetail = projectBuilder.getProjectDetail(projectKey);
  520. Developer developer = developerService.getDeveloperById(developerKey);
  521. if (projectDetail == null || projectDetail.getProject() == null || developer == null) {
  522. return "error/data-access-error";
  523. }
  524. if (req.getSession(true).getAttribute(WebConstants.USER) != null) {
  525. user = (User) req.getSession(true).getAttribute(WebConstants.USER);
  526. if (null == user.getUserKey()) {
  527. user = userService.addOrUpdateUser(user);
  528. req.getSession(true).setAttribute(WebConstants.USER, user);
  529. }
  530. }
  531. if (user != null) {
  532. developer.setStatus(Developer.STATUS_REQUEST_ACCEPTED);
  533. developer.setUserKey(user.getUserKey());
  534. developerService.saveDeveloper(developer);
  535. ViewStatus viewStatus = new ViewStatus();
  536. viewStatus.addData(WebConstants.PROJECT_DETAIL, projectDetail);
  537. viewStatus.setStatus(WebConstants.SUCCESS);
  538. model.put(VIEW_STATUS, viewStatus);
  539. // return "projects/show";
  540. }
  541. return "users/activate";
  542. }
  543. /**
  544. * Handles the request for search/list Project.
  545. *
  546. * @param model Carries data for the view
  547. * @return View name.
  548. */
  549. // @RequestMapping(value = "/list", method = RequestMethod.GET)
  550. // public String listProjects(Map<String, Object> model) {
  551. @RequestMapping(value = "/get", method = RequestMethod.GET)
  552. public String listProjects(@ModelAttribute RetrievalInfo retrievalInfo,
  553. Map<String, Object> model) {
  554. /* Fetch the range parameters as sent in the request */
  555. long startIndex = retrievalInfo.getStartIndex();
  556. long noOfRecordsRequested = retrievalInfo.getNoOfRecords();
  557. /* Get the idea list */
  558. // List<Project> projects = new ArrayList<Project>();
  559. List<Project> projects = projectService.listProjects(retrievalInfo);
  560. Iterator<Project> iterator = projects.iterator();
  561. Project projectWithDesc = null;
  562. while (iterator.hasNext()) {
  563. projectWithDesc = iterator.next();
  564. projectWithDesc.setDescriptionAsString(projectWithDesc.getDescription());
  565. shortenFields(projectWithDesc);
  566. }
  567. /* Map of data to be inserted into the view status object */
  568. HashMap<String, Object> parameters = new HashMap<String, Object>();
  569. /* Map containing the previous and next index values */
  570. HashMap<String, Long> pagingMap = new HashMap<String, Long>();
  571. /*
  572. * If the size of the list is greater than the no. of records requested
  573. * ,set the parameter 'next' to be used as start index for the next
  574. * page retrieval.
  575. */
  576. if (projects != null && projects.size() > noOfRecordsRequested) {
  577. pagingMap.put(WebConstants.NEXT, startIndex + noOfRecordsRequested);
  578. } else {
  579. /*
  580. * If the list size is not greater than the number requested set
  581. * the 'next' parameter to minus one
  582. */
  583. pagingMap.put(WebConstants.NEXT, (long) WebConstants.MINUS_ONE);
  584. }
  585. /*
  586. * Set the parameter 'previous' to be used as the start index for the
  587. * previous page retrieval
  588. */
  589. pagingMap.put(WebConstants.PREVIOUS, startIndex - noOfRecordsRequested);
  590. /* Add the map containing the paging values to the map of parameters */
  591. parameters.put(WebConstants.PAGING, pagingMap);
  592. // Create viewStatus
  593. ViewStatus viewStatus = createTheViewStatus(projects, parameters);
  594. model.put(VIEW_STATUS, viewStatus);
  595. return "projects/list";
  596. }
  597. /**
  598. * Shortens the length of title and description fields
  599. *
  600. * @param project
  601. */
  602. private void shortenFields(Project project) {
  603. if (null != project) {
  604. /* 50 chars for title */
  605. project.setName(StringUtils.abbreviate(project.getName(), 50));
  606. /* 150 chars for description */
  607. project.setDescriptionAsString(StringUtils.abbreviate(project.getDescriptionAsString(),
  608. 150));
  609. }
  610. }
  611. /**
  612. * Create the {@link ViewStatus} object containing the data retrieved.This
  613. * object has the status codes set to success/error based on whether the
  614. * parameter list contains data or not.
  615. *
  616. * @param listOfProjects the list containing data
  617. * @param hitCount
  618. * @return object containing the data and status codes
  619. */
  620. private ViewStatus createTheViewStatus(List<?> listProjects, Map<String, ?> parameters) {
  621. ViewStatus viewStatus = new ViewStatus();
  622. /*
  623. * If the list of ideas is not null and at least one idea
  624. * exists create the view status object with status as 'success' and
  625. * also set the data.
  626. */
  627. if (listProjects != null && listProjects.size() > WebConstants.ZERO) {
  628. viewStatus.setStatus(SUCCESS);
  629. viewStatus.addData(WebConstants.PROJECTS, listProjects);
  630. viewStatus.addData(WebConstants.MY_IDEAS_COUNT, listProjects.size());
  631. if (parameters != null) {
  632. Iterator<String> keysIter = parameters.keySet().iterator();
  633. String objectKey = null;
  634. while (keysIter.hasNext()) {
  635. objectKey = keysIter.next();
  636. viewStatus.addData(objectKey, parameters.get(objectKey));
  637. }
  638. }
  639. } else {/* In case the idea list is null or empty */
  640. viewStatus.setStatus(WebConstants.ERROR);
  641. viewStatus.addMessage(WebConstants.PROJECTS, WebConstants.NO_RECORDS_FOUND);
  642. }
  643. return viewStatus;
  644. }
  645. /**
  646. * @param projectService the projectService to set
  647. */
  648. public void setProjectService(ProjectService projectService) {
  649. this.projectService = projectService;
  650. }
  651. /**
  652. *
  653. * @return the projectService
  654. */
  655. public ProjectService getProjectService() {
  656. return projectService;
  657. }
  658. }