UserEnterOrEditForm f = this;
if (user == null){
user = new User();
}
user.setId(f.getId());
GroupManager groupManager = new GroupManager(locale, session);
Group group = groupManager.getGroup(f.getGroupId());
user.setGroup(group);
String username = f.getUsername();
user.setUsername(username.length() == 0?null:username); // empty input means: no username
if (f.getPassword().replaceAll("\\*", " ").trim().length() != 0){ // do not change existing (encoded) password if user did not edit the asterisks
user.setPassword(f.getPassword().length() == 0 ? null : f.getPassword()); // will be encoded during creation/modification
}
user.setTitle(f.getTitle().trim().length() == 0 ? null : f.getTitle());
user.setFirstName(f.getFirstName().trim().length() == 0 ? null : f.getFirstName());
user.setLastName(f.getLastName().trim().length() == 0 ? null : f.getLastName());
user.setPersonalNumber(f.getPersonalNumber().trim().length() == 0 ? null : f.getPersonalNumber());
user.setAddress(f.getAddress().trim().length() == 0 ? null : f.getAddress());
user.setPostalCode(f.getPostalCode().trim().length() == 0 ? null : f.getPostalCode());
user.setCity(f.getCity().trim().length() == 0 ? null : f.getCity());
user.setState(f.getState().trim().length() == 0 ? null : f.getState());
user.setEmail(f.getEmail().trim().length() == 0 ? null : f.getEmail());
user.setFixedPhone(f.getFixedPhone().trim().length() == 0 ? null : f.getFixedPhone());
String msisdn = "";
if(f.getCountryCode() != null && f.getCountryCode().trim().length() >= 1){
msisdn = f.getCountryCode();
}
if(f.getMobileNumber() != null && f.getMobileNumber().trim().length() >= 1){
msisdn += Long.parseLong(f.getMobileNumber()); //the convertion is used to cut the leading zero
user.setMsisdn(Long.parseLong(msisdn));
} else {
user.setMsisdn(null); // ignore also if only country code
}
user.setMobilePhoneProducer(f.getMobilePhoneProducer() != null ? f.getMobilePhoneProducer() : UserManager.PRODUCER_NOT_NOKIA);
user.setExternallyModifiedAt(getDateTime(f.getExternallyModifiedAt(), f.getExternallyModifiedAtTime()));
String[] selectedRoleIds = f.getSelectedRoleIds();
Set<Role> roles = new HashSet<Role>();
if (selectedRoleIds != null){
for (String selectedRoleId : selectedRoleIds) {
UserManager userManager = new UserManager(locale,session);
Role role = userManager.getRole(Long.parseLong(selectedRoleId));
if (role == null){
throw new BugException("illegal role id");
}
roles.add(role);
}
}
user.setRoles(roles); // may even be empty
String birthDayParameter = f.getBirthDay();
String birthMonthParameter = f.getBirthMonth();
String birthYearParameter = f.getBirthYear();
if (birthDayParameter.length() >= 1 && birthMonthParameter.length() >= 1 && birthYearParameter.length() >= 1) {
int birthDay = Integer.parseInt(birthDayParameter);
int birthMonth = Integer.parseInt(birthMonthParameter);
int birthYear = Integer.parseInt(birthYearParameter);
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(Calendar.DAY_OF_MONTH, birthDay);
cal.set(Calendar.MONTH, birthMonth - 1);
cal.set(Calendar.YEAR, birthYear);
user.setBirthdate(cal.getTime());
} else {
user.setBirthdate(null);
}
String languageCode = f.getLanguage();
if (languageCode.length() >= 2){
user.setLocale(languageCode);
} else {
user.setLocale(null);
}
user.setUnlimitedRedemptions(f.getUnlimitedRedemptions() ? 1 : 0);
user.setGroup(new GroupManager(locale,session).getGroup(f.getGroupId()));
UserSetManager userSetMgr = new UserSetManager(locale, session);
Set<UserSet> userSets = new HashSet<UserSet>();
if (f.getUserSets() != null) {
for (Long id : f.getUserSets()) {