@Override public String add(RoledUser user) throws ServiceException { // Assert if (user == null) throw new ServiceException("Wrong parameter"); if (!(user instanceof GreenUser)) throw new ServiceException("Wrong parameter"); if (user.getUserName() == null || user.getUserName().trim().equals("")) throw new ServiceException("Username not defined"); if (user.getPassword() == null || user.getPassword().trim().equals("")) throw new ServiceException("Password not defined"); // Save GreenUser u = new GreenUser(); u.setUsername(user.getUserName()); MD5 md5 = new MD5(); try { md5.Update(user.getPassword(), null); } catch (UnsupportedEncodingException e) { throw new ServiceException(e); } String hash = md5.asHex(); String encPass = hash; u.setPassword(encPass); u.setEnabled(0); u.setName(((GreenUser) user).getName()); u.setLastname(((GreenUser) user).getLastname()); String newIndex = CodeGenerator.createCode(); u.setId(newIndex); Session session = getCurrentSession(); String userId = (String) session.save(u); return userId; }
public void update(String id, RoledUser user) throws ServiceException { GreenUser greenUser = null; // Assert if (id == null) throw new ServiceException("User id not defined"); if (user == null) return; if (!(user instanceof GreenUser)) { throw new ServiceException("Illegal parameter call"); } else { greenUser = (GreenUser) user; } Session session = getCurrentSession(); GreenUser u = (GreenUser) session.get(GreenUser.class, id); if (u == null) throw new ServiceException("User whith id: " + id + " not exist"); if (greenUser.getUsername() != null && !greenUser.getUsername().trim().equals("")) { u.setUsername(greenUser.getUsername()); } if (greenUser.getPassword() != null && !greenUser.getPassword().trim().equals("")) { MD5 md5 = new MD5(); try { md5.Update(greenUser.getPassword(), null); } catch (UnsupportedEncodingException e) { throw new ServiceException(e); } String hash = md5.asHex(); String encPass = hash; u.setPassword(encPass); } if (greenUser.getName() != null && !greenUser.getName().trim().equals("")) { u.setName(greenUser.getName()); } if (greenUser.getLastname() != null && !greenUser.getLastname().trim().equals("")) { u.setLastname(greenUser.getLastname()); } // Update session.update(u); }