KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > cms > myaccount > action > AddressAction


1 package com.dotmarketing.cms.myaccount.action;
2
3 import com.dotmarketing.cms.factories.PublicAddressFactory;
4 import com.dotmarketing.cms.myaccount.struts.MyAccountForm;
5 import com.dotmarketing.util.UtilMethods;
6 import com.dotmarketing.util.WebKeys;
7 import com.liferay.portal.model.Address;
8 import com.liferay.portal.model.User;
9 import org.apache.commons.logging.LogFactory;
10 import org.apache.struts.Globals;
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 import org.apache.struts.actions.DispatchAction;
18 import java.util.ArrayList JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24
25
26 public class AddressAction extends DispatchAction {
27     /*public ActionForward editAddress(ActionMapping mapping, ActionForm lf, HttpServletRequest request,
28         HttpServletResponse response) throws Exception {
29         MyAccountForm form = (MyAccountForm) lf;
30
31         User user = (User) request.getSession().getAttribute(WebKeys.CMS_USER);
32         Address address = null;
33
34         try {
35             address = PublicAddressFactory.getAddressById(form.getAddressId());
36         } catch (Exception e) {
37             LogFactory.getLog(AddressAction.class).debug("Getting new Address");
38         }
39
40         if (!"0".equals(form.getAddressId()) && !user.getUserId().equals(address.getUserId())) {
41             ActionErrors aes = new ActionErrors();
42             LogFactory.getLog(AddressAction.class).error("Invalid addressId " + form.getAddressId() +
43                 " requested by userId:" + user.getUserId());
44             LogFactory.getLog(AddressAction.class).error("Invalid Address requested by userId:" + user.getUserId());
45             aes.add(Globals.ERROR_KEY, new ActionMessage("error.invalidAddress"));
46             saveMessages(request, aes);
47             address = null;
48         }
49
50         // set up the crumbtrail
51         List list = new ArrayList();
52         Map map = new HashMap();
53         map.put("title", "Home");
54         map.put("url", "/");
55         list.add(map);
56
57         map = new HashMap();
58         map.put("title", "My Account");
59         map.put("url", "/cms/myAccount");
60         list.add(map);
61
62         map = new HashMap();
63         map.put("title", "Add/Edit Address");
64         map.put("url", "/cms/myAccount?dispatch=editAddress&id=");
65         map.put("theEnd", "true");
66         list.add(map);
67
68         request.setAttribute(com.dotmarketing.util.WebKeys.CRUMB_TRAIL, list);
69
70         if (address != null) {
71             // build User address
72             form.setAddress1(address.getStreet1());
73             form.setAddress2(address.getStreet2());
74             form.setCity(address.getCity());
75             form.setState(address.getState());
76             form.setCountry(address.getCountry());
77             form.setPhone(address.getPhone());
78             form.setZip(address.getZip());
79             form.setPhone(address.getPhone());
80             form.setDescription(address.getDescription());
81             form.setCell(address.getCell());
82             form.setPriority(address.getPriority());
83         }
84
85         return mapping.findForward("myAccountAddressPage");
86     }
87
88     public ActionForward saveAddress(ActionMapping mapping, ActionForm lf, HttpServletRequest request,
89         HttpServletResponse response) throws Exception {
90         MyAccountForm form = (MyAccountForm) lf;
91
92         ActionErrors ae = null;
93         ae = form.validate(mapping, request);
94
95         if ((ae != null) && (ae.size() > 0)) {
96             saveMessages(request, ae);
97             return mapping.findForward("myAccountAddressPage");
98         }
99
100         User user = (User) request.getSession().getAttribute(WebKeys.CMS_USER);
101         Address address = PublicAddressFactory.getAddressById(form.getAddressId());
102         LogFactory.getLog(AddressAction.class).error("address.getAddressId()" + address.getAddressId());
103         LogFactory.getLog(AddressAction.class).error("address.getUserId()" + address.getUserId());
104         
105         
106         if (! "0".equals(form.getAddressId()) && !user.getUserId().equals(address.getUserId()) && UtilMethods.isSet(address.getUserId())) {
107             ActionErrors aes = new ActionErrors();
108             LogFactory.getLog(AddressAction.class).error("Invalid Address " + form.getAddressId() + " requested by userId:" + user.getUserId());
109             aes.add(Globals.ERROR_KEY, new ActionMessage("error.invalidAddress"));
110             request.getSession().setAttribute(Globals.ERROR_KEY, aes);
111
112             address = null;
113             return mapping.findForward("myAccountAddressPage");
114         }
115
116         
117         
118         
119         address.setUserId(user.getUserId());
120         address.setStreet1(form.getAddress1());
121         address.setStreet2(form.getAddress2());
122         address.setCity(form.getCity());
123         address.setState(form.getState());
124         address.setCountry(form.getCountry());
125         address.setZip(form.getZip());
126         address.setPhone(form.getPhone());
127         
128         address.setDescription(form.getDescription());
129         address.setCell(form.getCell());
130         address.setCompanyId(user.getCompanyId());
131         address.setPriority(form.getPriority());
132         address.setUserName(user.getFullName());
133         address.setModifiedDate(new java.util.Date());
134         address.setClassName(User.class.getName());
135         address.setClassPK(user.getUserId());
136
137         if (address.getCreateDate() == null) {
138             address.setCreateDate(new java.util.Date());
139         }
140
141
142
143         PublicAddressFactory.save(address);
144
145         ActionMessages am = new ActionMessages();
146         am.add(Globals.ERROR_KEY, new ActionMessage("message.addressSaved"));
147         request.getSession().setAttribute(Globals.MESSAGE_KEY, am);
148
149         ActionForward af = new ActionForward("/cms/myAccount");
150         af.setRedirect(true);
151
152         return af;
153     }
154
155     public ActionForward deleteAddress(ActionMapping mapping, ActionForm lf, HttpServletRequest request,
156         HttpServletResponse response) throws Exception {
157         MyAccountForm form = (MyAccountForm) lf;
158
159         User user = (User) request.getSession().getAttribute(WebKeys.CMS_USER);
160
161         Address address = null;
162
163         try {
164             address = PublicAddressFactory.getAddressById(form.getAddressId());
165         } catch (Exception e) {
166             LogFactory.getLog(AddressAction.class).debug("Getting new Address");
167         }
168
169         List adds = PublicAddressFactory.getAddressesByUserId(user.getUserId());
170
171         if (!address.isNew() && !user.getUserId().equals(address.getUserId())) {
172             ActionErrors aes = new ActionErrors();
173             LogFactory.getLog(AddressAction.class).error("Invalid Address requested by userId:" + user.getUserId());
174             aes.add(Globals.ERROR_KEY, new ActionMessage("error.invalidAddress"));
175             request.getSession().setAttribute(Globals.ERROR_KEY, aes);
176
177             address = null;
178         } else if (adds.size() < 2) {
179             ActionErrors aes = new ActionErrors();
180             aes.add(Globals.ERROR_KEY, new ActionMessage("error.oneAddressRequired"));
181             request.getSession().setAttribute(Globals.ERROR_KEY, aes);
182
183             address = null;
184         } else {
185             PublicAddressFactory.delete(address);
186
187             ActionMessages ams = new ActionMessages();
188             ams.add(Globals.MESSAGE_KEY, new ActionMessage("message.addressDeleted"));
189             request.getSession().setAttribute(Globals.MESSAGE_KEY, ams);
190         }
191
192         ActionForward af = new ActionForward("/cms/myAccount");
193         af.setRedirect(true);
194
195         return af;
196     }*/

197 }
198
Popular Tags