1 package net.sourceforge.formview.usecases.action; 2 3 import java.util.Collection ; 4 import java.util.Iterator ; 5 6 import javax.servlet.http.HttpServletRequest ; 7 import javax.servlet.http.HttpServletResponse ; 8 9 import org.apache.commons.beanutils.BeanUtils; 10 import org.apache.commons.beanutils.ConvertUtils; 11 import org.apache.struts.action.ActionErrors; 12 import org.apache.struts.action.ActionForm; 13 import org.apache.struts.action.ActionForward; 14 import org.apache.struts.action.ActionMapping; 15 import org.apache.struts.action.ActionMessage; 16 import org.apache.struts.action.ActionMessages; 17 18 import net.sourceforge.formview.FieldView; 19 import net.sourceforge.formview.FormViewConstants; 20 import net.sourceforge.formview.struts.action.FormViewDispatchAction; 21 import net.sourceforge.formview.usecases.dto.DeveloperDTO; 22 import net.sourceforge.formview.usecases.dto.ManagerDTO; 23 import net.sourceforge.formview.usecases.dto.ProjectDTO; 24 import net.sourceforge.formview.usecases.form.ProjectForm; 25 import net.sourceforge.formview.usecases.service.ProjectService; 26 import net.sourceforge.formview.util.WEBFormViewUtil; 27 28 public class ProjectAction extends FormViewDispatchAction { 29 30 public ActionForward load(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, 31 HttpServletResponse response) throws Exception { 32 ProjectForm form = (ProjectForm)actionForm; 33 if (form.getId() != null && form.getId().length() > 0) { 35 ProjectDTO projectDTO = ProjectService.findProjectById(form.getId()); 38 BeanUtils.copyProperties(form, projectDTO); 39 ManagerDTO manager = projectDTO.getManager(); 41 if (manager != null) 42 form.setManagerId(manager.getId()); 43 Collection developers = projectDTO.getDevelopers(); 45 if (developers != null) { 46 String [] developersId = new String [developers.size()]; 47 String [] developersName = new String [developers.size()]; 48 int i=0; 49 for (Iterator iter = developers.iterator(); iter.hasNext(); i++) { 50 DeveloperDTO developer = (DeveloperDTO) iter.next(); 51 developersId[i] = developer.getId(); 52 developersName[i] = developer.getName(); 53 } 54 form.setDevelopersId(developersId); 55 form.setDevelopersName(developersName); 56 } 57 form.setDispatch("update"); 58 } 59 else { 60 form.setDispatch("create"); 62 } 63 return display(mapping, form, request, response); 64 } 65 66 public ActionForward display(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, 67 HttpServletResponse response) throws Exception { 68 Collection managerList = ProjectService.findAllManager(); 70 request.setAttribute("managerList", managerList); 71 return mapping.findForward("display"); 72 } 73 74 public ActionForward create(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, 75 HttpServletResponse response) throws Exception { 76 ProjectForm form = (ProjectForm)actionForm; 77 ActionErrors errors = form.validate(mapping, request); 79 if (!errors.isEmpty()) { 80 addErrors(request, errors); 82 return display(mapping, actionForm, request, response); 83 } 84 ProjectDTO projectDTO = new ProjectDTO(); 85 BeanUtils.copyProperties(projectDTO, form); 86 projectDTO.setManager(ProjectService.findManagerById(form.getManagerId())); 87 ProjectService.createProject(projectDTO); 88 if (form.getDevelopersId() != null) { 90 for (int i = 0; i < form.getDevelopersId().length; i++) { 91 String id = form.getDevelopersId()[i]; 92 String name = form.getDevelopersName()[i]; 93 projectDTO.addDeveloper(new DeveloperDTO(id, name)); 94 } 95 } 96 97 form.setId(projectDTO.getId()); 98 ActionMessages messages = new ActionMessages(); 100 ActionMessage message = new ActionMessage( 101 "projectForm.create.ok"); 102 messages.add(ActionMessages.GLOBAL_MESSAGE, message); 103 saveMessages(request, messages); 104 105 return loadRead(mapping, form, request, response); 106 } 107 108 public ActionForward update(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, 109 HttpServletResponse response) throws Exception { 110 ProjectForm form = (ProjectForm)actionForm; 111 if (form.getId() != null && form.getId().length() > 0) { 112 ActionErrors errors = form.validate(mapping, request); 114 if (!errors.isEmpty()) { 115 addErrors(request, errors); 117 return display(mapping, actionForm, request, response); 118 } 119 ProjectDTO projectDTO = new ProjectDTO(); 120 BeanUtils.copyProperties(projectDTO, form); 121 projectDTO.setManager(ProjectService.findManagerById(form.getManagerId())); 122 ProjectService.updateProject(projectDTO); 123 if (form.getDevelopersId() != null) { 125 for (int i = 0; i < form.getDevelopersId().length; i++) { 126 String id = form.getDevelopersId()[i]; 127 String name = form.getDevelopersName()[i]; 128 projectDTO.addDeveloper(new DeveloperDTO(id, name)); 129 } 130 } 131 ActionMessages messages = new ActionMessages(); 133 ActionMessage message = new ActionMessage( 134 "projectForm.update.ok"); 135 messages.add(ActionMessages.GLOBAL_MESSAGE, message); 136 saveMessages(request, messages); 137 } 138 return loadRead(mapping, form, request, response); 139 } 140 141 public ActionForward loadDeveloperUpdate(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, 142 HttpServletResponse response) throws Exception { 143 ProjectForm form = (ProjectForm)actionForm; 144 String developerIndex = form.getDeveloperIndexToUpdate(); 145 Integer index = (Integer )ConvertUtils.convert(developerIndex, Integer .class); 146 147 WEBFormViewUtil.saveState(request, getServlet().getServletContext(), FormViewConstants.STATE_UPDATE); 149 WEBFormViewUtil.saveFormView(request, getServlet().getServletContext(), mapping.getPath()); 150 151 FieldView fieldView = new FieldView("developersName"); 154 fieldView.setIndex(index); 155 fieldView.setBehaviour(FormViewConstants.BEHAVIOUR_READWRITE); 156 WEBFormViewUtil.saveFieldView(request, getServlet().getServletContext(), mapping.getPath(), fieldView); 157 158 return load(mapping, form, request, response); 159 160 } 161 162 } 163 164 165 | Popular Tags |