I'm building a GWT application for GAE, using JDO for Datastore access. I use and Entity class for data mapping, so it contains lots of Persistence annotations:
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class AppointmentEntity implements Serializable {
private static final long serialVersionUID = 1L;
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;
@Persistent
private String firstName;
@Persistent
private String lastName;
@Persistent
private String DNI;
@Persistent
private Date appointmentDate;
@Persistent
private String attendant;
@Persistent
private String treatment;
@Persistent
private String details;
@Persistent
private Date nextAppointment;
@Persistent(serialized = "true")
private DownloadableFile file;
//getters and setters...
Because of GWT and JDO nature, i can't use this Entity classes in my presentation layer; so there's a need of passing Entity Object attributes to the corresponding DTO, which is a POJO that has the same attributes of the Entity class:
public class AppointmentDTO implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String firstName;
private String lastName;
private String DNI;
private Date appointmentDate;
private Date nextAppointmentDate;
private String attendant;
private String treatment;
private String details;
//getters and setters...
To do the Job of mapping attributes i make a Wrapper class that handles it, but it doesn't feel quite right to me. Does anyone have a better approach?
public class AppointmentWrapper {
private List<AppointmentEntity> entitiesList;
public AppointmentWrapper(List<AppointmentEntity> appointmentEntities) {
entitiesList = appointmentEntities;
}
public List<AppointmentDTO> getDTOList() {
List<AppointmentDTO> dtoList = new ArrayList<AppointmentDTO>();
for (AppointmentEntity entity : entitiesList) {
AppointmentDTO dto = new AppointmentDTO();
dto.setId(entity.getId());
dto.setFirstName(entity.getFirstName());
dto.setLastName(entity.getLastName());
dto.setAppointmentDate(entity.getAppointmentDate());
dto.setNextAppointmentDate(entity.getNextAppointment());
dto.setAttendant(entity.getAttendant());
dto.setDetails(entity.getDetails());
dto.setDNI(entity.getDNI());
dto.setTreatment(entity.getTreatment());
dtoList.add(dto);
}
return dtoList;
}
}
-
\$\begingroup\$ My feeling is that few members have GWT/JDO experience, but lot of them have design/programming experience. Linking to what Entity or DTO are would probably help everyone understand the issue in order to help you. \$\endgroup\$Quentin Pradet– Quentin Pradet2012年03月02日 12:48:17 +00:00Commented Mar 2, 2012 at 12:48
-
\$\begingroup\$ As request, i'm included full classes source code. It's not a GWT/JDO problem, is more a design issue \$\endgroup\$Carlos Gavidia-Calderon– Carlos Gavidia-Calderon2012年03月02日 15:10:39 +00:00Commented Mar 2, 2012 at 15:10
-
\$\begingroup\$ Solutions to design issues are frequently dependent on the technology being used. \$\endgroup\$Apprentice Queue– Apprentice Queue2012年03月02日 16:22:38 +00:00Commented Mar 2, 2012 at 16:22
1 Answer 1
There is this concerning GWT and GAE objects. If that doesn't work for you, I would rather have a class of public static methods that does the conversions rather than sticking it into some kind of "wrapper class" that contains object states. For example,
public final class JDOEntityUtil {
static public AppointmentDTO toDTO(AppointmentEntity entity) {
AppointmentDTO dto = new AppointmentDTO();
dto.setId(entity.getId());
dto.setFirstName(entity.getFirstName());
dto.setLastName(entity.getLastName());
dto.setAppointmentDate(entity.getAppointmentDate());
dto.setNextAppointmentDate(entity.getNextAppointment());
dto.setAttendant(entity.getAttendant());
dto.setDetails(entity.getDetails());
dto.setDNI(entity.getDNI());
dto.setTreatment(entity.getTreatment());
return dto;
}
static public AppointmentEntity toEntity(AppointmentDTO dto) {
AppointmentEntity entity = new AppointmentEntity();
entity.setId(dto.getId()); /* or however you want to construct this */
entity.setFirstName(dto.getFirstName());
entity.setLastName(dto.getLastName());
entity.setAppointmentDate(dto.getAppointmentDate());
entity.setNextAppointmentDate(dto.getNextAppointment());
entity.setAttendant(dto.getAttendant());
entity.setDetails(dto.getDetails());
entity.setDNI(dto.getDNI());
entity.setTreatment(dto.getTreatment());
return entity;
}
}