Date today = new Date();
if (user.isNew() || (!user.isNew() && user.getLastLoginDate() == null)) {
// ### CREATE USER ###
Company company = PublicCompanyFactory.getDefaultCompany();
user.setEmailAddress(form.getEmail().trim().toLowerCase());
user.setFirstName(form.getFirstName() == null ? "" : form.getFirstName());
user.setMiddleName(form.getMiddleName() == null ? "" : form.getMiddleName());
user.setLastName(form.getLastName() == null ? "" : form.getLastName());
user.setNickName("");
user.setCompanyId(company.getCompanyId());
user.setPasswordEncrypted(true);
user.setGreeting("Welcome, " + user.getFullName() + "!");
// Set defaults values
if (user.isNew()) {
//if it's a new user we set random password
String pass = PublicEncryptionFactory.getRandomPassword();
user.setPassword(PublicEncryptionFactory.digestString(pass));
user.setLanguageId(defaultUser.getLanguageId());
user.setTimeZoneId(defaultUser.getTimeZoneId());
user.setSkinId(defaultUser.getSkinId());
user.setDottedSkins(defaultUser.isDottedSkins());
user.setRoundedSkins(defaultUser.isRoundedSkins());
user.setResolution(defaultUser.getResolution());
user.setRefreshRate(defaultUser.getRefreshRate());
user.setLayoutIds("");
user.setActive(true);
user.setCreateDate(today);
}
APILocator.getUserAPI().save(user,APILocator.getUserAPI().getSystemUser(),false);
// ### END CREATE USER ###
// ### CREATE USER_PROXY ###
UserProxy userProxy = com.dotmarketing.business.APILocator.getUserProxyAPI().getUserProxy(user.getUserId(),APILocator.getUserAPI().getSystemUser(), false);
userProxy.setPrefix("");
userProxy.setTitle(form.getTitle());
userProxy.setOrganization(form.getOrganization());
userProxy.setUserId(user.getUserId());
com.dotmarketing.business.APILocator.getUserProxyAPI().saveUserProxy(userProxy,APILocator.getUserAPI().getSystemUser(), false);
// ### END CRETE USER_PROXY ###
// saving user inode on web form
form.setUserInode(userProxy.getInode());
if(UtilMethods.isSet(form.getFormType())){
HibernateUtil.saveOrUpdate(form);
}
///// WE CAN DO THIS! BUT WE NEED TO ADD CATEGORIES TO WEBFORM AND ALSO CHANGE THE PROCESSES THAT
//// CREATE THE EXCEL DOWNLOAD FROM WEB FORMS. I DIDN'T ADD IT SO I COMMENTED THIS CODE FOR NOW
//get the old categories, wipe them out
/*
List<Category> categories = InodeFactory.getParentsOfClass(userProxy, Category.class);
for (int i = 0; i < categories.size(); i++) {
categories.get(i).deleteChild(userProxy);
}
*/
// Save the new categories
/*String[] arr = form.getCategories();
if (arr != null) {
for (int i = 0; i < arr.length; i++) {
Category node = (Category) InodeFactory.getInode(arr[i], Category.class);
node.addChild(userProxy);
}
}*/
// ### CREATE ADDRESS ###
try {
List<Address> addresses = PublicAddressFactory.getAddressesByUserId(user.getUserId());
Address address = (addresses.size() > 0 ? addresses.get(0) : PublicAddressFactory.getInstance());
address.setStreet1(form.getAddress1() == null ? "" : form.getAddress1());
address.setStreet2(form.getAddress2() == null ? "" : form.getAddress2());
address.setCity(form.getCity() == null ? "" : form.getCity());
address.setState(form.getState() == null ? "" : form.getState());
address.setZip(form.getZip() == null ? "" : form.getZip());
String phone = form.getPhone();
address.setPhone(phone == null ? "" : phone);
address.setUserId(user.getUserId());
address.setCompanyId(company.getCompanyId());
PublicAddressFactory.save(address);
} catch (Exception ex) {
Logger.error(this,ex.getMessage(),ex);
}