KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > portlets > events > action > EditRecuranceAction


1 /*
2  * Created on 19/10/2004
3  *
4  */

5 package com.dotmarketing.portlets.events.action;
6
7 import java.net.URLDecoder JavaDoc;
8 import java.util.Calendar JavaDoc;
9 import java.util.GregorianCalendar JavaDoc;
10
11 import javax.portlet.ActionRequest;
12 import javax.portlet.ActionResponse;
13 import javax.portlet.PortletConfig;
14
15 import org.apache.commons.beanutils.BeanUtils;
16 import org.apache.commons.lang.builder.EqualsBuilder;
17 import org.apache.struts.action.ActionForm;
18 import org.apache.struts.action.ActionMapping;
19
20 import com.dotmarketing.db.DotHibernate;
21 import com.dotmarketing.factories.InodeFactory;
22 import com.dotmarketing.portal.struts.DotPortletAction;
23 import com.dotmarketing.portlets.events.factories.EventFactory;
24 import com.dotmarketing.portlets.events.factories.RecuranceFactory;
25 import com.dotmarketing.portlets.events.model.Event;
26 import com.dotmarketing.portlets.events.model.Recurance;
27 import com.dotmarketing.portlets.events.struts.RecuranceForm;
28 import com.dotmarketing.portlets.facilities.model.Facility;
29 import com.dotmarketing.util.Validator;
30 import com.liferay.portal.model.User;
31 import com.liferay.portal.struts.ActionException;
32 import com.liferay.portal.util.Constants;
33 import com.liferay.util.servlet.SessionMessages;
34
35 /**
36  * @author David Torres
37  *
38  */

39 public class EditRecuranceAction extends DotPortletAction {
40     
41     public static boolean debug = false;
42     
43     public void processAction(
44              ActionMapping mapping, ActionForm form, PortletConfig config,
45              ActionRequest req, ActionResponse res)
46          throws Exception JavaDoc {
47
48         String JavaDoc cmd = (req.getParameter(Constants.CMD)!=null)? req.getParameter(Constants.CMD) : Constants.EDIT;
49
50         String JavaDoc referer = req.getParameter("referer");
51         if ((referer!=null) && (referer.length()!=0)) {
52             referer = URLDecoder.decode(referer,"UTF-8");
53         }
54
55         DotHibernate dh = new DotHibernate();
56         dh.startTransaction();
57         User user = _getUser(req);
58
59         boolean admin = EventFactory.isAnEventAdministrator(user);
60         req.setAttribute("isAdmin", new Boolean JavaDoc (admin));
61         
62         /*
63          * We are editing the recurance
64          */

65         if ((cmd != null) && cmd.equals(Constants.EDIT)) {
66             try {
67                 _editRecurance(req, res, config, form, user);
68                 setForward(req,"portlet.ext.events.edit_recurance");
69             } catch (ActionException ae) {
70                 _handleException(ae, req);
71             }
72         }
73         
74         /*
75          * If we are updating the campaign, copy the information
76          * from the struts bean to the hbm inode and run the
77          * update action and return to the list
78          */

79         else if ((cmd != null) && cmd.equals(Constants.SAVE)) {
80             try {
81
82                 if (Validator.validate(req,form,mapping)) {
83                     if (!_saveRecurance(req, res, config, form, user)) {
84                         setForward(req,"portlet.ext.events.edit_recurance");
85                         return;
86                     }
87                 }
88                 _sendToReferral(req,res,referer);
89
90             } catch (ActionException ae) {
91                 _handleException(ae, req);
92             }
93         }
94         dh.commitTransaction();
95
96     }
97
98     ///// ************** ALL METHODS HERE *************************** ////////
99

100     
101     private void _editRecurance(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user)
102         throws Exception JavaDoc {
103
104         
105         RecuranceForm rf = (RecuranceForm) form;
106         String JavaDoc eventInode = req.getParameter("parent");
107         Event e = (Event) InodeFactory.getInode(eventInode, Event.class);
108         req.setAttribute("event",e);
109         
110         Recurance r = (Recurance) InodeFactory.getChildOfClass(e, Recurance.class);
111
112         if (r.getInode() == 0) {
113             Calendar JavaDoc startC = GregorianCalendar.getInstance();
114             startC.setTime(e.getStartDate());
115             Calendar JavaDoc endC = GregorianCalendar.getInstance();
116             endC.setTime(e.getEndDate());
117             Calendar JavaDoc tempC = GregorianCalendar.getInstance();
118             tempC.set(endC.get(Calendar.YEAR), endC.get(Calendar.MONTH), endC.get(Calendar.DATE),
119                     startC.get(Calendar.HOUR_OF_DAY), startC.get(Calendar.MINUTE));
120             if (tempC.after(endC)) {
121                 r.setEnding(tempC.getTime());
122                 tempC.set(startC.get(Calendar.YEAR), startC.get(Calendar.MONTH), startC.get(Calendar.DATE),
123                         endC.get(Calendar.HOUR_OF_DAY), endC.get(Calendar.MINUTE));
124                 r.setStarting(tempC.getTime());
125             } else {
126                 r.setStarting(startC.getTime());
127                 r.setEnding(endC.getTime());
128             }
129         }
130         req.setAttribute("recurance",r);
131
132         BeanUtils.copyProperties(rf, r);
133         
134     }
135
136     private boolean _saveRecurance(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user)
137         throws Exception JavaDoc {
138
139         RecuranceForm rf = (RecuranceForm) form;
140         Recurance r = (Recurance) InodeFactory.getInode(rf.getInode(), Recurance.class);
141         if (_recuranceChanged(r, rf)) {
142             Event e = (Event) InodeFactory.getInode(rf.getParent(), Event.class);
143
144             Recurance recuranceCopy = new Recurance ();
145             BeanUtils.copyProperties(recuranceCopy, rf);
146             Facility newFacility = (Facility) InodeFactory.getParentOfClass(e, Facility.class);
147             if (!req.getParameter("continueWithConflicts").equals("true") && EventFactory.findConflicts(e, recuranceCopy, newFacility).size() > 0) {
148                 req.setAttribute("event",e);
149                 req.setAttribute("recurance",r);
150                 req.setAttribute("recuranceForm",rf);
151                 SessionMessages.add(req, "message", "message.event.recurance.has.conflicts");
152                 req.setAttribute("conflict_found", "true");
153                 return false;
154             }
155
156             BeanUtils.copyProperties(r, rf);
157             InodeFactory.saveInode(r);
158             e.setStartDate(r.getStarting());
159             e.setEndDate(r.getEnding());
160             e.setApprovalStatus(com.dotmarketing.util.Constants.EVENT_WAITING_APPROVAL_STATUS);
161             RecuranceFactory.buildRecurringEvents(r,e);
162
163             e = (Event) InodeFactory.getParentOfClass(r, Event.class);
164             if (e.getInode() > 0 )
165                 EventFactory.sendEmailNotification(e, newFacility, user, true);
166         }
167         return true;
168     }
169     
170     private boolean _recuranceChanged (Recurance r, RecuranceForm rf) {
171         EqualsBuilder eb = new EqualsBuilder ();
172         eb.append(rf.getDayOfMonth(), r.getDayOfMonth());
173         eb.append(rf.getDaysOfWeek(), r.getDaysOfWeek());
174         eb.append(rf.getStarting(), r.getStarting());
175         eb.append(rf.getEnding(), r.getEnding());
176         eb.append(rf.getInterval(), r.getInterval());
177         eb.append(rf.getOccurs(), r.getOccurs());
178         return !eb.isEquals();
179     }
180     
181 }
182
183
Popular Tags