PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/src/classes/com/sapienter/jbilling/client/user/CreateAction.java

https://github.com/bibulous/SkyrackJbill2.2
Java | 265 lines | 181 code | 32 blank | 52 comment | 40 complexity | e07935c31d0cce84378e92e099aa8c27 MD5 | raw file
  1. /*
  2. jBilling - The Enterprise Open Source Billing System
  3. Copyright (C) 2003-2009 Enterprise jBilling Software Ltd. and Emiliano Conde
  4. This file is part of jbilling.
  5. jbilling is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU Affero General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. jbilling is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Affero General Public License for more details.
  13. You should have received a copy of the GNU Affero General Public License
  14. along with jbilling. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. package com.sapienter.jbilling.client.user;
  17. import java.io.IOException;
  18. import javax.servlet.ServletException;
  19. import javax.servlet.http.HttpServletRequest;
  20. import javax.servlet.http.HttpServletResponse;
  21. import javax.servlet.http.HttpSession;
  22. import org.apache.log4j.Logger;
  23. import org.apache.struts.action.Action;
  24. import org.apache.struts.action.ActionError;
  25. import org.apache.struts.action.ActionErrors;
  26. import org.apache.struts.action.ActionForm;
  27. import org.apache.struts.action.ActionForward;
  28. import org.apache.struts.action.ActionMapping;
  29. import org.apache.struts.action.ActionMessage;
  30. import org.apache.struts.action.ActionMessages;
  31. import org.apache.struts.action.DynaActionForm;
  32. import com.sapienter.jbilling.client.util.Constants;
  33. import com.sapienter.jbilling.server.user.ContactDTOEx;
  34. import com.sapienter.jbilling.server.user.UserDTOEx;
  35. import com.sapienter.jbilling.server.user.IUserSessionBean;
  36. import com.sapienter.jbilling.server.user.db.CompanyDTO;
  37. import com.sapienter.jbilling.server.user.db.CustomerDTO;
  38. import com.sapienter.jbilling.server.user.db.UserDTO;
  39. import com.sapienter.jbilling.server.user.partner.db.Partner;
  40. import com.sapienter.jbilling.server.user.permisson.db.RoleDTO;
  41. import com.sapienter.jbilling.server.util.Context;
  42. import com.sapienter.jbilling.server.util.db.CurrencyDTO;
  43. import com.sapienter.jbilling.server.util.db.LanguageDTO;
  44. public class CreateAction extends Action {
  45. private static final Logger LOG = Logger.getLogger(CreateAction.class);
  46. public ActionForward execute(ActionMapping mapping, ActionForm form,
  47. HttpServletRequest request, HttpServletResponse response)
  48. throws IOException, ServletException {
  49. ActionErrors errors = new ActionErrors();
  50. ActionMessages messages = new ActionMessages();
  51. String retValue = "done";
  52. Integer newUserId = null;
  53. // some attributes of the new user will be inherited from the root user
  54. // that is creating this account
  55. HttpSession session = request.getSession();
  56. Integer entityId = (Integer) session.getAttribute(
  57. Constants.SESSION_ENTITY_ID_KEY);
  58. Integer languageId = (Integer) session.getAttribute(
  59. Constants.SESSION_LANGUAGE);
  60. // get the dynamic form
  61. DynaActionForm userForm = (DynaActionForm) form;
  62. LOG.debug("Create user action");
  63. // verify that the password and the verification password are the same
  64. if (!((String) userForm.get("password")).equals((String)
  65. userForm.get("verifyPassword"))) {
  66. errors.add(ActionErrors.GLOBAL_ERROR,
  67. new ActionError("user.create.error.password_match"));
  68. } else {
  69. try {
  70. IUserSessionBean myRemoteSession = (IUserSessionBean)
  71. Context.getBean(Context.Name.USER_SESSION);
  72. // create the dto from the dynamic form
  73. Integer typeId = (Integer) userForm.get("type");
  74. if (typeId == null) {
  75. // some users won't have the permission to pick the
  76. // type, so it is assumend they are creating customers
  77. typeId = Constants.TYPE_CUSTOMER;
  78. }
  79. UserDTOEx dto = new UserDTOEx();
  80. dto.setCompany(new CompanyDTO(entityId));
  81. dto.setUserName((String)userForm.get("username"));
  82. dto.setPassword((String)userForm.get("password"));
  83. dto.setLanguage(new LanguageDTO(languageId));
  84. dto.setMainRoleId(typeId);
  85. dto.setCurrency(new CurrencyDTO((Integer) userForm.get("currencyId")));
  86. // add the roles
  87. // now, it will be just one and directly mapped to the user type
  88. // this doesn't have to be like this, it is now because it is
  89. // enough for the BAT requirements
  90. dto.getRoles().add(new RoleDTO(typeId));
  91. // finally, create the contact dto to send the email
  92. ContactDTOEx contact = new ContactDTOEx();
  93. contact.setEmail((String) userForm.get("email"));
  94. // and default the country to the entity's
  95. contact.setCountryCode(myRemoteSession.getEntityContact(
  96. entityId).getCountryCode());
  97. // by default the contact is included
  98. contact.setInclude(new Integer(1));
  99. // if it is a customer, it'll need the customer dto
  100. if (typeId.equals(Constants.TYPE_CUSTOMER)) {
  101. CustomerDTO customerDto = new CustomerDTO();
  102. // set the partner
  103. Integer partnerId = null;
  104. try {
  105. partnerId = Integer.valueOf((String) userForm.get(
  106. "partnerId"));
  107. } catch (Exception e) {
  108. try {
  109. partnerId = Integer.valueOf((String)
  110. session.getAttribute("jsp_partnerId"));
  111. } catch (Exception e1) {
  112. }
  113. }
  114. if (partnerId != null) {
  115. customerDto.setPartner(new Partner(partnerId));
  116. }
  117. customerDto.setReferralFeePaid(new Integer(0));
  118. dto.setCustomer(customerDto);
  119. // set the sub-account fields
  120. String parentStr = (String) userForm.get("parentId");
  121. Integer parentId = null;
  122. if (parentStr != null && parentStr.length() > 0) {
  123. parentId = Integer.valueOf(parentStr);
  124. }
  125. if (parentId != null) {
  126. customerDto.setParent(new CustomerDTO(parentId));
  127. }
  128. customerDto.setIsParent(((Boolean) userForm.get(
  129. "chbx_is_parent")).booleanValue()
  130. ? new Integer(1) : new Integer(0));
  131. // verify that the parent is valid
  132. if (customerDto.getParent() != null) {
  133. UserDTOEx parent = myRemoteSession.getUserDTOEx(
  134. customerDto.getParent().getId());
  135. if (parent == null || !parent.getEntityId().equals(entityId) ||
  136. parent.getDeleted() == 1) {
  137. errors.add(ActionErrors.GLOBAL_ERROR,
  138. new ActionError(
  139. "user.create.error.noParent"));
  140. } else {
  141. if (parent.getCustomer() == null ||
  142. parent.getCustomer().getIsParent() == null ||
  143. parent.getCustomer().getIsParent().intValue() == 0) {
  144. errors.add(ActionErrors.GLOBAL_ERROR,
  145. new ActionError(
  146. "user.create.error.badParent"));
  147. }
  148. }
  149. }
  150. customerDto.setInvoiceChild(((Boolean) userForm.get(
  151. "chbx_invoiceChild")).booleanValue()
  152. ? new Integer(1) : new Integer(0));
  153. if (customerDto.getInvoiceChild().intValue() == 1 &&
  154. customerDto.getParent() == null) {
  155. // it has to be a child with a parent for this flag to
  156. // make any sense
  157. errors.add(ActionErrors.GLOBAL_ERROR,
  158. new ActionError(
  159. "user.create.error.invoiceChild"));
  160. }
  161. }
  162. if (typeId.equals(Constants.TYPE_PARTNER) && errors.isEmpty()) {
  163. // partners require more data
  164. // verify that the user is available
  165. UserDTO user = myRemoteSession.getUserDTO(
  166. dto.getUserName(), entityId);
  167. if (user != null) {
  168. errors.add(ActionErrors.GLOBAL_ERROR,
  169. new ActionError("user.create.error.taken",
  170. (String) userForm.get("username")));
  171. } else {
  172. // leave the data available to the next step
  173. session.setAttribute(Constants.SESSION_CUSTOMER_DTO, dto);
  174. session.setAttribute(
  175. Constants.SESSION_CUSTOMER_CONTACT_DTO, contact);
  176. // but to allow the defaults to be loaded, make sure no
  177. // id is in the session
  178. session.removeAttribute(Constants.SESSION_PARTNER_ID);
  179. retValue = "partner";
  180. }
  181. } else if (errors.isEmpty()) {
  182. newUserId = myRemoteSession.create(dto, contact);
  183. if (newUserId == null) {
  184. errors.add(ActionErrors.GLOBAL_ERROR,
  185. new ActionError("user.create.error.taken",
  186. (String) userForm.get("username")));
  187. } else if (newUserId.intValue() == -1) {
  188. errors.add(ActionErrors.GLOBAL_ERROR,
  189. new ActionError("user.create.error.badPartner"));
  190. }
  191. }
  192. } catch (Exception e) {
  193. errors.add(ActionErrors.GLOBAL_ERROR,
  194. new ActionError("all.internal"));
  195. LOG.error("Exception", e);
  196. }
  197. }
  198. if (errors.isEmpty()) {
  199. // the creation was successfull, let's now see if that's all or
  200. // the contact information will be entered
  201. messages.add(ActionMessages.GLOBAL_MESSAGE,
  202. new ActionMessage("user.create.done"));
  203. // later, when a customer is self creating, the forward might
  204. // have to be to the contact form.
  205. // leave the user id in the session so the page knows that
  206. // is time to show up the contact/cc options
  207. session.setAttribute(Constants.SESSION_USER_ID,
  208. newUserId);
  209. LOG.debug("new user id = " +
  210. newUserId);
  211. session.setAttribute(Constants.SESSION_CONTACT_USER_ID,
  212. newUserId);
  213. saveMessages(request, messages);
  214. // remove any list with users
  215. session.removeAttribute(Constants.SESSION_LIST_KEY +
  216. Constants.LIST_TYPE_CUSTOMER);
  217. session.removeAttribute(Constants.SESSION_LIST_KEY +
  218. Constants.LIST_TYPE_CUSTOMER_SIMPLE);
  219. session.removeAttribute(Constants.SESSION_LIST_KEY +
  220. Constants.LIST_TYPE_SUB_ACCOUNTS);
  221. } else {
  222. // something failed
  223. saveErrors(request, errors);
  224. LOG.debug("errors: " + errors.size());
  225. retValue = "error";
  226. }
  227. return mapping.findForward(retValue);
  228. }
  229. }