Created
November 1, 2011 15:54
-
-
Save theresajayne/1330908 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package uk.co.inbrand.service.impl; | |
import java.util.HashSet; | |
import java.util.Iterator; | |
import java.util.Set; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import org.dom4j.DocumentException; | |
import org.springframework.transaction.annotation.Transactional; | |
import uk.co.inbrand.dao.AddressDao; | |
import uk.co.inbrand.dao.LicencesDao; | |
import uk.co.inbrand.dao.OrganisationDao; | |
import uk.co.inbrand.dao.UserRolesDao; | |
import uk.co.inbrand.dao.UsersDao; | |
import uk.co.inbrand.dto.AddressBase; | |
import uk.co.inbrand.dto.LicencesBase; | |
import uk.co.inbrand.dto.OrganisationBase; | |
import uk.co.inbrand.dto.UserRolesBase; | |
import uk.co.inbrand.dto.UsersSummary; | |
import uk.co.inbrand.dto.UsersBase; | |
import uk.co.inbrand.model.Address; | |
import uk.co.inbrand.model.Licences; | |
import uk.co.inbrand.model.Organisation; | |
import uk.co.inbrand.model.UserRoles; | |
import uk.co.inbrand.model.Users; | |
import uk.co.inbrand.service.AbstractAssembler; | |
/** | |
* Assembler class for License | |
* | |
*/ | |
@Transactional | |
public class UsersAssembler extends AbstractAssembler { | |
private UsersDao usersDAO; | |
private LicencesDao licencesDAO; | |
private LicencesAssembler licencesAssembler; | |
private OrganisationDao organisationDAO; | |
private OrganisationAssembler organisationAssembler; | |
private AddressDao addressDAO; | |
private AddressAssembler addressAssembler; | |
private UserRolesAssembler userRolesAssembler; | |
private UserRolesDao userRolesDao; | |
public UsersAssembler( | |
UsersDao usersDAO, | |
LicencesDao licencesDAO, | |
LicencesAssembler licencesAssembler, | |
OrganisationDao organisationDAO, | |
OrganisationAssembler organisationAssembler, | |
AddressDao addressDAO, | |
AddressAssembler addressAssembler, | |
UserRolesAssembler userRolesAssembler, | |
UserRolesDao userRolesDao | |
) { | |
super(); | |
this.usersDAO = usersDAO; | |
this.licencesDAO = licencesDAO; | |
this.licencesAssembler = licencesAssembler; | |
this.organisationDAO = organisationDAO; | |
this.organisationAssembler = organisationAssembler; | |
this.addressDAO = addressDAO; | |
this.addressAssembler = addressAssembler; | |
this.userRolesAssembler = userRolesAssembler; | |
this.userRolesDao = userRolesDao; | |
} | |
protected UsersDao getUsersDAO() { | |
return usersDAO; | |
} | |
protected LicencesDao getLicencesDAO() { | |
return licencesDAO; | |
} | |
protected AddressDao getAddressDAO() { | |
return addressDAO; | |
} | |
protected AddressAssembler getAddressAssembler() { | |
return addressAssembler; | |
} | |
protected LicencesAssembler getLicencesAssembler() { | |
return licencesAssembler; | |
} | |
protected OrganisationDao getOrganisationDAO() { | |
return organisationDAO; | |
} | |
protected OrganisationAssembler getOrganisationAssembler() { | |
return organisationAssembler; | |
} | |
public void extractSummaryFromDomain(Users domain, UsersSummary dto) { | |
dto.setUser_id(-1 != domain.getUser_id() ? domain.getUser_id() : -1); | |
dto.setUser_firstname(domain.getUser_firstname()); | |
dto.setUser_lastname(domain.getUser_lastname()); | |
dto.setUser_lastlogged(domain.getUser_lastlogged()); | |
Set<UserRolesBase> role_dto = dto.getUser_roles(); | |
if(role_dto == null ) role_dto = new HashSet<UserRolesBase>(); | |
Set<UserRoles> role_domain = domain.getUser_roles(); | |
if(role_domain == null) role_domain = new HashSet<UserRoles>(); | |
Iterator<UserRoles> it = role_domain.iterator(); | |
while(it.hasNext()) { | |
UserRoles r_domain = it.next(); | |
UserRolesBase r_dto = new UserRolesBase(); | |
userRolesAssembler.extractBaseFromDomain(r_domain, r_dto); | |
role_dto.add(r_dto); | |
} | |
dto.setUser_roles(role_dto); | |
dto.setUser_loggedin(domain.getUser_loggedin()); | |
dto.setUser_name(domain.getUser_name()); | |
dto.setUser_password(domain.getUser_password()); | |
} | |
/** | |
* Will call the relevant extractMethod | |
* @param domain The domain object to extract from | |
* @param dto The DTO to extract into | |
* @throws Exception_Exception | |
* @throws DocumentException | |
*/ | |
public void extractFromDomain(Users domain, UsersBase dto) | |
throws DocumentException { | |
// | |
// Eugh. As bad as a switch | |
// | |
if (dto instanceof UsersBase) { | |
extractBaseFromDomain(domain, (UsersBase) dto); | |
} else { | |
extractSummaryFromDomain(domain, dto); | |
} | |
} | |
public void extractBaseFromDomain(Users domain, UsersBase dto) { | |
// | |
// Init basic fields | |
// | |
extractSummaryFromDomain(domain, dto); | |
// | |
// Ensure these are actual Dates as Struts 2 has problems comparing different classes in validation | |
// | |
if(domain.getUser_licence()!=null) { | |
//copy the licence across | |
LicencesBase licence_dto = new LicencesBase(); | |
try { | |
getLicencesAssembler().extractBaseFromDomain(domain.getUser_licence(), licence_dto); | |
} catch (DocumentException ex) { | |
Logger.getLogger(UsersAssembler.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
dto.setUser_licence(licence_dto); | |
} | |
if(domain.getUser_organisation()!= null) { | |
OrganisationBase organisation_dto = new OrganisationBase(); | |
getOrganisationAssembler().extractBaseFromDomain(domain.getUser_organisation(), organisation_dto); | |
dto.setUser_organisation(organisation_dto); | |
} | |
if(domain.getUser_address() != null) { | |
AddressBase address_dto = new AddressBase(); | |
getAddressAssembler().extractBaseFromDomain(domain.getUser_address(), address_dto); | |
dto.setUser_address(address_dto); | |
} | |
} | |
public void populateDomainFromSummary(Users domain, UsersSummary dto) { | |
domain.setUser_id(-1 != dto.getUser_id() ? dto.getUser_id() : -1 ); | |
} | |
public void populateDomainFromBase(Users domain, UsersBase dto) { | |
populateDomainFromSummary(domain, dto); | |
domain.setUser_lastlogged(dto.getUser_lastlogged()); | |
domain.setUser_name(dto.getUser_name()); | |
domain.setUser_password(dto.getUser_password()); | |
domain.setUser_firstname(dto.getUser_firstname()); | |
domain.setUser_lastname(dto.getUser_lastname()); | |
Set<UserRoles> role_domain = domain.getUser_roles(); | |
if(role_domain == null ) role_domain = new HashSet<UserRoles>(); | |
Set<UserRolesBase> role_dto = dto.getUser_roles(); | |
if(role_dto == null ) role_dto = new HashSet<UserRolesBase>(); | |
Iterator<UserRolesBase> it = role_dto.iterator(); | |
while (it.hasNext()) { | |
UserRoles r_domain = new UserRoles(); | |
UserRolesBase r_dto = it.next(); | |
userRolesAssembler.populateDomainFromBase(r_domain, r_dto); | |
role_domain.add(r_domain); | |
} | |
domain.setUser_roles(role_domain); | |
domain.setUser_lastlogged(dto.getUser_lastlogged()); | |
domain.setUser_loggedin(dto.getUser_loggedin()); | |
if(dto.getUser_address()!= null) { | |
Address address_domain = new Address(); | |
address_domain = addressDAO.getAddressById(dto.getUser_address().getAddr_id()); | |
if(address_domain == null) { | |
address_domain = new Address(); | |
} | |
addressAssembler.populateDomainFromBase(address_domain, dto.getUser_address()); | |
domain.setUser_address(address_domain); | |
} | |
if(dto.getUser_licence() != null) { | |
Licences licences_domain = new Licences(); | |
licencesAssembler.populateDomainFromBase(licences_domain, dto.getUser_licence()); | |
domain.setUser_licence(licences_domain); | |
} | |
if(dto.getUser_organisation() != null) { | |
Organisation organisation_domain = new Organisation(); | |
organisation_domain = organisationDAO.getRecordById(dto.getUser_organisation().getOrg_id()); | |
organisationAssembler.populateDomainFromBase(organisation_domain, dto.getUser_organisation()); | |
domain.setUser_organisation(organisation_domain); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment