branch feature/7017 updated (1d3e4d3 -> 30a186b)
This is an automated email from the git hooks/post-receive script. New change to branch feature/7017 in repository observe. See http://git.codelutin.com/observe.git from 1d3e4d3 continue les écrans simples sur le modèle seine (refs #7017) new 30a186b refactor save action The 1 revisions listed above as "new" are entirely new to this repository and will be described in separate emails. The revisions listed as "adds" were already present in the repository and have only been added to this reference. Detailed log of new commits: commit 30a186be5bd9ea4ba82ad3e2313ae4f323364073 Author: Sylvain Bavencoff <bavencoff@codelutin.com> Date: Mon Apr 27 11:48:38 2015 +0200 refactor save action Summary of changes: .../observe/services/AbstractObserveService.java | 88 ++++++++++++++++++++++ .../services/data/seine/TripSeineService.java | 2 +- .../services/data/seine/TripSeineServiceImpl.java | 54 +++++-------- .../open/impl/seine/TripSeineUIHandler.java | 4 +- 4 files changed, 108 insertions(+), 40 deletions(-) -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@list.forge.codelutin.com>.
This is an automated email from the git hooks/post-receive script. New commit to branch feature/7017 in repository observe. See http://git.codelutin.com/observe.git commit 30a186be5bd9ea4ba82ad3e2313ae4f323364073 Author: Sylvain Bavencoff <bavencoff@codelutin.com> Date: Mon Apr 27 11:48:38 2015 +0200 refactor save action --- .../observe/services/AbstractObserveService.java | 88 ++++++++++++++++++++++ .../services/data/seine/TripSeineService.java | 2 +- .../services/data/seine/TripSeineServiceImpl.java | 54 +++++-------- .../open/impl/seine/TripSeineUIHandler.java | 4 +- 4 files changed, 108 insertions(+), 40 deletions(-) diff --git a/observe-services/src/main/java/fr/ird/observe/services/AbstractObserveService.java b/observe-services/src/main/java/fr/ird/observe/services/AbstractObserveService.java index b0a0c17..4870d6a 100644 --- a/observe-services/src/main/java/fr/ird/observe/services/AbstractObserveService.java +++ b/observe-services/src/main/java/fr/ird/observe/services/AbstractObserveService.java @@ -17,6 +17,7 @@ import org.nuiton.topia.TopiaContext; import org.nuiton.topia.persistence.TopiaDAO; import org.nuiton.topia.persistence.TopiaEntity; import org.nuiton.topia.persistence.util.TopiaEntityBinder; +import org.nuiton.topia.persistence.util.TopiaEntityHelper; import org.nuiton.util.beans.BinderModelBuilder; import java.util.ArrayList; @@ -33,6 +34,10 @@ public abstract class AbstractObserveService implements ObserveService { /** Logger. */ private static final Log log = LogFactory.getLog(AbstractObserveService.class); + String PARENT_BEAN = "parentBean"; + + String BEAN = "bean"; + protected ObserveServiceContext serviceContext; @Override @@ -199,4 +204,87 @@ public abstract class AbstractObserveService implements ObserveService { } + public <P extends TopiaEntity, E extends TopiaEntity> String doSave(String parentId, E toSave, SaveAction<P, E> saveAction) { + E saved; + + saveAction.beforeSave(parentId, toSave); + + P parent = null; + + if (parentId != null) { + + parent = findByTopiaId(saveAction.parentClass, parentId); + + checkNotNullAndExistingEntity(PARENT_BEAN, parent); + } + + if (toSave.getTopiaId() == null) { + + checkNotNullAndNoneExistingEntity(BEAN, toSave); + + saved = saveAction.onCreate(parent, toSave); + + } else { + + checkNotNullAndExistingEntity(BEAN, toSave); + + saved = findByTopiaId(saveAction.entityClass, toSave.getTopiaId()); + + checkNotNullAndExistingEntity(BEAN, saved); + + saved = saveAction.onUpdate(parent, toSave, saved); + } + + if (parent == null) { + + // sauvegarde de l'entité + getDao(saveAction.entityClass).update(saved); + + } else { + + // sauvegarde du père de l'entité + getDao(saveAction.parentClass).update(parent); + + } + + return saved.getTopiaId(); + + + } + + protected abstract class SaveAction<P extends TopiaEntity, E extends TopiaEntity> { + + protected final Class<P> parentClass; + + protected final Class<E> entityClass; + + public SaveAction(Class<P> parentClass, Class<E> entityClass) { + this.parentClass = parentClass; + this.entityClass = entityClass; + } + + public void beforeSave(String parentId, E toSave) { + + } + + public E onCreate(P parent, E toCreate) { + return getDao(entityClass).update(toCreate); + }; + + public abstract E onUpdate(P parentBean, E toSave, E beanToSave); + } + + + public <E extends TopiaEntity> void checkNotNullAndNoneExistingEntity(String variableName, E entity) { + TopiaEntityHelper.checkNotNullAndNoneExistingEntity(variableName, entity); + } + + + public <E extends TopiaEntity> void checkNotNullAndExistingEntity(String variableName, E entity) { + TopiaEntityHelper.checkNotNullAndExistingEntity(variableName, entity); + } + + + + } diff --git a/observe-services/src/main/java/fr/ird/observe/services/data/seine/TripSeineService.java b/observe-services/src/main/java/fr/ird/observe/services/data/seine/TripSeineService.java index bd16f3e..924ea39 100644 --- a/observe-services/src/main/java/fr/ird/observe/services/data/seine/TripSeineService.java +++ b/observe-services/src/main/java/fr/ird/observe/services/data/seine/TripSeineService.java @@ -29,7 +29,7 @@ public interface TripSeineService extends ObserveService { TripSeine preCreate(String programId); @Commit - TripSeine save(TripSeine toSave); + String save(TripSeine toSave); @Commit void delete(String tripSeineId); diff --git a/observe-services/src/main/java/fr/ird/observe/services/data/seine/TripSeineServiceImpl.java b/observe-services/src/main/java/fr/ird/observe/services/data/seine/TripSeineServiceImpl.java index 0d99d82..46fb48d 100644 --- a/observe-services/src/main/java/fr/ird/observe/services/data/seine/TripSeineServiceImpl.java +++ b/observe-services/src/main/java/fr/ird/observe/services/data/seine/TripSeineServiceImpl.java @@ -101,30 +101,27 @@ public class TripSeineServiceImpl extends AbstractObserveService implements Trip } @Override - public TripSeine save(TripSeine toSave) { + public String save(TripSeine toSave) { + String tripSeineId = doSave(null, toSave, new SaveAction<Program, TripSeine>(Program.class, TripSeine.class) { - // on force toujours la date a etre sans heure, minute,... - Date startDate = DateUtil.getDay(toSave.getStartDate()); - toSave.setStartDate(startDate); - - // mise a jour de la date de fin - toSave.updateDateFin(); - - TripSeine saved; - - if (toSave.getTopiaId() == null) { - - // create - saved = create(toSave); - - } else { + @Override + public void beforeSave(String parentId, TripSeine toSave) { + super.beforeSave(parentId, toSave); + Date startDate = DateUtil.getDay(toSave.getStartDate()); + toSave.setStartDate(startDate); - // update - saved = update(toSave); + // mise a jour de la date de fin + toSave.updateDateFin(); + } - } + @Override + public TripSeine onUpdate(Program parentBean, TripSeine toUpdate, TripSeine updated) { + getBinderForEdit().copyExcluding(toUpdate, updated, TripSeine.PROPERTY_ROUTE); + return updated; + } + }); - return saved; + return tripSeineId; } @@ -183,21 +180,4 @@ public class TripSeineServiceImpl extends AbstractObserveService implements Trip } - protected TripSeine create(TripSeine toCreate) { - - TripSeineDAO dao = getDao(); - TripSeine created = dao.create(toCreate); - return created; - - } - - protected TripSeine update(TripSeine toUpdate) { - - TripSeineDAO dao = getDao(); - TripSeine updated = dao.findByTopiaId(toUpdate.getTopiaId()); - getBinderForEdit().copyExcluding(toUpdate, updated, TripSeine.PROPERTY_ROUTE); - return updated; - - } - } diff --git a/observe-swing/src/main/java/fr/ird/observe/ui/content/open/impl/seine/TripSeineUIHandler.java b/observe-swing/src/main/java/fr/ird/observe/ui/content/open/impl/seine/TripSeineUIHandler.java index 234ab99..27ca6a9 100644 --- a/observe-swing/src/main/java/fr/ird/observe/ui/content/open/impl/seine/TripSeineUIHandler.java +++ b/observe-swing/src/main/java/fr/ird/observe/ui/content/open/impl/seine/TripSeineUIHandler.java @@ -227,8 +227,8 @@ public class TripSeineUIHandler extends ContentOpenableUIHandler<TripSeine> { bean.setOpen(true); TripSeineService service = getService(TripSeineService.class); - TripSeine saved = service.save(bean); - bean.setTopiaId(saved.getTopiaId()); + String saveId = service.save(bean); + bean.setTopiaId(saveId); // recuperation de la position de la maree dans le program obtainChildPosition(bean); -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@list.forge.codelutin.com>.
participants (1)
-
codelutin.com scm