KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > formview > usecases > action > ProjectAction


1 package net.sourceforge.formview.usecases.action;
2
3 import java.util.Collection JavaDoc;
4 import java.util.Iterator JavaDoc;
5
6 import javax.servlet.http.HttpServletRequest JavaDoc;
7 import javax.servlet.http.HttpServletResponse JavaDoc;
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 JavaDoc request,
31               HttpServletResponse JavaDoc response) throws Exception JavaDoc {
32         ProjectForm form = (ProjectForm)actionForm;
33         // Load Project if Id project is null (UPDATE) and do nothing otherwise
34
if (form.getId() != null && form.getId().length() > 0) {
35             // UPDATE
36
// Load Project with id get project ID and update form with value of project.
37
ProjectDTO projectDTO = ProjectService.findProjectById(form.getId());
38             BeanUtils.copyProperties(form, projectDTO);
39             // Manager
40
ManagerDTO manager = projectDTO.getManager();
41             if (manager != null)
42                 form.setManagerId(manager.getId());
43             // List of developers
44
Collection JavaDoc developers = projectDTO.getDevelopers();
45             if (developers != null) {
46                 String JavaDoc[] developersId = new String JavaDoc[developers.size()];
47                 String JavaDoc[] developersName = new String JavaDoc[developers.size()];
48                 int i=0;
49                 for (Iterator JavaDoc 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             // CREATE
61
form.setDispatch("create");
62         }
63         return display(mapping, form, request, response);
64     }
65
66     public ActionForward display(ActionMapping mapping, ActionForm actionForm, HttpServletRequest JavaDoc request,
67                   HttpServletResponse JavaDoc response) throws Exception JavaDoc {
68         // Load list of managers and save it into request
69
Collection JavaDoc managerList = ProjectService.findAllManager();
70         request.setAttribute("managerList", managerList);
71         return mapping.findForward("display");
72     }
73     
74     public ActionForward create(ActionMapping mapping, ActionForm actionForm, HttpServletRequest JavaDoc request,
75               HttpServletResponse JavaDoc response) throws Exception JavaDoc {
76         ProjectForm form = (ProjectForm)actionForm;
77         // Validate Form
78
ActionErrors errors = form.validate(mapping, request);
79         if (!errors.isEmpty()) {
80             // Form is not valid, Save errors and reload form
81
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         // List of Developer
89
if (form.getDevelopersId() != null) {
90             for (int i = 0; i < form.getDevelopersId().length; i++) {
91                 String JavaDoc id = form.getDevelopersId()[i];
92                 String JavaDoc name = form.getDevelopersName()[i];
93                 projectDTO.addDeveloper(new DeveloperDTO(id, name));
94             }
95         }
96         
97         form.setId(projectDTO.getId());
98         // Message save=ok
99
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 JavaDoc request,
109               HttpServletResponse JavaDoc response) throws Exception JavaDoc {
110         ProjectForm form = (ProjectForm)actionForm;
111         if (form.getId() != null && form.getId().length() > 0) {
112             // Validate Form
113
ActionErrors errors = form.validate(mapping, request);
114             if (!errors.isEmpty()) {
115                 // Form is not valid, Save errors and reload form
116
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             // List of Developer
124
if (form.getDevelopersId() != null) {
125                 for (int i = 0; i < form.getDevelopersId().length; i++) {
126                     String JavaDoc id = form.getDevelopersId()[i];
127                     String JavaDoc name = form.getDevelopersName()[i];
128                     projectDTO.addDeveloper(new DeveloperDTO(id, name));
129                 }
130             }
131             // Message save=ok
132
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 JavaDoc request,
142               HttpServletResponse JavaDoc response) throws Exception JavaDoc {
143         ProjectForm form = (ProjectForm)actionForm;
144         String JavaDoc developerIndex = form.getDeveloperIndexToUpdate();
145         Integer JavaDoc index = (Integer JavaDoc)ConvertUtils.convert(developerIndex, Integer JavaDoc.class);
146         
147         // Save state and formview
148
WEBFormViewUtil.saveState(request, getServlet().getServletContext(), FormViewConstants.STATE_UPDATE);
149         WEBFormViewUtil.saveFormView(request, getServlet().getServletContext(), mapping.getPath());
150         
151         // Set field name=developersName with index=index to READ-WRITE
152
// And Save it into request
153
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