001: package com.technoetic.xplanner.actions;
002:
003: import com.technoetic.xplanner.domain.Person;
004: import com.technoetic.xplanner.domain.Project;
005: import com.technoetic.xplanner.domain.repository.ObjectRepository;
006: import com.technoetic.xplanner.forms.ProjectEditorForm;
007: import net.sf.hibernate.Session;
008: import org.apache.log4j.Logger;
009: import org.apache.struts.action.ActionForm;
010: import org.apache.struts.action.ActionForward;
011: import org.apache.struts.action.ActionMapping;
012:
013: import javax.servlet.ServletException;
014: import javax.servlet.http.HttpServletRequest;
015: import javax.servlet.http.HttpServletResponse;
016: import java.util.Collection;
017: import java.util.Iterator;
018:
019: /**
020: * User: Mateusz Prokopowicz
021: * Date: Dec 30, 2004
022: * Time: 10:43:51 AM
023: */
024: public class UpdateTimeNotificationReceivers extends AbstractAction {
025: public static final String ADD = "addTimeNotification";
026: public static final String DELETE = "delTimeNotification";
027: private static final Logger log = Logger
028: .getLogger("UpdateTimeAction");
029:
030: protected ActionForward doExecute(ActionMapping mapping,
031: ActionForm actionForm, HttpServletRequest request,
032: HttpServletResponse response) throws Exception {
033: ProjectEditorForm form = (ProjectEditorForm) actionForm;
034: try {
035: Session session = getSession(request);
036: try {
037: ActionForward forward = null;
038: if (!form.isSubmitted()) {
039: forward = new ActionForward(mapping.getInput());
040: } else {
041: forward = doAction(session, form, request, mapping);
042: }
043: populateForm(session, form, request);
044: return forward;
045: } catch (Exception ex) {
046: session.connection().rollback();
047: log.error("error", ex);
048: throw new ServletException(ex);
049: }
050: } catch (ServletException ex) {
051: throw ex;
052: } catch (Exception ex) {
053: log.error("error", ex);
054: throw new ServletException(ex);
055: }
056: }
057:
058: private ActionForward doAction(Session session,
059: ProjectEditorForm form, HttpServletRequest request,
060: ActionMapping actionMapping) throws Exception {
061: ObjectRepository projectRepository = getRepository(Project.class);
062: ObjectRepository personRepository = getRepository(Person.class);
063: Project project = (Project) projectRepository.load(Integer
064: .parseInt(form.getOid()));
065: Collection receivers = project.getNotificationReceivers();
066: if (form.getAction().equals(ADD)) {
067: if (form.getPersonToAddId() != null
068: && !form.getPersonToAddId().equals("0")) {
069: Person person = (Person) personRepository.load(Integer
070: .parseInt(form.getPersonToAddId()));
071: receivers.add(person);
072: project.setNotificationReceivers(receivers);
073: form.setPersonToAddId("0");
074: }
075: } else if (form.getAction().equals(DELETE)) {
076: if (form.getPersonToDelete() != null) {
077: String personToDelId = form.getPersonToDelete();
078: Person person = (Person) personRepository.load(Integer
079: .parseInt(personToDelId));
080: receivers.remove(person);
081: form.setPersonToDelete(null);
082: }
083: } else {
084: throw new ServletException("Unknown action: "
085: + form.getAction());
086: }
087: return new ActionForward(actionMapping.getInput());
088: }
089:
090: private void populateForm(Session session, ProjectEditorForm form,
091: HttpServletRequest request) throws Exception {
092: Project project = (Project) session.load(Project.class,
093: new Integer(form.getOid()));
094: Iterator itr = project.getNotificationReceivers().iterator();
095: while (itr.hasNext()) {
096: Person person = (Person) itr.next();
097: form.addPersonInfo("" + person.getId(), person.getUserId(),
098: person.getInitials(), person.getName());
099: }
100: }
101: }
|