From: rkv Date: Thu, 1 Nov 2012 07:15:20 +0000 (+0000) Subject: DTO is introduced for knowledge elements. BeanHelper is modified so that setters... X-Git-Tag: Root_Delivery1_2012_12_06~98 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=99f082643ad18cdac91d4493d0d29c9723692546;p=tools%2Fsiman.git DTO is introduced for knowledge elements. BeanHelper is modified so that setters and getters are not checked for same property types to be able to copy DTO to BO and vice versa. --- diff --git a/Workspace/Siman-Common/ testng_util.xml b/Workspace/Siman-Common/ testng_util.xml new file mode 100644 index 0000000..9c58f73 --- /dev/null +++ b/Workspace/Siman-Common/ testng_util.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + diff --git a/Workspace/Siman-Common/src/log4j.xml b/Workspace/Siman-Common/src/log4j.xml index c73cdd5..f31a8af 100644 --- a/Workspace/Siman-Common/src/log4j.xml +++ b/Workspace/Siman-Common/src/log4j.xml @@ -50,6 +50,14 @@ + + + + + + + + diff --git a/Workspace/Siman-Common/src/org/splat/dal/bo/kernel/User.java b/Workspace/Siman-Common/src/org/splat/dal/bo/kernel/User.java index 41443f0..41dad3b 100644 --- a/Workspace/Siman-Common/src/org/splat/dal/bo/kernel/User.java +++ b/Workspace/Siman-Common/src/org/splat/dal/bo/kernel/User.java @@ -234,4 +234,11 @@ public class User extends Persistent implements Principal, Name { // ------------------------- return last + " " + first; } + /** + * Get the role. + * @return the role + */ + public Role getRole() { + return role; + } } \ No newline at end of file diff --git a/Workspace/Siman-Common/src/org/splat/dal/bo/som/KnowledgeElement.java b/Workspace/Siman-Common/src/org/splat/dal/bo/som/KnowledgeElement.java index 0bfa2e5..6f4a9a8 100644 --- a/Workspace/Siman-Common/src/org/splat/dal/bo/som/KnowledgeElement.java +++ b/Workspace/Siman-Common/src/org/splat/dal/bo/som/KnowledgeElement.java @@ -11,11 +11,8 @@ import java.util.Date; import java.util.List; import java.util.Vector; -import org.hibernate.Session; - import org.splat.dal.bo.kernel.Persistent; import org.splat.dal.bo.kernel.User; -import org.splat.dal.dao.som.Database; import org.splat.kernel.InvalidPropertyException; import org.splat.kernel.MissedPropertyException; import org.splat.kernel.MultiplyDefinedException; diff --git a/Workspace/Siman-Common/src/org/splat/service/KnowledgeElementService.java b/Workspace/Siman-Common/src/org/splat/service/KnowledgeElementService.java index 2ccef0c..a2f0f02 100644 --- a/Workspace/Siman-Common/src/org/splat/service/KnowledgeElementService.java +++ b/Workspace/Siman-Common/src/org/splat/service/KnowledgeElementService.java @@ -15,6 +15,7 @@ import org.splat.dal.bo.som.KnowledgeElement; import org.splat.dal.bo.som.KnowledgeElementType; import org.splat.dal.bo.som.ProgressState; import org.splat.kernel.InvalidPropertyException; +import org.splat.service.dto.KnowledgeElementDTO; /** * Knowledge element service interface. @@ -72,6 +73,15 @@ public interface KnowledgeElementService { */ public KnowledgeElement selectKnowledgeElement(long index); + /** + * Get a knowledge element DTO by id. + * + * @param index + * the knowledge element id + * @return the found knowledge element as DTO + */ + public KnowledgeElementDTO getKnowledgeElement(long index); + /** * Update the description of the knowledge element. * diff --git a/Workspace/Siman-Common/src/org/splat/service/KnowledgeElementServiceImpl.java b/Workspace/Siman-Common/src/org/splat/service/KnowledgeElementServiceImpl.java index 76f8667..c01cd68 100644 --- a/Workspace/Siman-Common/src/org/splat/service/KnowledgeElementServiceImpl.java +++ b/Workspace/Siman-Common/src/org/splat/service/KnowledgeElementServiceImpl.java @@ -9,12 +9,19 @@ package org.splat.service; +import java.util.Collection; +import java.util.Vector; + import org.splat.dal.bo.som.KnowledgeElement; import org.splat.dal.bo.som.ProgressState; +import org.splat.dal.bo.som.Scenario; import org.splat.dal.dao.som.KnowledgeElementDAO; import org.splat.kernel.InvalidPropertyException; import org.splat.log.AppLogger; +import org.splat.service.dto.KnowledgeElementDTO; import org.splat.service.technical.IndexService; +import org.splat.som.Step; +import org.splat.util.BeanHelper; import org.springframework.transaction.annotation.Transactional; /** @@ -42,6 +49,10 @@ public class KnowledgeElementServiceImpl implements KnowledgeElementService { * Injected study service. */ private StudyService _studyService; + /** + * Injected project element service. + */ + private ProjectElementService _projectElementService; /** * {@inheritDoc} @@ -72,7 +83,9 @@ public class KnowledgeElementServiceImpl implements KnowledgeElementService { /** * Update knowledge element in the database and in the lucene index. - * @param knowledgeElement the knowledge element to update + * + * @param knowledgeElement + * the knowledge element to update * @return true if updating succeeded */ protected boolean update(KnowledgeElement knowledgeElement) { @@ -120,10 +133,56 @@ public class KnowledgeElementServiceImpl implements KnowledgeElementService { * * @see org.splat.service.KnowledgeElementService#selectKnowledgeElement(long) */ - @Transactional(readOnly=true) + @Transactional(readOnly = true) public KnowledgeElement selectKnowledgeElement(long index) { KnowledgeElement result = getKnowledgeElementDAO().get(index); - getStudyService().loadWorkflow(result.getOwnerScenario().getOwnerStudy()); + getStudyService().loadWorkflow( + result.getOwnerScenario().getOwnerStudy()); + return result; + } + + /** + * {@inheritDoc} + * + * @see org.splat.service.KnowledgeElementService#getKnowledgeElement(long) + */ + @Transactional(readOnly = true) + public KnowledgeElementDTO getKnowledgeElement(long index) { + KnowledgeElement kelm = getKnowledgeElementDAO().get(index); + getStudyService().loadWorkflow(kelm.getOwnerScenario().getOwnerStudy()); + KnowledgeElementDTO result = BeanHelper.copyBean(kelm, + KnowledgeElementDTO.class); + result.setScenarioTitle(kelm.getOwnerScenario().getTitle()); + result.setStudyTitle(kelm.getOwnerScenario().getOwnerStudy().getTitle()); + result.getInvolving().addAll(getAllSteps(kelm.getOwnerScenario())); + return result; + } + + /** + * Get all steps of the scenario. + * @param scenar the scenario + * @return collection of steps + */ + private Collection getAllSteps(Scenario scenar) { + Vector result = new Vector(); + Step[] step = getProjectElementService().getSteps(scenar); + + int base = step[0].getNumber(); + int last = step[step.length - 1].getNumber(); + for (int i = 0; i < step.length; i++) { + result.add(step[i]); + } + step = getProjectElementService().getSteps(scenar.getOwnerStudy()); + for (int i = step.length - 1; i > -1; i--) { + if (step[i].getNumber() >= base) + continue; + result.add(0, step[i]); + } + for (int i = 0; i < step.length; i++) { + if (step[i].getNumber() <= last) + continue; + result.add(step[i]); + } return result; } @@ -193,6 +252,7 @@ public class KnowledgeElementServiceImpl implements KnowledgeElementService { /** * Get the studyService. + * * @return the studyService */ public StudyService getStudyService() { @@ -201,9 +261,27 @@ public class KnowledgeElementServiceImpl implements KnowledgeElementService { /** * Set the studyService. - * @param studyService the studyService to set + * + * @param studyService + * the studyService to set */ public void setStudyService(StudyService studyService) { _studyService = studyService; } + + /** + * Get the projectElementService. + * @return the projectElementService + */ + public ProjectElementService getProjectElementService() { + return _projectElementService; + } + + /** + * Set the projectElementService. + * @param projectElementService the projectElementService to set + */ + public void setProjectElementService(ProjectElementService projectElementService) { + _projectElementService = projectElementService; + } } diff --git a/Workspace/Siman-Common/src/org/splat/service/ProjectElementServiceImpl.java b/Workspace/Siman-Common/src/org/splat/service/ProjectElementServiceImpl.java index 7e75376..b8a3535 100644 --- a/Workspace/Siman-Common/src/org/splat/service/ProjectElementServiceImpl.java +++ b/Workspace/Siman-Common/src/org/splat/service/ProjectElementServiceImpl.java @@ -14,7 +14,6 @@ import java.util.List; import org.splat.dal.bo.som.ProjectElement; import org.splat.dal.bo.som.Publication; -import org.splat.dal.dao.som.Database; import org.splat.dal.dao.som.ProjectElementDAO; import org.splat.service.technical.ProjectSettingsService; import org.splat.som.Step; diff --git a/Workspace/Siman-Common/src/org/splat/service/dto/KnowledgeElementDTO.java b/Workspace/Siman-Common/src/org/splat/service/dto/KnowledgeElementDTO.java new file mode 100644 index 0000000..e354bd9 --- /dev/null +++ b/Workspace/Siman-Common/src/org/splat/service/dto/KnowledgeElementDTO.java @@ -0,0 +1,171 @@ +package org.splat.service.dto; +/** + * + * @author Daniel Brunier-Coulin + * @copyright OPEN CASCADE 2012 + */ + +import java.text.DecimalFormat; +import java.util.Date; +import java.util.List; +import java.util.Vector; + +import org.splat.dal.bo.som.ProgressState; +import org.splat.som.Step; + + +public class KnowledgeElementDTO { + + private long rid; + private KnowledgeElementTypeDTO type; // User extendable types + private ProgressState state; + private String title; + private String value; + private UserDTO author; + private Date date; + private List involving = new Vector(); + private String studyTitle; + private String scenarioTitle; + +// ============================================================================================================================== +// Public member functions +// ============================================================================================================================== + + /** + * @param given + * @return + */ + public boolean equals (KnowledgeElementDTO given) { + if (!this.getType().equals(given.getType())) return false; + if (this.getValue().equals(given.getValue())) return true; + return false; + } + + public UserDTO getAuthor () { + return author; + } + + public Date getDate () { + return date; + } + + public ProgressState getProgressState () { + return state; + } + + public String getTitle () { + return title; + } + + public String getReference () { + DecimalFormat toString = new DecimalFormat("00000"); // Supports 99 999 knowledge elements + return "KE" + toString.format(this.getIndex()); + } + + public KnowledgeElementTypeDTO getType () { + return type; + } + + public String getValue () { + return value; + } + + /** + * Set a status of + * @param aState knowledge element progress state to set + */ + public void setProgressState(ProgressState aState) { + state = aState; + } + /** + * Set a title of the knowledge. + * @param aTitle a title to set + */ + public void setTitle(String aTitle) { + title = aTitle; + } + /** + * Set the value. + * @param value the value to set + */ + public void setValue(String value) { + this.value = value; + } + /** + * Get list of involving steps. + * @return list of steps + */ + public List getInvolving() { + return involving; + } + /** + * Get title of the parent study. + * @return study title + */ + public String getStudyTitle() { + return studyTitle; + } + /** + * Get title of the parent scenario. + * @return scenario title + */ + public String getScenarioTitle() { + return scenarioTitle; + } + + /** + * Get the rid. + * @return the rid + */ + public long getIndex() { + return rid; + } + + /** + * Set the rid. + * @param rid the rid to set + */ + public void setIndex(long rid) { + this.rid = rid; + } + + /** + * Set the studyTitle. + * @param studyTitle the studyTitle to set + */ + public void setStudyTitle(String studyTitle) { + this.studyTitle = studyTitle; + } + + /** + * Set the scenarioTitle. + * @param scenarioTitle the scenarioTitle to set + */ + public void setScenarioTitle(String scenarioTitle) { + this.scenarioTitle = scenarioTitle; + } + + /** + * Set the type. + * @param type the type to set + */ + public void setType(KnowledgeElementTypeDTO type) { + this.type = type; + } + + /** + * Set the author. + * @param author the author to set + */ + public void setAuthor(UserDTO author) { + this.author = author; + } + + /** + * Set the date. + * @param date the date to set + */ + public void setDate(Date date) { + this.date = date; + } +} \ No newline at end of file diff --git a/Workspace/Siman-Common/src/org/splat/service/dto/KnowledgeElementTypeDTO.java b/Workspace/Siman-Common/src/org/splat/service/dto/KnowledgeElementTypeDTO.java new file mode 100644 index 0000000..48cec3b --- /dev/null +++ b/Workspace/Siman-Common/src/org/splat/service/dto/KnowledgeElementTypeDTO.java @@ -0,0 +1,82 @@ +package org.splat.service.dto; + +/** + * Knowledge type DTO. + * + * @author Roman Kozlov (RKV) + */ +public class KnowledgeElementTypeDTO { + + /** + * Type name. + */ + private String name; + /** + * Persistent id of the type. + */ + private long rid; + + // ============================================================================================================================== + // Public member functions + // ============================================================================================================================== + + /** + * {@inheritDoc} + * + * @see java.lang.Object#equals(java.lang.Object) + */ + public boolean equals(Object entity) { + if (entity == null) + return false; + if (entity instanceof String) { + return this.name.equals((String) entity); // Names are unique + } else if (entity instanceof KnowledgeElementTypeDTO) { + KnowledgeElementTypeDTO object = (KnowledgeElementTypeDTO) entity; + long he = object.getIndex(); + long me = this.getIndex(); + if (me * he != 0) { + return (he == me); + } else { + return this.getName().equals(object.getName()); + } + } else { + return false; + } + } + + /** + * Get the name of the knowledge type. + * + * @return the type name + */ + public String getName() { + return name; + } + + /** + * Get the rid. + * + * @return the rid + */ + public long getIndex() { + return rid; + } + + /** + * Set the rid. + * + * @param rid + * the rid to set + */ + public void setIndex(long rid) { + this.rid = rid; + } + + /** + * Set the name. + * @param name the name to set + */ + public void setName(String name) { + this.name = name; + } +} \ No newline at end of file diff --git a/Workspace/Siman-Common/src/org/splat/service/dto/RoleDTO.java b/Workspace/Siman-Common/src/org/splat/service/dto/RoleDTO.java new file mode 100644 index 0000000..5f9966c --- /dev/null +++ b/Workspace/Siman-Common/src/org/splat/service/dto/RoleDTO.java @@ -0,0 +1,66 @@ +package org.splat.service.dto; + +import java.util.Vector; + +/** + * Role DTO. + * + * @see UserDTO + */ +public class RoleDTO { + + private String username; + private String role; + + // ============================================================================================================================== + // Constructors + // ============================================================================================================================== + + // Database fetch constructor + protected RoleDTO() { + } + + // Initialization constructor + protected RoleDTO(String username, String role) { + this.username = username; + this.role = role; + } + + // ============================================================================================================================== + // Protected member functions + // ============================================================================================================================== + + protected void addRole(String role) { + this.role = this.role + "," + role; + } + + protected RoleDTO[] toArray() { + String[] name = role.split(","); + Vector role = new Vector(); + + for (int i = 0; i < name.length; i++) + role.add(new RoleDTO(username, name[i])); + return role.toArray(new RoleDTO[name.length]); + } + + // ============================================================================================================================== + // Public member functions + // ============================================================================================================================== + // In functions below, the role is supposed having previously been extracted as an array. + + public String getName() { + return role; + } + + public void setName(final String name) { + role = name; + } + + public boolean is(String name) { + return this.role.equals(name); + } + + public boolean isSame(RoleDTO other) { + return this.role.equals(other.role); + } +} \ No newline at end of file diff --git a/Workspace/Siman-Common/src/org/splat/service/dto/UserDTO.java b/Workspace/Siman-Common/src/org/splat/service/dto/UserDTO.java new file mode 100644 index 0000000..3d2b4c9 --- /dev/null +++ b/Workspace/Siman-Common/src/org/splat/service/dto/UserDTO.java @@ -0,0 +1,157 @@ +package org.splat.service.dto; + +import java.security.Principal; +import org.splat.kernel.Name; + +/** + * User DTO. + * + * @author Roman Kozlov (RKV) + */ +public class UserDTO implements Principal, Name { + + private String password; // Property without getter function + private String username; // Unique in the user directory + private String first; + private String last; + private String display; // Optional + private RoleDTO role = new RoleDTO(); // Roles as list (as stored into the database) + private String email; + private String organid; + + /** + * Persistent id of the type. + */ + private long rid; + + // ============================================================================================================================== + // Public member functions + // ============================================================================================================================== + + public boolean equals(Object item) { + if (item == null) + return false; + if (item instanceof String) { + return this.username.equals((String) item); // Usernames are unique + } else if (item instanceof UserDTO) { + UserDTO given = (UserDTO) item; + return (given.username.equals(this.username)); // Usernames are unique + } else { + return false; + } + } + + public String getDisplayName() { + if (display == null) + return last + " " + first; + else + return display; + } + + public String getFirstName() { + return first; + } + + public void setFirstName(final String name) { + first = name; + } + + public String getMailAddress() { + return email; + } + + public void setMailAddress(final String addr) { + email = addr; + } + + public String getName() { + return last; + } + + public void setName(final String name) { + last = name; + } + + public String getOrganizationName() { + return organid; + } + + public void setOrganizationName(String name) { + organid = name; + } + + public String getRoleNames() { + return role.getName(); + } + + public RoleDTO[] getRoles() { + return role.toArray(); + } + + public String getUsername() { + return username; + } + + public String toString() { + return last + " " + first; + } + + /** + * Get the rid. + * + * @return the rid + */ + public long getIndex() { + return rid; + } + + /** + * Set the rid. + * + * @param rid + * the rid to set + */ + public void setIndex(long rid) { + this.rid = rid; + } + + /** + * Get the password. + * @return the password + */ + public String getPassword() { + return password; + } + + /** + * Set the password. + * @param password the password to set + */ + public void setPassword(String password) { + this.password = password; + } + + /** + * Set the username. + * @param username the username to set + */ + public void setUsername(String username) { + this.username = username; + } + + /** + * Get the role. + * @return the role + */ + public RoleDTO getRole() { + return role; + } + + /** + * Set the role. + * @param role the role to set + */ + public void setRole(RoleDTO role) { + this.role = role; + } +} \ No newline at end of file diff --git a/Workspace/Siman-Common/src/org/splat/service/technical/ProjectSettingsService.java b/Workspace/Siman-Common/src/org/splat/service/technical/ProjectSettingsService.java index c9919c6..d979f0f 100644 --- a/Workspace/Siman-Common/src/org/splat/service/technical/ProjectSettingsService.java +++ b/Workspace/Siman-Common/src/org/splat/service/technical/ProjectSettingsService.java @@ -16,7 +16,6 @@ import java.util.List; import java.util.Set; import org.splat.dal.bo.som.ProjectElement; -import org.splat.service.technical.ProjectSettingsService.Step; import org.splat.service.technical.ProjectSettingsServiceImpl.FileNaming; /** diff --git a/Workspace/Siman-Common/src/org/splat/util/BeanHelper.java b/Workspace/Siman-Common/src/org/splat/util/BeanHelper.java index 167d417..4e31866 100644 --- a/Workspace/Siman-Common/src/org/splat/util/BeanHelper.java +++ b/Workspace/Siman-Common/src/org/splat/util/BeanHelper.java @@ -7,46 +7,53 @@ * @version $Revision$ *****************************************************************************/ -package org.splat.util; +package org.splat.util; import net.sf.beanlib.provider.BeanTransformer; import net.sf.beanlib.provider.replicator.BeanReplicator; -import net.sf.beanlib.spi.BeanTransformerSpi; - /** - * Helper class for beans. - * This class supplies : - * - methods to copy bean to an other bean + * Helper class for beans. This class supplies : - methods to copy bean to an other bean + * * @author Maria KRUCHININA - * + * */ public final class BeanHelper { - + /** * private constructor to make it abstract. */ - private BeanHelper(){ + private BeanHelper() { super(); } - + /** * copy a bean to a bean. - * @param the original type - * @param the target type - * @param from the original bean - * @param clazz the destination class + * + * @param + * the original type + * @param + * the target type + * @param from + * the original bean + * @param clazz + * the destination class * @return an instance of the destination class */ - public static D copyBean(final T from,final Class clazz) { + public static D copyBean(final T from, final Class clazz) { D result; - - if(from == null) { + + if (from == null) { result = null; } else { - BeanTransformerSpi transformer = new BeanTransformer(new TimestampTransformerFactory()); - BeanReplicator bp = new BeanReplicator(transformer); - + BeanTransformer bt = new BeanTransformer( + new TimestampTransformerFactory(), + new DTOTransformerFactory()); + // Don't check parameter types of getters and setters. + // Find them just by property name. + bt.initDetailedPropertyFilter(null); + + BeanReplicator bp = new BeanReplicator(bt); result = bp.replicateBean(from, clazz); } return result; diff --git a/Workspace/Siman-Common/src/org/splat/util/DTOTransformer.java b/Workspace/Siman-Common/src/org/splat/util/DTOTransformer.java new file mode 100644 index 0000000..9af8ea4 --- /dev/null +++ b/Workspace/Siman-Common/src/org/splat/util/DTOTransformer.java @@ -0,0 +1,107 @@ +/***************************************************************************** + * Company OPEN CASCADE + * Application SIMAN + * File $Id$ + * Creation date 05.10.2012 + * @author $Author$ + * @version $Revision$ + *****************************************************************************/ + +package org.splat.util; + +import java.util.Map; + +import org.splat.log.AppLogger; + +import net.sf.beanlib.PropertyInfo; +import net.sf.beanlib.spi.BeanTransformerSpi; +import net.sf.beanlib.spi.CustomBeanTransformerSpi; + +/** + * DTO transformer used by BeanHelper. + * + * @author Roman Kozlov (RKV) + */ +public class DTOTransformer implements CustomBeanTransformerSpi { + + /** + * The logger for the service. + */ + public final static AppLogger logger = AppLogger + .getLogger(DTOTransformer.class); + + /** + * the bean transformer. + */ + private final BeanTransformerSpi _beanTransformer; + + /** + * Constructor. + * + * @param beanTransformer + * the bean transformer + */ + public DTOTransformer(final BeanTransformerSpi beanTransformer) { + _beanTransformer = beanTransformer; + } + + /** + * + * {@inheritDoc} + * + * @see net.sf.beanlib.spi.CustomBeanTransformerSpi#isTransformable(java.lang.Object, java.lang.Class, net.sf.beanlib.PropertyInfo) + */ + public boolean isTransformable(final Object from, + final Class toClass, final net.sf.beanlib.PropertyInfo info) { + boolean ok = false; + if (from != null) { + String fromName = from.getClass().getSimpleName(); + String toName = toClass.getSimpleName(); + if (logger.isDebugEnabled()) { + logger.debug("From: " + fromName + "; To: " + toName); + ok = (fromName.equals(toName + "DTO") || toName.equals(fromName + + "DTO")); + } + } else { + if (logger.isDebugEnabled()) { + logger.debug("From: null; To: " + toClass.getSimpleName()); + } + } + if (logger.isDebugEnabled()) { + logger.debug("Can transform from " + info.getFromBean() + "." + + info.getPropertyName() + " to " + info.getToBean() + "." + + info.getPropertyName() + ": " + ok); + } + return ok; + } + + /** + * + * {@inheritDoc} + * + * @see net.sf.beanlib.spi.Transformable#transform(java.lang.Object, java.lang.Class, net.sf.beanlib.PropertyInfo) + */ + public T transform(final Object in, final Class toClass, + final PropertyInfo info) { + if (logger.isDebugEnabled()) { + logger.debug("Transform " + in.getClass().getSimpleName() + ": " + + in.toString() + " to " + toClass.getSimpleName()); + } + Map cloneMap = _beanTransformer.getClonedMap(); + Object clone = cloneMap.get(in); + + if (clone != null) { + return (T) clone; + } + + if (logger.isDebugEnabled()) { + logger.debug("Copy bean from " + info.getFromBean() + "." + + info.getPropertyName() + " to " + info.getToBean() + "." + + info.getPropertyName()); + } + clone = BeanHelper.copyBean(in, toClass); + cloneMap.put(in, clone); + + return (T) clone; + } +} diff --git a/Workspace/Siman-Common/src/org/splat/util/DTOTransformerFactory.java b/Workspace/Siman-Common/src/org/splat/util/DTOTransformerFactory.java new file mode 100644 index 0000000..76d75ab --- /dev/null +++ b/Workspace/Siman-Common/src/org/splat/util/DTOTransformerFactory.java @@ -0,0 +1,30 @@ +/***************************************************************************** + * Company OPEN CASCADE + * Application SIMAN + * File $Id$ + * Creation date 05.10.2012 + * @author $Author$ + * @version $Revision$ + *****************************************************************************/ + +package org.splat.util; + +import net.sf.beanlib.spi.BeanTransformerSpi; +import net.sf.beanlib.spi.CustomBeanTransformerSpi; + +/** + * Factory for creation DTO transformer(s). + * @author Roman Kozlov (RKV) + */ +public class DTOTransformerFactory implements CustomBeanTransformerSpi.Factory { + + /** + * {@inheritDoc} + * @see net.sf.beanlib.spi.CustomBeanTransformerSpi$Factory#newCustomBeanTransformer(net.sf.beanlib.spi.BeanTransformerSpi) + */ + public CustomBeanTransformerSpi newCustomBeanTransformer(BeanTransformerSpi beanTransformer) { + return new DTOTransformer(beanTransformer); + } + +} + diff --git a/Workspace/Siman-Common/src/spring/businessServiceContext.xml b/Workspace/Siman-Common/src/spring/businessServiceContext.xml index 0e58004..9dbba55 100644 --- a/Workspace/Siman-Common/src/spring/businessServiceContext.xml +++ b/Workspace/Siman-Common/src/spring/businessServiceContext.xml @@ -48,6 +48,8 @@ http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> + DTOTransformer is intended to be used by BeanHelper for transforming DTO to appropriate BO and vice + * versa. + * + * @author Roman Kozlov (RKV) + * + */ +@Test(groups = { "util" }) +public class TestDTOTransformer extends BaseTest { + + /** + * Logger for the class. + */ + private static final AppLogger LOG = AppLogger + .getLogger(TestDTOTransformer.class); + + /** + * Test transforming a knowledge element to a DTO.
+ * Description :
+ * Create a knowledge element BO and transform it to a DTO.
+ * + * Outcome results:
+ * + *
    + *
  • DTO should be filled correctly including date, knoledge type and author properties
    + *
  • + *
+ *
+ * + * @throws InvalidPropertyException + * if an invalid property is used when creating objects + * @throws MultiplyDefinedException + * when trying to create an object with already existing id + * @throws MissedPropertyException + * if a mandatory property is not defined for an object to be created + * + */ + @Test + public void testBOtoDTO() throws InvalidPropertyException, + MissedPropertyException, MultiplyDefinedException { + LOG.debug(">>>>> BEGIN testBOtoDTO()"); + + KnowledgeElement aKelm = getKnowledgeElement(); + KnowledgeElementDTO aDTO = BeanHelper.copyBean(aKelm, + KnowledgeElementDTO.class); + Assert.assertEquals(aDTO.getTitle(), + aKelm.getTitle(), "Titles are not equal."); + Assert.assertEquals(aDTO.getValue(), + aKelm.getValue(), "Values are not equal."); + Assert + .assertNotNull(aDTO.getAuthor(), + "Author property is not copied."); + Assert + .assertNotNull(aDTO.getAuthor().getRole(), + "Author.role property is not copied."); + Assert.assertEquals(aDTO.getAuthor().getName(), + aKelm.getAuthor().getName(), "Author names are not equal."); + Assert.assertNotNull(aDTO.getProgressState(), + "ProgressState property is not copied."); + Assert.assertEquals(aDTO.getProgressState(), + aKelm.getProgressState(), "Progress states are not equal."); + Assert.assertNotNull(aDTO.getType(), "Type property is not copied."); + Assert.assertEquals(aDTO.getType().getName(), + aKelm.getType().getName(), "Type names are not equal."); + Assert.assertEquals(aDTO.getAuthor().getRoleNames(), + aKelm.getAuthor().getRoleNames(), "Author role names are not equal."); + LOG.debug(">>>>> END testBOtoDTO()"); + } + + /** + * Create a transient KnowledgeElement for tests. + * + * @return a transient KnowledgeElement + * @throws InvalidPropertyException + * if an invalid property is used when creating objects + * @throws MultiplyDefinedException + * when trying to create an object with already existing id + * @throws MissedPropertyException + * if a mandatory property is not defined for an object to be created + */ + private KnowledgeElement getKnowledgeElement() + throws InvalidPropertyException, MissedPropertyException, + MultiplyDefinedException { + // Create a test knowledge type + + KnowledgeElement.Properties kprops = new KnowledgeElement.Properties(); + KnowledgeElementType aKType = new KnowledgeElementType("TST_kelmtype"); + // Create a test user + User.Properties uprop = new User.Properties(); + uprop.setUsername("TST_Username").setName("TST_SimanUnitTestsUser") + .setFirstName("TST_FirstName").setDisplayName("TST_test.user") + .addRole("TST_user").setMailAddress( + "noreply@salome-platform.org"); + uprop.disableCheck(); + User anAuthor = new User(uprop); + // Create a test study + Study.Properties stprops = new Study.Properties().setReference( + "TST_SID_01").setTitle("TST_Study").setManager(anAuthor); + Study aStudy = new Study(stprops); + // Create a test scenario + Scenario.Properties sprops = new Scenario.Properties().setTitle( + "TST_Study").setManager(anAuthor).setOwnerStudy(aStudy); + Scenario aScenario = new Scenario(sprops); + aStudy.getScenariiList().add(aScenario); + + Date aDate = new Date(); + + // Prepare a knowledge element transient object + kprops + .setTitle("Test knowledge element") + .setAuthor(anAuthor) + .setOwnerScenario(aScenario) + .setType(aKType) + .setDate(aDate) + .setValue( + "This is the test knowledge element.\nIt is created by the unit test."); + + return new KnowledgeElement(kprops); + } +} diff --git a/Workspace/Siman/src/org/splat/simer/Action.java b/Workspace/Siman/src/org/splat/simer/Action.java index 7b56bc4..71e8bb1 100644 --- a/Workspace/Siman/src/org/splat/simer/Action.java +++ b/Workspace/Siman/src/org/splat/simer/Action.java @@ -13,6 +13,7 @@ import org.apache.struts2.interceptor.ServletRequestAware; import org.apache.struts2.interceptor.SessionAware; import org.apache.log4j.Logger; import org.splat.dal.bo.kernel.User; +import org.splat.service.dto.KnowledgeElementDTO; import org.splat.som.ApplicationRights; import org.splat.som.StudyRights; import org.splat.dal.bo.som.KnowledgeElement; @@ -171,7 +172,7 @@ public class Action extends ActionSupport implements ServletRequestAware, Sessio // _openStudy = (OpenStudy)session.get("study.open"); return _openStudy; // May be null } - protected OpenKnowledge open (KnowledgeElement kelm) { + protected OpenKnowledge open (KnowledgeElementDTO kelm) { OpenKnowledge open = _openKnowledge.open(kelm); closeKnowledge(); // Just in case @@ -255,7 +256,7 @@ public class Action extends ActionSupport implements ServletRequestAware, Sessio getLeftMenuSettings().setMenuNamespace(menu.getNamespace()); } - /**s + /** * Initialization the Context for Menu Bar and Tool Bar. * * @param titleProperty - The title of the open study/knowledge. diff --git a/Workspace/Siman/src/org/splat/simer/DisplayKnowledgeAction.java b/Workspace/Siman/src/org/splat/simer/DisplayKnowledgeAction.java index 2495f13..339742c 100644 --- a/Workspace/Siman/src/org/splat/simer/DisplayKnowledgeAction.java +++ b/Workspace/Siman/src/org/splat/simer/DisplayKnowledgeAction.java @@ -4,6 +4,7 @@ import java.util.List; import org.splat.dal.bo.som.KnowledgeElement; import org.splat.service.KnowledgeElementService; +import org.splat.service.dto.KnowledgeElementDTO; import org.splat.som.Step; public class DisplayKnowledgeAction extends DisplayBaseAction { @@ -60,14 +61,14 @@ public class DisplayKnowledgeAction extends DisplayBaseAction { public String doOpen() { myknelm = getOpenKnowledge(); - if (myindex != null) + if (myindex != null) { try { // Opening a knowledge from the search result int index = Integer.valueOf(myindex); if (myknelm != null && myknelm.getIndex() == index) { // - The selected knowledge is currently open selection = myknelm.getSelection(); // Current selection } else { // - The selected knowledge is new - KnowledgeElement kelm = getKnowledgeElementService() - .selectKnowledgeElement(index); + KnowledgeElementDTO kelm = getKnowledgeElementService() + .getKnowledgeElement(index); myknelm = open(kelm); selection = myknelm.getSelection(); // Default selection } @@ -75,9 +76,9 @@ public class DisplayKnowledgeAction extends DisplayBaseAction { logger.error("Reason:", error); return ERROR; } - else if (selection != null) { // Re-opening (refreshing) the currently open knowledge - KnowledgeElement kelm = getKnowledgeElementService() - .selectKnowledgeElement(myknelm.getIndex()); + } else if (selection != null) { // Re-opening (refreshing) the currently open knowledge + KnowledgeElementDTO kelm = getKnowledgeElementService() + .getKnowledgeElement(myknelm.getIndex()); myknelm = open(kelm); // Closes the previously open knowledge myknelm.setSelection(selection); } diff --git a/Workspace/Siman/src/org/splat/simer/OpenKnowledge.java b/Workspace/Siman/src/org/splat/simer/OpenKnowledge.java index aa64770..63e7fff 100644 --- a/Workspace/Siman/src/org/splat/simer/OpenKnowledge.java +++ b/Workspace/Siman/src/org/splat/simer/OpenKnowledge.java @@ -5,23 +5,38 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.ResourceBundle; -import java.util.Vector; import org.splat.dal.bo.som.KnowledgeElement; import org.splat.dal.bo.som.ProgressState; -import org.splat.dal.bo.som.Scenario; import org.splat.dal.bo.som.SimulationContext; -import org.splat.service.ProjectElementService; -import org.splat.service.technical.ProjectSettingsService; +import org.splat.service.dto.KnowledgeElementDTO; import org.splat.som.Step; import org.splat.wapp.SimpleMenu; +/** + * Open knowledge details for presentation. + */ public class OpenKnowledge extends OpenObject { - private KnowledgeElement myknelm; + /** + * The open knowledge element DTO. + */ + private KnowledgeElementDTO myknelm = new KnowledgeElementDTO(); + /** + * Creation date. + */ private String credate; + + /** + * Knowledge element left menu. + */ public class Menu extends SimpleMenu { - // ------------------------------------ + /** + * Build menu from the list of study steps. + * + * @param context + * the list of steps + */ public Menu(List context) { super("steps", "study"); int i = 0; @@ -47,22 +62,30 @@ public class OpenKnowledge extends OpenObject { // Constructor // ============================================================================================================================== - public OpenKnowledge open (KnowledgeElement knelm) { - // --------------------------------------------- + /** + * Set the given knowledge as currently open. + * + * @param knelm + * the knowledge element DTO + * @return the open knowledge presentation + */ + public OpenKnowledge open(KnowledgeElementDTO knelm) { ResourceBundle label = ResourceBundle.getBundle("labels", getApplicationSettings().getCurrentLocale()); ResourceBundle custom = ResourceBundle.getBundle("som", getApplicationSettings().getCurrentLocale()); - SimpleDateFormat convert = new SimpleDateFormat( - custom.getString("date.format")); + SimpleDateFormat convert = new SimpleDateFormat(custom + .getString("date.format")); StringBuffer value = new StringBuffer(knelm.getValue()); - Scenario scene = knelm.getOwnerScenario(); + String sceneTitle = knelm.getScenarioTitle(); + String studyTitle = knelm.getStudyTitle(); myknelm = knelm; // Preparation of the display credate = convert.format(myknelm.getDate()); - involving = getAllSteps(knelm.getOwnerScenario()); + // involving = getAllSteps(knelm.getOwnerScenario()); + involving = knelm.getInvolving(); context = new ArrayList(); menu = new Menu(getInvolvedSteps()); selection = menu.getSelection(); // The default selection is set in the menu definition @@ -73,15 +96,17 @@ public class OpenKnowledge extends OpenObject { ustep = next; for (Iterator j = next .getAllSimulationContexts().iterator(); j.hasNext();) { - context.add(new SimulationContextFacade(j.next(), getProjectSettings().getAllSteps())); + context.add(new SimulationContextFacade(j.next(), + getProjectSettings().getAllSteps())); } } - value.append("

").append("") - .append(label.getString("label.source")).append(":") - .append("
").append(label.getString("label.study")) - .append(" \"").append(scene.getOwnerStudy().getTitle()) - .append("\",").append("
").append(scene.getTitle()) - .append(".").append("

"); + value.append("

").append("").append( + label.getString("label.source")).append(":").append("
") + .append(label.getString("label.study")).append(" \"") + .append(studyTitle/* scene.getOwnerStudy().getTitle() */) + .append("\",").append("
") + .append(sceneTitle/* scene.getTitle() */).append(".").append( + "

"); description = value.toString(); setupContents(); // Initializes documents and knowledge at ustep return this; @@ -92,54 +117,44 @@ public class OpenKnowledge extends OpenObject { // ============================================================================================================================== public String getAuthorName() { - // ----------------------------- return myknelm.getAuthor().toString(); } public String getDate() { - // ------------------------ return credate; } public Long getIndex() { - // ------------------------- return myknelm.getIndex(); } public Menu getMenu() { - // ---------------------- return (Menu) menu; } public ProgressState getProgressState() { - // --------------------------------------- return myknelm.getProgressState(); } public String getReference() { - // ---------------------------- return myknelm.getReference(); } - public KnowledgeElement getKnowledgeObject() { - // --------------------------------------------- + public KnowledgeElementDTO getKnowledgeObject() { return myknelm; } public String getTitle() { - // ------------------------ return myknelm.getTitle(); } public String getType() { - // ------------------------ return ResourceBundle.getBundle("som", getApplicationSettings().getCurrentLocale()).getString( "type.knowledge." + myknelm.getType().getName()); } public void setSelection(String step) { - // -------------------------------------- selection = step; int index = Integer.valueOf(selection); for (Iterator i = involving.iterator(); i.hasNext();) { @@ -150,33 +165,4 @@ public class OpenKnowledge extends OpenObject { menu.selects(selection); setupContents(); // The contents may have changed even if the selection is the same } - - // ============================================================================================================================== - // Private services - // ============================================================================================================================== - - private List getAllSteps(Scenario scenar) { - // ------------------------------------------------ - Vector result = new Vector(); - Step[] step = getProjectElementService().getSteps(scenar); - - int base = step[0].getNumber(); - int last = step[step.length - 1].getNumber(); - for (int i = 0; i < step.length; i++) { - result.add(step[i]); - } - step = getProjectElementService().getSteps(scenar.getOwnerStudy()); - for (int i = step.length - 1; i > -1; i--) { - if (step[i].getNumber() >= base) - continue; - result.add(0, step[i]); - } - for (int i = 0; i < step.length; i++) { - if (step[i].getNumber() <= last) - continue; - result.add(step[i]); - } - return result; - } - } \ No newline at end of file diff --git a/Workspace/Siman/src/org/splat/simer/OpenObject.java b/Workspace/Siman/src/org/splat/simer/OpenObject.java index 8231fa5..a4a9bdd 100644 --- a/Workspace/Siman/src/org/splat/simer/OpenObject.java +++ b/Workspace/Siman/src/org/splat/simer/OpenObject.java @@ -28,17 +28,44 @@ public abstract class OpenObject implements Proxy { protected static HashMap docpres = null; protected static HashMap knowpres = null; - protected User cuser = null; // Connected user - protected String selection = null; // Menu selected by the user - protected Step ustep = null; // Corresponding selected step - protected String description = null; // Object description (rich text) + /** + * Connected user. + */ + protected User cuser = null; + /** + * Menu selected by the user. + */ + protected String selection = null; + /** + * Corresponding selected step. + */ + protected Step ustep = null; + /** + * Object description (rich text). + */ + protected String description = null; protected List involving = new ArrayList(); + /** + * Simulation Context display representations. + */ protected List context = new ArrayList(); // Simulation Context display representations - protected List contents = null; // Document display representations - protected List knowledge = null; // Knowledge Element display representations structured by knowledge types + /** + * Document display representations. + */ + protected List contents = null; + /** + * Knowledge Element display representations structured by knowledge types. + */ + protected List knowledge = null; - protected Menu menu = null; // Left pane menu of this object - protected PopupMenu popup = null; // Pop-up menu of this object, if the user has write access + /** + * Left pane menu of this object. + */ + protected Menu menu = null; + /** + * Pop-up menu of this object, if the user has write access. + */ + protected PopupMenu popup = null; /** * Injected project settings service. */