Isis-fish-commits
Threads by month
- ----- 2026 -----
- June
- May
- April
- March
- February
- January
- ----- 2025 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2010 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2009 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2008 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2007 -----
- December
- November
November 2016
- 2 participants
- 13 discussions
r4379 - trunk/src/main/java/fr/ifremer/isisfish/logging
by bpoussin@users.forge.codelutin.com 30 Nov '16
by bpoussin@users.forge.codelutin.com 30 Nov '16
30 Nov '16
Author: bpoussin
Date: 2016-11-30 17:04:30 +0100 (Wed, 30 Nov 2016)
New Revision: 4379
Url: http://forge.codelutin.com/projects/isis-fish/repository/revisions/4379
Log:
Evolution #8451: Log de toutes les modifications de la base
on ne log que s'il y a vraiment un changement (old != new)
Modified:
trunk/src/main/java/fr/ifremer/isisfish/logging/RegionChangeLogger.java
Modified: trunk/src/main/java/fr/ifremer/isisfish/logging/RegionChangeLogger.java
===================================================================
--- trunk/src/main/java/fr/ifremer/isisfish/logging/RegionChangeLogger.java 2016-11-30 14:05:04 UTC (rev 4378)
+++ trunk/src/main/java/fr/ifremer/isisfish/logging/RegionChangeLogger.java 2016-11-30 16:04:30 UTC (rev 4379)
@@ -46,6 +46,7 @@
import fr.ifremer.isisfish.IsisFishRuntimeException;
import fr.ifremer.isisfish.datastore.RegionStorage;
+import org.apache.commons.lang3.StringUtils;
/**
* Listener topia qui ecrit un fichier de log avec toutes les modifications de la base de données
@@ -109,12 +110,17 @@
addLogLine(changeType, clazz, entity, "", "", "");
}
- protected void addLogLine(String changeType, Class<? extends TopiaEntity> clazz, Object entity, String field, String oldValue, String newValue) {
- String date = DateFormatUtils.format(new Date(), "yyyy/MM/dd HH:mm:ss");
- String entityDisplayInfo = getEntityDisplayInfo(entity);
- addLine(date, changeType, clazz.getSimpleName().replace("Impl", ""), entityDisplayInfo, field, oldValue, newValue);
+ protected void addLogLine(String changeType, Class<? extends TopiaEntity> clazz,
+ Object entity, String field, String oldValue, String newValue) {
+ // on ne log que s'il y a vraiment un changement
+ if (!StringUtils.equals(oldValue, newValue)) {
+ String date = DateFormatUtils.format(new Date(), "yyyy/MM/dd HH:mm:ss");
+ String entityDisplayInfo = getEntityDisplayInfo(entity);
+ addLine(date, changeType, clazz.getSimpleName().replace("Impl", ""), entityDisplayInfo, field, oldValue, newValue);
+ }
}
+
/**
* Add line into file.
*
1
0
Author: bpoussin
Date: 2016-11-30 15:05:04 +0100 (Wed, 30 Nov 2016)
New Revision: 4378
Url: http://forge.codelutin.com/projects/isis-fish/repository/revisions/4378
Log:
fixes #8834: Supprimer l'option -Xmx des scripts de lancement
Modified:
trunk/src/main/assembly/isisfish.bat
trunk/src/main/assembly/isisfish.sh
Modified: trunk/src/main/assembly/isisfish.bat
===================================================================
--- trunk/src/main/assembly/isisfish.bat 2016-11-30 13:31:05 UTC (rev 4377)
+++ trunk/src/main/assembly/isisfish.bat 2016-11-30 14:05:04 UTC (rev 4378)
@@ -5,4 +5,4 @@
rem SET PATH=%PATH%;%R_HOME%\bin\i386
echo [Script] Isis starting...
-java -Xmx2048M -jar ${project.build.finalName}.${project.packaging} %1 %2 %3 %4 %5 %6 %7 %8 %9 > debug.txt 2>&1
+java -jar ${project.build.finalName}.${project.packaging} %1 %2 %3 %4 %5 %6 %7 %8 %9 > debug.txt 2>&1
Modified: trunk/src/main/assembly/isisfish.sh
===================================================================
--- trunk/src/main/assembly/isisfish.sh 2016-11-30 13:31:05 UTC (rev 4377)
+++ trunk/src/main/assembly/isisfish.sh 2016-11-30 14:05:04 UTC (rev 4378)
@@ -1,5 +1,5 @@
#!/bin/bash
-MEMORY="-Xmx2048M"
+#MEMORY="-Xmx2048M"
java $MEMORY -jar ${project.build.finalName}.${project.packaging} $* &> debug.txt
1
0
r4377 - in trunk/src/main/java/fr/ifremer/isisfish: entities ui/input
by bpoussin@users.forge.codelutin.com 30 Nov '16
by bpoussin@users.forge.codelutin.com 30 Nov '16
30 Nov '16
Author: bpoussin
Date: 2016-11-30 14:31:05 +0100 (Wed, 30 Nov 2016)
New Revision: 4377
Url: http://forge.codelutin.com/projects/isis-fish/repository/revisions/4377
Log:
changement de l'extension du fichier .isonz -> .isis.json.gz
Modified:
trunk/src/main/java/fr/ifremer/isisfish/entities/RegionExportJson.java
trunk/src/main/java/fr/ifremer/isisfish/ui/input/InputHandler.java
Modified: trunk/src/main/java/fr/ifremer/isisfish/entities/RegionExportJson.java
===================================================================
--- trunk/src/main/java/fr/ifremer/isisfish/entities/RegionExportJson.java 2016-11-30 13:29:51 UTC (rev 4376)
+++ trunk/src/main/java/fr/ifremer/isisfish/entities/RegionExportJson.java 2016-11-30 13:31:05 UTC (rev 4377)
@@ -62,6 +62,7 @@
static private Log log = LogFactory.getLog(RegionExportJson.class);
final static public int FORMAT_VERSION = 1;
+ final static public String FORMAT_EXTENSION = ".isis.json.gz";
protected LinkedList<TopiaEntity> toVisit;
protected Set<String> visited;
Modified: trunk/src/main/java/fr/ifremer/isisfish/ui/input/InputHandler.java
===================================================================
--- trunk/src/main/java/fr/ifremer/isisfish/ui/input/InputHandler.java 2016-11-30 13:29:51 UTC (rev 4376)
+++ trunk/src/main/java/fr/ifremer/isisfish/ui/input/InputHandler.java 2016-11-30 13:31:05 UTC (rev 4377)
@@ -385,13 +385,12 @@
*/
protected void exportJson(TopiaEntity e) {
try {
- File file = FileUtil.getFile(".*.isonz$",
+ File file = FileUtil.getFile(".*" + RegionExportJson.FORMAT_EXTENSION + "$",
t("isisfish.message.import.region.zipped"));
if (file != null) {
- // add .isonz extension is not set
- if (!file.getAbsolutePath().endsWith(".isonz")) {
- file = new File(file.getAbsolutePath() + ".isonz");
+ if (!file.getAbsolutePath().endsWith(RegionExportJson.FORMAT_EXTENSION)) {
+ file = new File(file.getAbsolutePath() + RegionExportJson.FORMAT_EXTENSION);
}
int resp = JOptionPane.YES_OPTION;
@@ -431,35 +430,40 @@
public void importEntityJson() {
setStatusMessage(inputUI, t("isisfish.message.import.zip"), true);
try {
- File file = FileUtil.getFile(".*\\.isonz$",
- t("isisfish.message.import.region.zipped"));
InputSaveVerifier inputSaveVerifier = inputUI.getContextValue(InputSaveVerifier.class);
- if (file != null && inputSaveVerifier.checkEdit() != JOptionPane.CANCEL_OPTION) {
- FisheryRegion fisheryRegion = inputUI.getContextValue(FisheryRegion.class);
- TopiaContext tx = fisheryRegion.getTopiaContext();
- RegionImportJson.RegionMergeDatabase merge =
- new RegionImportJson.RegionMergeDatabase(tx);
- try (InputStream in = new BufferedInputStream(
- new GZIPInputStream(new FileInputStream(file)))) {
- RegionImportJson json = new RegionImportJson(new InputStreamReader(in, "UTF-8"),
- merge);
- Collection<TopiaEntity> entities = json.getEntities();
- log.info("Entities to importe: " + entities.size());
- for (TopiaEntity e : entities) {
- tx.add(e);
+ if (inputSaveVerifier.checkEdit() != JOptionPane.CANCEL_OPTION) {
+ File file = FileUtil.getFile(".*" + RegionExportJson.FORMAT_EXTENSION + "$",
+ t("isisfish.message.import.region.zipped"));
+ if (file != null) {
+ if (!file.getAbsolutePath().endsWith(RegionExportJson.FORMAT_EXTENSION)) {
+ file = new File(file.getAbsolutePath() + RegionExportJson.FORMAT_EXTENSION);
}
- tx.commitTransaction();
- log.info("Import merged");
- } catch (Exception eee) {
- tx.rollbackTransaction();
- if (!merge.isAbort()) {
- throw eee;
- } else {
- log.info("Import aborted");
+ FisheryRegion fisheryRegion = inputUI.getContextValue(FisheryRegion.class);
+ TopiaContext tx = fisheryRegion.getTopiaContext();
+ RegionImportJson.RegionMergeDatabase merge =
+ new RegionImportJson.RegionMergeDatabase(tx);
+ try (InputStream in = new BufferedInputStream(
+ new GZIPInputStream(new FileInputStream(file)))) {
+ RegionImportJson json = new RegionImportJson(new InputStreamReader(in, "UTF-8"),
+ merge);
+ Collection<TopiaEntity> entities = json.getEntities();
+ log.info("Entities to importe: " + entities.size());
+ for (TopiaEntity e : entities) {
+ tx.add(e);
+ }
+ tx.commitTransaction();
+ log.info("Import merged");
+ } catch (Exception eee) {
+ tx.rollbackTransaction();
+ if (!merge.isAbort()) {
+ throw eee;
+ } else {
+ log.info("Import aborted");
+ }
}
+ inputSaveVerifier.cancel();
+ loadRegion(fisheryRegion.getName());
}
- inputSaveVerifier.cancel();
- loadRegion(fisheryRegion.getName());
}
} catch (Exception eee) {
throw new IsisFishRuntimeException("Can't import region", eee);
1
0
r4376 - trunk/src/main/java/fr/ifremer/isisfish/map
by bpoussin@users.forge.codelutin.com 30 Nov '16
by bpoussin@users.forge.codelutin.com 30 Nov '16
30 Nov '16
Author: bpoussin
Date: 2016-11-30 14:29:51 +0100 (Wed, 30 Nov 2016)
New Revision: 4376
Url: http://forge.codelutin.com/projects/isis-fish/repository/revisions/4376
Log:
fixes #8833: Lorsqu'on utilise ses propres fichiers cartes, ne pas utiliser de couleur de fond
Modified:
trunk/src/main/java/fr/ifremer/isisfish/map/IsisMapBean.java
Modified: trunk/src/main/java/fr/ifremer/isisfish/map/IsisMapBean.java
===================================================================
--- trunk/src/main/java/fr/ifremer/isisfish/map/IsisMapBean.java 2016-11-29 16:22:02 UTC (rev 4375)
+++ trunk/src/main/java/fr/ifremer/isisfish/map/IsisMapBean.java 2016-11-30 13:29:51 UTC (rev 4376)
@@ -356,6 +356,15 @@
}
}
+ protected void addDrawingAttributes(Properties p, String layerId, String lineColor, String fillColor) {
+ if (lineColor != null) {
+ p.setProperty(layerId + "." + DrawingAttributes.linePaintProperty, lineColor);
+ }
+ if (fillColor != null) {
+ p.setProperty(layerId + "." + DrawingAttributes.fillPaintProperty, fillColor);
+ }
+ }
+
/**
* Manage shp layer display.
*
@@ -377,8 +386,7 @@
ShapeLayer shapeLayer = new ShapeLayer();
Properties p = new Properties();
p.setProperty(layerId + "." + ShapeLayer.shapeFileProperty, mapFile);
- p.setProperty(layerId + "." + DrawingAttributes.linePaintProperty, lineColor);
- p.setProperty(layerId + "." + DrawingAttributes.fillPaintProperty, fillColor);
+ addDrawingAttributes(p, layerId, lineColor, fillColor);
shapeLayer.setProperties(layerId, p);
shapeLayer.setName(layerId);
@@ -404,8 +412,7 @@
E00Layer e00Layer = new E00Layer();
Properties p = new Properties();
p.setProperty(layerId + ".FileName", mapFile);
- p.setProperty(layerId + "." + DrawingAttributes.linePaintProperty, lineColor);
- p.setProperty(layerId + "." + DrawingAttributes.fillPaintProperty, fillColor);
+ addDrawingAttributes(p, layerId, lineColor, fillColor);
e00Layer.setProperties(layerId, p);
e00Layer.setName(layerId);
@@ -436,8 +443,7 @@
DTEDLayer dtedLayer = new DTEDLayer();
Properties p = new Properties();
p.setProperty(layerId + ".dted.paths", parentFolder);
- p.setProperty(layerId + "." + DrawingAttributes.linePaintProperty, lineColor);
- p.setProperty(layerId + "." + DrawingAttributes.fillPaintProperty, fillColor);
+ addDrawingAttributes(p, layerId, lineColor, fillColor);
dtedLayer.setProperties(layerId, p);
dtedLayer.setName(layerId);
@@ -467,8 +473,7 @@
Properties p = new Properties();
p.setProperty(layerId + "." + MIFLayer.MIF_FileProperty, mapFile);
p.setProperty(layerId + "." + MIFLayer.pointVisibleProperty, "true");
- p.setProperty(layerId + "." + DrawingAttributes.linePaintProperty, lineColor);
- p.setProperty(layerId + "." + DrawingAttributes.fillPaintProperty, fillColor);
+ addDrawingAttributes(p, layerId, lineColor, fillColor);
mifLayer.setProperties(layerId, p);
mifLayer.setName(layerId);
@@ -506,8 +511,7 @@
// This property should reflect the paths to the RPF directories
p.setProperty(layerId + ".rpf.paths", parentFolder);
p.setProperty(layerId + "." + MIFLayer.pointVisibleProperty, "true");
- p.setProperty(layerId + "." + DrawingAttributes.linePaintProperty, lineColor);
- p.setProperty(layerId + "." + DrawingAttributes.fillPaintProperty, fillColor);
+ addDrawingAttributes(p, layerId, lineColor, fillColor);
rpfLayer.setProperties(layerId, p);
rpfLayer.setName(layerId);
@@ -543,8 +547,7 @@
Properties p = new Properties();
p.setProperty(layerId + "." + VPFLayer.pathProperty, mapFile);
p.setProperty(layerId + "." + VPFLayer.defaultLayerProperty, "vmapCoastline");
- p.setProperty(layerId + "." + DrawingAttributes.linePaintProperty, lineColor);
- p.setProperty(layerId + "." + DrawingAttributes.fillPaintProperty, fillColor);
+ addDrawingAttributes(p, layerId, lineColor, fillColor);
vpfLayer.setProperties(layerId, p);
vpfLayer.setName(layerId);
@@ -618,6 +621,9 @@
//setScale(SCALE);
addGraticuleLayer();
+ String lineColor = "ff000000";
+ String fillColor = "ffbdde83";
+
// ajout des shapes
boolean shapeLoaded = false;
for (String filename : getFisheryRegion().getMapFilePath()) {
@@ -625,7 +631,7 @@
if (log.isDebugEnabled()) {
log.debug(t("isisfish.message.load.map", filename));
}
- addLayer(filename, filename, "ff000000", "ffbdde83");
+ addLayer(filename, filename, lineColor, null);
shapeLoaded = true;
}
}
@@ -637,7 +643,7 @@
// a pas reussi a charger les fichiers demandés, on charge la
// carte du monde
String filename = IsisFish.config.getDefaultMapFilename();
- addLayer(filename, filename, "ff000000", "ffbdde83");
+ addLayer(filename, filename, lineColor, fillColor);
}
addSpecificLayer();
1
0
r4375 - in trunk/src/main/java/fr/ifremer/isisfish: entities ui/input
by bpoussin@users.forge.codelutin.com 29 Nov '16
by bpoussin@users.forge.codelutin.com 29 Nov '16
29 Nov '16
Author: bpoussin
Date: 2016-11-29 17:22:02 +0100 (Tue, 29 Nov 2016)
New Revision: 4375
Url: http://forge.codelutin.com/projects/isis-fish/repository/revisions/4375
Log:
rewrite UI do use radio button
Modified:
trunk/src/main/java/fr/ifremer/isisfish/entities/RegionImportJson.java
trunk/src/main/java/fr/ifremer/isisfish/ui/input/InputHandler.java
trunk/src/main/java/fr/ifremer/isisfish/ui/input/InputSaveVerifier.java
Modified: trunk/src/main/java/fr/ifremer/isisfish/entities/RegionImportJson.java
===================================================================
--- trunk/src/main/java/fr/ifremer/isisfish/entities/RegionImportJson.java 2016-11-23 13:08:32 UTC (rev 4374)
+++ trunk/src/main/java/fr/ifremer/isisfish/entities/RegionImportJson.java 2016-11-29 16:22:02 UTC (rev 4375)
@@ -10,20 +10,27 @@
import fr.ifremer.isisfish.types.TimeUnit;
import fr.ifremer.isisfish.util.EntitySemanticsDecorator;
import fr.ifremer.isisfish.util.MatrixCSVHelper;
+import java.awt.GridLayout;
import java.io.IOException;
import java.io.Reader;
+import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collection;
+import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
-import java.util.LinkedList;
+import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
+import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
+import javax.swing.JPanel;
+import javax.swing.JRadioButton;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.logging.Log;
@@ -50,327 +57,6 @@
*/
public class RegionImportJson {
- /**
- * Use during import to merge imported data in existing Region
- */
- static public interface RegionMerge {
- static public enum AnswerType {
- NONE, REUSE, IMPORT, ABORT
- };
-
- static public class Answer {
- public AnswerType type;
- public TopiaEntity entity;
-
- public Answer(AnswerType type, TopiaEntity entity) {
- this.type = type;
- this.entity = entity;
- }
-
- public boolean isAbort() {
- return type == AnswerType.ABORT;
- }
- }
-
- /**
- * Get last answer return by choice
- * @return last answer return by previous call to choice
- */
- public Answer getLastAnswer();
-
- /**
- * Ask how to merge entity
- * @param id
- * @param toString
- * @param details
- * @return
- */
- public Answer choice(String id, String toString, Map<String, Object> details);
-
- /**
- * If exist return entity in existing Region with id in parameter
- * @param id
- * @return null or TopiaEntity
- */
- public TopiaEntity getEntity(String id);
- }
-
- static public class RegionMergeDatabase implements RegionMerge {
- static public enum ComplexeAnswerType {
- NONE, NONE_THIS_TYPE, NONE_ALL,
- REUSE, REUSE_THIS_TYPE, REUSE_ALL,
- IMPORT, IMPORT_THIS_TYPE, IMPORT_ALL,
- ABORT
- };
-
- /**
- * Object used to keep recurent answer (abort, import All, reuse All, ...)
- */
- static public class RegionMergeContext {
- protected boolean abort = false;
- protected boolean noneAll = false;
- protected Set<Class> noneType = new HashSet<Class>();
- protected boolean reuseAll = false;
- protected Set<Class> reuseType = new HashSet<Class>();
- protected boolean importAll = false;
- protected Set<Class> importType = new HashSet<Class>();
- protected AnswerType lastAnswer = null;
- protected TopiaEntity reuseEntity;
- protected Class currentType;
-
- public RegionMergeContext() {
- }
-
- public RegionMergeContext(boolean importAll) {
- this.importAll = importAll;
- }
-
- public RegionMerge.Answer getAnswer() {
- RegionMerge.Answer result = null;
- if (lastAnswer != null) {
- result = new RegionMerge.Answer(lastAnswer, reuseEntity);
- }
- return result;
- }
-
- public Class getCurrentType() {
- return currentType;
- }
-
- protected void setLastAnswer(ComplexeAnswerType lastAnswer) {
- this.setLastAnswer(lastAnswer, null);
- }
-
- protected void setLastAnswer(ComplexeAnswerType lastAnswer, TopiaEntity e) {
- this.reuseEntity = e;
- switch (lastAnswer) {
- case NONE:
- this.lastAnswer = AnswerType.NONE;
- break;
- case NONE_THIS_TYPE:
- noneType.add(currentType);
- this.lastAnswer = AnswerType.NONE;
- break;
- case NONE_ALL:
- noneAll = true;
- this.lastAnswer = AnswerType.NONE;
- break;
- case ABORT:
- abort = true;
- this.lastAnswer = AnswerType.ABORT;
- break;
- case IMPORT:
- this.lastAnswer = AnswerType.IMPORT;
- break;
- case IMPORT_THIS_TYPE:
- importType.add(currentType);
- this.lastAnswer = AnswerType.IMPORT;
- break;
- case IMPORT_ALL:
- importAll = true;
- this.lastAnswer = AnswerType.IMPORT;
- break;
- case REUSE:
- this.lastAnswer = AnswerType.REUSE;
- break;
- case REUSE_THIS_TYPE:
- reuseType.add(currentType);
- this.lastAnswer = AnswerType.REUSE;
- break;
- case REUSE_ALL:
- reuseAll = true;
- this.lastAnswer = AnswerType.REUSE;
- break;
- }
- }
-
- public AnswerType initAnswer(Class type) {
- lastAnswer = null;
- currentType = type;
- if (abort) {
- lastAnswer = AnswerType.ABORT;
- } else if (noneAll || noneType.contains(type)) {
- lastAnswer = AnswerType.NONE;
- } else if (importAll || importType.contains(type)) {
- lastAnswer = AnswerType.IMPORT;
- }
- return lastAnswer;
- }
-
- public AnswerType initAnswer(Class type, Set<TopiaEntity> possible) {
- lastAnswer = null;
- currentType = type;
- if (possible != null && possible.size() == 1 && (reuseAll || reuseType.contains(type))) {
- lastAnswer = AnswerType.REUSE;
- this.reuseEntity = possible.iterator().next();
- }
- return lastAnswer;
- }
- }
-
-
- protected TopiaContext tx;
- protected RegionMergeContext context;
- protected Answer lastAnswer;
-
- public RegionMergeDatabase(TopiaContext tx) {
- this.tx = tx;
- }
-
- @Override
- public Answer getLastAnswer() {
- return lastAnswer;
- }
-
- @Override
- public TopiaEntity getEntity(String id) {
- TopiaEntity result = tx.findByTopiaId(id);
- return result;
- }
-
- @Override
- public Answer choice(String id, String toString, Map<String, Object> details) {
- Class type = TopiaId.getClassName(id);
- if (context == null) {
- context = new RegionMergeContext();
- }
-
- if (context.initAnswer(type) == null) {
- TopiaDAO<TopiaEntity> dao = IsisFishDAOHelper.getDAO(tx, type);
-
- LinkedHashSet<TopiaEntity> possible = new LinkedHashSet<TopiaEntity>();
-
- possible.add(dao.findByTopiaId(id));
- if (Equation.class.isAssignableFrom(context.getCurrentType())) {
- if (details.containsKey("name") && details.containsKey("category")) {
- possible.addAll(dao.findAllByProperties("name", details.get("name"), "category", details.get("category")));
- }
- } else {
- if (details.containsKey("name")) {
- possible.addAll(dao.findAllByProperty("name", details.get("name")));
- }
- }
-
- if (context.initAnswer(type, possible) == null) {
- if (Equation.class.isAssignableFrom(context.getCurrentType())) {
- if (details.containsKey("category")) {
- possible.addAll(dao.findAllByProperty("category", details.get("category")));
- }
- } else {
- possible.addAll(dao.findAll());
- }
-
- ask(context, toString, details, possible);
- }
- }
-
- return lastAnswer = context.getAnswer();
- }
-
- protected void ask(RegionMergeContext context, String toString,
- Map<String, Object> details, Collection<TopiaEntity> possible) {
-
- JComboBox select = new JComboBox(possible.toArray());
-
- Object[] options = new Object[] {
- "None", // for null
- "None same type", // for null
- "None all", // for null
- "Reuse selected object",
- "Reuse same type",
- "Reuse all",
- "Import",
- "Import same type",
- "Import All",
- "Abort"
- };
-
-
- int result = JOptionPane.showOptionDialog(null,
- new Object[]{
- String.format("You try to import '%s' (%s)", toString,
- ClassUtils.getShortClassName(context.getCurrentType())),
- "",
- "Possible answer are:",
- "None: don't import nor reuse an object.",
- "None same type: auto response None for this answer and all next answer for same object type.",
- "None all: auto response None for this answer and all next answer for all object type.",
- "Reuse: use object already in current Region (make your choice in next combobox).",
- select,
- "Reuse same type: auto response Reuse for this answer and all next answer if only one object found ('id' or 'name') in current Region for this type.",
- "Reuse all: auto response Reuse for this answer and all next answer if only one object found ('id' or 'name') in current Region for all object.",
- "Import: import object from file.",
- "Import same type: auto response Import for this answer and all next answer for same object type.",
- "Import All: auto response Import for this answer and all next answer for all object.",
- "Abort: Cancel this import (import nothing)."
- },
- "Make a choice",
- JOptionPane.DEFAULT_OPTION,
- JOptionPane.QUESTION_MESSAGE,
- null,
- options,
- options[0]);
-
- TopiaEntity selectEntity = (TopiaEntity)select.getSelectedItem();
- switch (result) {
- case 0:
- context.setLastAnswer(ComplexeAnswerType.NONE);
- break;
- case 1:
- context.setLastAnswer(ComplexeAnswerType.NONE_THIS_TYPE);
- break;
- case 2:
- context.setLastAnswer(ComplexeAnswerType.NONE_ALL);
- break;
- case 3:
- context.setLastAnswer(ComplexeAnswerType.REUSE, selectEntity);
- break;
- case 4:
- context.setLastAnswer(ComplexeAnswerType.REUSE_THIS_TYPE, selectEntity);
- break;
- case 5:
- context.setLastAnswer(ComplexeAnswerType.REUSE_ALL, selectEntity);
- break;
- case 6:
- context.setLastAnswer(ComplexeAnswerType.IMPORT);
- break;
- case 7:
- context.setLastAnswer(ComplexeAnswerType.IMPORT_THIS_TYPE);
- break;
- case 8:
- context.setLastAnswer(ComplexeAnswerType.IMPORT_ALL);
- break;
- case 9:
- context.setLastAnswer(ComplexeAnswerType.ABORT);
- break;
- default:
- ask(context, toString, details, possible);
- break;
- }
- }
- }
-
- static public class RegionMergeImportAll implements RegionMerge {
- protected Answer lastAnswer;
-
- @Override
- public Answer getLastAnswer() {
- return lastAnswer;
- }
-
-
- @Override
- public TopiaEntity getEntity(String id) {
- return null;
- }
-
- @Override
- public Answer choice(String id, String toString, Map<String, Object> details) {
- return lastAnswer = new Answer(AnswerType.IMPORT, null);
- }
-
- }
-
/** to use log facility, just put in your code: log.info(\"...\"); */
static private Log log = LogFactory.getLog(RegionImportJson.class);
@@ -411,7 +97,7 @@
if (!isRegion) {
// il n'y a qu'un objet dans le json, on le deserialize
String rootId = info.get("rootId").asText();
- RegionVisitor v = new RegionVisitor(merge, rootId, entities, jsonEntities);
+ RegionVisitor v = new RegionVisitor(merge, null, null, rootId, entities, jsonEntities);
// no assigment, collect of entities is done during deserialize
v.loadEntity();
} else {
@@ -422,7 +108,7 @@
// les champs commencant par des # sont #info et #entities
if (!field.getKey().startsWith("#")) {
for (JsonNode id : field.getValue()) {
- RegionVisitor v = new RegionVisitor(merge, id.asText(), entities, jsonEntities);
+ RegionVisitor v = new RegionVisitor(merge, null, null, id.asText(), entities, jsonEntities);
// no assigment, collect of entities is done during deserialize
v.loadEntity();
}
@@ -440,13 +126,19 @@
protected RegionMerge merge;
protected JsonNode nodeEntity;
+ protected TopiaEntity parentEntity;
+ protected String propertyName;
protected TopiaEntity currentEntity;
protected LinkedHashMap<String, TopiaEntity> entities;
protected JsonNode jsonEntities;
- public RegionVisitor(RegionMerge merge, String idEntity,
+ public RegionVisitor(RegionMerge merge,
+ TopiaEntity parentEntity, String propertyName,
+ String idEntity,
LinkedHashMap<String, TopiaEntity> entities, JsonNode jsonEntities) {
this.merge = merge;
+ this.parentEntity = parentEntity;
+ this.propertyName = propertyName;
this.nodeEntity = jsonEntities.get(idEntity);
this.entities = entities;
this.jsonEntities = jsonEntities;
@@ -461,20 +153,19 @@
return currentEntity;
}
- protected TopiaEntity convertJsonToEntity(JsonNode node) {
- TopiaEntity result;
+ protected TopiaEntity convertJsonToEntity(JsonNode node, TopiaEntity result) {
String id = node.get("#id").asText();
String className = node.get("#class").asText();
try {
// try to reuse entity
- result = merge.getEntity(id);
if (result == null) {
- // if no entity with same id, create new one
+ // if no entity with same id, create new one, with new Id
Class clazz = Class.forName(className);
result = (TopiaEntity)clazz.newInstance();
- result.setTopiaId(id);
+ result.setTopiaId(TopiaId.create(TopiaId.getClassName(id)));
}
// add in list save after import
+ // id and result.id can be different. id is used to keep link in json
entities.put(id, result);
result.accept(this);
@@ -497,7 +188,22 @@
TopiaEntity result;
String toString = node.get("#toString").asText();
- RegionMerge.Answer answer = merge.choice(id, toString, details);
+
+ EnumSet<RegionMerge.AnswerType> disallow = EnumSet.noneOf(RegionMerge.AnswerType.class);
+
+ // specifique rule for some Entities
+ Class currentEntityClass = TopiaId.getClassName(id);
+ if (Species.class.isAssignableFrom(currentEntityClass) && Population.PROPERTY_SPECIES.equals(propertyName)) {
+ // une population a forcement une espece
+ disallow.add(RegionMerge.AnswerType.NONE);
+ } else if (Cell.class.isAssignableFrom(currentEntityClass)) {
+ // on ne peut pas importer une Cellule d'une autre Region
+ disallow.add(RegionMerge.AnswerType.CREATE);
+ }
+
+ RegionMerge.Answer answer = merge.choice(id, toString, details,
+ disallow.toArray(new RegionMerge.AnswerType[disallow.size()]));
+
switch (answer.type) {
case ABORT:
throw new IsisFishRuntimeException("Import aborted by user");
@@ -504,17 +210,22 @@
case NONE:
result = null;
break;
- case REUSE:
+ case USE:
result = answer.entity;
break;
+ case REPLACE:
+ result = convertJsonToEntity(node, answer.entity);
+ break;
+ case CREATE:
+ result = convertJsonToEntity(node, null);
+ break;
default:
- result = convertJsonToEntity(node);
- break;
+ throw new IsisFishRuntimeException("Unsupported action");
}
return result;
}
- protected Object readValue(Class<?> type, JsonNode value) {
+ protected Object readValue(final String propertyName, Class<?> type, JsonNode value) {
Object result;
try {
if (value == null || value.isNull() || value.isMissingNode()) {
@@ -521,7 +232,7 @@
result = null;
} else if (TopiaEntity.class.isAssignableFrom(type)) {
String id = value.asText();
- RegionVisitor child = new RegionVisitor(merge, id, entities, jsonEntities);
+ RegionVisitor child = new RegionVisitor(merge, currentEntity, propertyName, id, entities, jsonEntities);
result = child.loadEntity();
} else if (MatrixND.class.isAssignableFrom(type)) {
String mat = value.asText();
@@ -530,7 +241,7 @@
new EntitySemanticsDecorator.EntityProvider() {
@Override
public Object findById(String id) {
- RegionVisitor child = new RegionVisitor(merge, id, entities, jsonEntities);
+ RegionVisitor child = new RegionVisitor(merge, currentEntity, propertyName, id, entities, jsonEntities);
Object result = child.loadEntity();
return result;
}
@@ -579,7 +290,7 @@
public void visit(TopiaEntity entity, String propertyName, Class<?> type, Object value) {
JsonNode jsonValue = nodeEntity.get(propertyName);
try {
- value = readValue(type, jsonValue);
+ value = readValue(propertyName, type, jsonValue);
BeanUtils.setProperty(entity, propertyName, value);
} catch (Exception eee) {
throw new IsisFishRuntimeException(String.format("Can't set property '%s' from json value: '%s'", propertyName, jsonValue), eee);
@@ -590,15 +301,15 @@
public void visit(TopiaEntity entity, String propertyName, Class<?> collectionType, Class<?> type, Object value) {
JsonNode jsonValue = nodeEntity.get(propertyName);
try {
- if (value != null) {
+ if (jsonValue != null) {
Collection c;
if (Set.class.isAssignableFrom(collectionType)) {
c = new HashSet();
} else {
- c = new LinkedList();
+ c = new ArrayList();
}
for (JsonNode currentValue : jsonValue) {
- Object v = readValue(type, currentValue);
+ Object v = readValue(propertyName, type, currentValue);
if (v != null) {
c.add(v);
}
@@ -619,4 +330,344 @@
public void clear() {
}
}
+
+ /**
+ * Use during import to merge imported data in existing Region
+ */
+ static public interface RegionMerge {
+ static public enum AutoAnswerType {
+ ONCE("Reply only for this awnser"),
+ TYPE("Reply all time same thing for this type"),
+ ALL("Reply all time same thing for all type");
+
+ private final String toString;
+ private AutoAnswerType(String toString) {
+ this.toString = toString;
+ }
+
+ @Override
+ public String toString() {
+ return toString;
+ }
+
+ }
+
+ static public enum AnswerType {
+ NONE("None", "don't import nor reuse an object."),
+ USE("Use", "use (don't import) object already in current Region (make your choice in next combobox)."),
+ REPLACE("Replace", "replace object in current Region with imported object from file (make your choice in next combobox"),
+ CREATE("Create", "create new object from file."),
+ ABORT("Abort", "cancel this import (import nothing).");
+
+ private final String toString;
+ private final String description;
+ AnswerType(String toString, String description) {
+ this.toString = toString;
+ this.description = description;
+ }
+
+ public String getDescription() {
+ return description;
+ }
+
+ @Override
+ public String toString() {
+ return toString;
+ }
+ };
+
+ static public class Answer {
+ public AnswerType type;
+ public TopiaEntity entity;
+
+ public Answer(AnswerType type, TopiaEntity entity) {
+ this.type = type;
+ this.entity = entity;
+ }
+ }
+
+ /**
+ * Ask how to merge entity
+ * @param id
+ * @param toString
+ * @param details
+ * @param disallow
+ * @return
+ */
+ public Answer choice(String id, String toString, Map<String, Object> details, AnswerType ... disallow);
+
+ public boolean isAbort();
+ }
+
+ static public class RegionMergeDatabase implements RegionMerge {
+// static public enum ComplexeAnswerType {
+// NONE("None", "don't import nor reuse an object."),
+// NONE_THIS_TYPE("None same type", "auto reply None for this answer and all next answer for same object type."),
+// NONE_ALL("None all", "auto reply None for this answer and all next answer for all object type."),
+//
+// USE("Use", "use (don't import) object already in current Region (make your choice in next combobox)."),
+// USE_THIS_TYPE("Use same type", "auto reply Use for this answer and all next answer if only one object found ('id' or 'name') in current Region for this type."),
+// USE_ALL("Use all", "auto reply Use for this answer and all next answer if only one object found ('id' or 'name') in current Region for all object."),
+//
+// REPLACE("Replace", "replace object in current Region with imported object from file (make your choice in next combobox"),
+// REPLACE_THIS_TYPE("Replace same type", "auto reply Replace for this answer and all next answer if only one object found ('id' or 'name') in current Region for this type."),
+// REPLACE_ALL("Replace all", "auto reply Replace for this answer and all next answer if only one object found ('id' or 'name') in current Region for all object."),
+//
+// CREATE("Create", "create new object from file."),
+// CREATE_THIS_TYPE("Create same type", "auto reply Create for this answer and all next answer for same object type."),
+// CREATE_ALL("Create All", "auto reply Create for this answer and all next answer for all object."),
+//
+// ABORT("Abort", "cancel this import (import nothing).");
+//
+// private final String toString;
+// private final String description;
+// ComplexeAnswerType(String toString, String description) {
+// this.toString = toString;
+// this.description = description;
+// }
+//
+// public String getDescription() {
+// return description;
+// }
+//
+// @Override
+// public String toString() {
+// return toString;
+// }
+// }
+
+ /**
+ * Object used to keep recurent answer (abort, import All, reuse All, ...)
+ */
+ static public class RegionMergeContext {
+ protected boolean abort = false;
+
+ protected EnumSet<AnswerType> replyAll = EnumSet.noneOf(AnswerType.class);
+ protected HashMap<AnswerType, Set<Class>> replyType = new HashMap<AnswerType, Set<Class>>() {
+ @Override
+ public Set<Class> get(Object key) {
+ Set<Class> result = super.get(key);
+ if (result == null) {
+ result = new HashSet<Class>();
+ put((AnswerType)key, result);
+ }
+ return result;
+ }
+ };
+// protected boolean noneAll = false;
+// protected Set<Class> noneType = new HashSet<Class>();
+// protected boolean useAll = false;
+// protected Set<Class> useType = new HashSet<Class>();
+// protected boolean replaceAll = false;
+// protected Set<Class> replaceType = new HashSet<Class>();
+// protected boolean createAll = false;
+// protected Set<Class> createType = new HashSet<Class>();
+
+ protected AnswerType lastAnswerType = null;
+ protected TopiaEntity reuseEntity;
+ protected Class currentType;
+
+ public RegionMergeContext() {
+ }
+
+ public RegionMergeContext(boolean importAll) {
+ this.replyAll.add(AnswerType.CREATE);
+ }
+
+ public RegionMerge.Answer getAnswer() {
+ RegionMerge.Answer result = null;
+ if (lastAnswerType != null) {
+ result = new RegionMerge.Answer(lastAnswerType, reuseEntity);
+ }
+ return result;
+ }
+
+ public Class getCurrentType() {
+ return currentType;
+ }
+
+ protected void setLastAnswer(AnswerType lastAnswer, AutoAnswerType autoType) {
+ this.setLastAnswer(lastAnswer, autoType, null);
+ }
+
+ protected void setLastAnswer(AnswerType lastAnswer, AutoAnswerType autoType, TopiaEntity e) {
+ this.reuseEntity = e;
+ this.lastAnswerType = lastAnswer;
+
+ if (AnswerType.ABORT == lastAnswer) {
+ abort = true;
+ }
+
+ switch (autoType) {
+ case TYPE:
+ Set<Class> set = replyType.get(lastAnswer);
+ set.add(currentType);
+ break;
+ case ALL:
+ replyAll.add(lastAnswer);
+ break;
+ }
+ }
+
+ public AnswerType initAnswer(Class type) {
+ lastAnswerType = null;
+ currentType = type;
+ if (abort) {
+ lastAnswerType = AnswerType.ABORT;
+ } else if (replyAll.contains(AnswerType.NONE) || replyType.get(AnswerType.NONE).contains(type)) {
+ lastAnswerType = AnswerType.NONE;
+ } else if (replyAll.contains(AnswerType.CREATE) || replyType.get(AnswerType.CREATE).contains(type)) {
+ lastAnswerType = AnswerType.CREATE;
+ }
+ return lastAnswerType;
+ }
+
+ public AnswerType initAnswer(Class type, Set<TopiaEntity> possible) {
+ lastAnswerType = null;
+ currentType = type;
+ if (possible != null && possible.size() == 1 &&
+ (replyAll.contains(AnswerType.USE) || replyType.get(AnswerType.USE).contains(type))) {
+ lastAnswerType = AnswerType.USE;
+ this.reuseEntity = possible.iterator().next();
+ } else if (possible != null && possible.size() == 1 &&
+ (replyAll.contains(AnswerType.REPLACE) || replyType.get(AnswerType.REPLACE).contains(type))) {
+ lastAnswerType = AnswerType.REPLACE;
+ this.reuseEntity = possible.iterator().next();
+ }
+ return lastAnswerType;
+ }
+ }
+
+
+ protected TopiaContext tx;
+ protected RegionMergeContext context;
+ protected Answer lastAnswer;
+
+ public RegionMergeDatabase(TopiaContext tx) {
+ this.tx = tx;
+ }
+
+ protected boolean isAcceptableAnswer(AnswerType type, AnswerType ... disallow) {
+ boolean result = type != null && !Arrays.asList(disallow).contains(type);
+ return result;
+ }
+
+ @Override
+ public Answer choice(String id, String toString, Map<String, Object> details, AnswerType ... disallow) {
+ Class type = TopiaId.getClassName(id);
+ if (context == null) {
+ context = new RegionMergeContext();
+ }
+
+ if (!isAcceptableAnswer(context.initAnswer(type), disallow)) {
+ TopiaDAO<TopiaEntity> dao = IsisFishDAOHelper.getDAO(tx, type);
+
+ LinkedHashSet<TopiaEntity> possible = new LinkedHashSet<TopiaEntity>();
+
+ possible.add(dao.findByTopiaId(id));
+ if (Equation.class.isAssignableFrom(context.getCurrentType())) {
+ if (details.containsKey("name") && details.containsKey("category")) {
+ possible.addAll(dao.findAllByProperties("name", details.get("name"), "category", details.get("category")));
+ }
+ } else {
+ if (details.containsKey("name")) {
+ possible.addAll(dao.findAllByProperty("name", details.get("name")));
+ }
+ }
+
+ if (!isAcceptableAnswer(context.initAnswer(type, possible), disallow)) {
+ if (Equation.class.isAssignableFrom(context.getCurrentType())) {
+ if (details.containsKey("category")) {
+ possible.addAll(dao.findAllByProperty("category", details.get("category")));
+ }
+ } else {
+ possible.addAll(dao.findAll());
+ }
+
+ ask(context, toString, details, possible, disallow);
+ }
+ }
+
+ return lastAnswer = context.getAnswer();
+ }
+
+ protected AnswerType ask(RegionMergeContext context, String toString,
+ Map<String, Object> details, Collection<TopiaEntity> possible,
+ AnswerType ... disallow) {
+
+ JPanel radioPanel = new JPanel(new GridLayout(1, 0));
+ ButtonGroup group = new ButtonGroup();
+ for (AutoAnswerType auto : AutoAnswerType.values()) {
+ JRadioButton radio = new JRadioButton(auto.toString);
+ radio.setSelected(AutoAnswerType.ONCE == auto);
+ radio.setActionCommand(auto.name());
+ group.add(radio);
+ radioPanel.add(radio);
+ }
+
+ JComboBox select = new JComboBox(possible.toArray());
+
+ ArrayList<AnswerType> allowedOptions =
+ new ArrayList<AnswerType>(Arrays.asList(AnswerType.values()));
+ for (AnswerType t : disallow) {
+ allowedOptions.remove(t);
+ }
+ Object[] options = allowedOptions.toArray();
+
+ List descriptions = new ArrayList();
+ descriptions.add(String.format("You try to import '%s' (%s)", toString,
+ ClassUtils.getShortClassName(context.getCurrentType())));
+ descriptions.add("Possible answer are:");
+ for (AnswerType t : allowedOptions) {
+ descriptions.add(t.toString() + ": " + t.getDescription());
+ }
+ descriptions.add(select);
+ // answer only for this answer
+ // make same answer for ask about same type
+ // make same answer all time
+ descriptions.add(radioPanel);
+
+ int result = JOptionPane.showOptionDialog(null,
+ descriptions.toArray(),
+ "Make a choice",
+ JOptionPane.DEFAULT_OPTION,
+ JOptionPane.QUESTION_MESSAGE,
+ null,
+ options,
+ options[0]);
+
+ TopiaEntity selectEntity = (TopiaEntity)select.getSelectedItem();
+ if (result >= 0) {
+ AnswerType reply = allowedOptions.get(result);
+ AutoAnswerType auto = AutoAnswerType.valueOf(group.getSelection().getActionCommand());
+ context.setLastAnswer(reply, auto, selectEntity);
+ } else {
+ ask(context, toString, details, possible);
+ }
+ return context.lastAnswerType;
+ }
+
+ @Override
+ public boolean isAbort() {
+ boolean result = lastAnswer != null && lastAnswer.type == AnswerType.ABORT;
+ return result;
+ }
+
+ }
+
+ static public class RegionMergeImportAll implements RegionMerge {
+ protected Answer lastAnswer;
+
+ @Override
+ public boolean isAbort() {
+ return false;
+ }
+
+ @Override
+ public Answer choice(String id, String toString, Map<String, Object> details, AnswerType ... disallow) {
+ return lastAnswer = new Answer(AnswerType.CREATE, null);
+ }
+
+ }
+
}
Modified: trunk/src/main/java/fr/ifremer/isisfish/ui/input/InputHandler.java
===================================================================
--- trunk/src/main/java/fr/ifremer/isisfish/ui/input/InputHandler.java 2016-11-23 13:08:32 UTC (rev 4374)
+++ trunk/src/main/java/fr/ifremer/isisfish/ui/input/InputHandler.java 2016-11-29 16:22:02 UTC (rev 4375)
@@ -433,7 +433,8 @@
try {
File file = FileUtil.getFile(".*\\.isonz$",
t("isisfish.message.import.region.zipped"));
- if (file != null) {
+ InputSaveVerifier inputSaveVerifier = inputUI.getContextValue(InputSaveVerifier.class);
+ if (file != null && inputSaveVerifier.checkEdit() != JOptionPane.CANCEL_OPTION) {
FisheryRegion fisheryRegion = inputUI.getContextValue(FisheryRegion.class);
TopiaContext tx = fisheryRegion.getTopiaContext();
RegionImportJson.RegionMergeDatabase merge =
@@ -448,12 +449,17 @@
tx.add(e);
}
tx.commitTransaction();
+ log.info("Import merged");
} catch (Exception eee) {
tx.rollbackTransaction();
- if (!merge.getLastAnswer().isAbort()) {
+ if (!merge.isAbort()) {
throw eee;
+ } else {
+ log.info("Import aborted");
}
}
+ inputSaveVerifier.cancel();
+ loadRegion(fisheryRegion.getName());
}
} catch (Exception eee) {
throw new IsisFishRuntimeException("Can't import region", eee);
Modified: trunk/src/main/java/fr/ifremer/isisfish/ui/input/InputSaveVerifier.java
===================================================================
--- trunk/src/main/java/fr/ifremer/isisfish/ui/input/InputSaveVerifier.java 2016-11-23 13:08:32 UTC (rev 4374)
+++ trunk/src/main/java/fr/ifremer/isisfish/ui/input/InputSaveVerifier.java 2016-11-29 16:22:02 UTC (rev 4375)
@@ -221,6 +221,9 @@
String typeSimpleName = type.getSimpleName();
String name = typeSimpleName + "_new";
+ // FIXME 20161110 poussin don't use introspection but
+ // IsisFishDAOHelper.getDAO(context, type);
+
// use introspection to call IsisFishDAOHelper.getXXXDAO()
TopiaContext topiaContext = inputContentUI.getTopiaContext();
Method method = MethodUtils.getAccessibleMethod(IsisFishDAOHelper.class,
1
0
r4374 - trunk/src/main/java/fr/ifremer/isisfish/datastore
by echatellier@users.forge.codelutin.com 23 Nov '16
by echatellier@users.forge.codelutin.com 23 Nov '16
23 Nov '16
Author: echatellier
Date: 2016-11-23 14:08:32 +0100 (Wed, 23 Nov 2016)
New Revision: 4374
Url: http://forge.codelutin.com/projects/isis-fish/repository/revisions/4374
Log:
fixes #8802: Correction dans le calcul des r?\195?\169sultats n?\195?\169c?\195?\169ssaires.
Modified:
trunk/src/main/java/fr/ifremer/isisfish/datastore/ResultStorageAbstract.java
Modified: trunk/src/main/java/fr/ifremer/isisfish/datastore/ResultStorageAbstract.java
===================================================================
--- trunk/src/main/java/fr/ifremer/isisfish/datastore/ResultStorageAbstract.java 2016-11-10 13:08:37 UTC (rev 4373)
+++ trunk/src/main/java/fr/ifremer/isisfish/datastore/ResultStorageAbstract.java 2016-11-23 13:08:32 UTC (rev 4374)
@@ -307,7 +307,7 @@
ExportStorage storage = ExportStorage.getExport(exportName);
try {
ExportInfo export = storage.getNewInstance();
- Collections.addAll(enabledResult, export.getNecessaryResult());
+ Collections.addAll(requestedResult, export.getNecessaryResult());
} catch (IsisFishException eee) {
if (log.isWarnEnabled()) {
log.warn(t("isisfish.error.instanciate.export",
1
0
r4373 - in trunk/src/main/java/fr/ifremer/isisfish: entities ui/input
by bpoussin@users.forge.codelutin.com 10 Nov '16
by bpoussin@users.forge.codelutin.com 10 Nov '16
10 Nov '16
Author: bpoussin
Date: 2016-11-10 14:08:37 +0100 (Thu, 10 Nov 2016)
New Revision: 4373
Url: http://forge.codelutin.com/projects/isis-fish/repository/revisions/4373
Log:
enhance evolution #8737: Export / Import des ?\195?\169l?\195?\169ments d'une r?\195?\169gion, soit pour import dans une autre r?\195?\169gion, soit pour fusion de deux r?\195?\169gions.
Modified:
trunk/src/main/java/fr/ifremer/isisfish/entities/RegionImportJson.java
trunk/src/main/java/fr/ifremer/isisfish/ui/input/InputHandler.java
Modified: trunk/src/main/java/fr/ifremer/isisfish/entities/RegionImportJson.java
===================================================================
--- trunk/src/main/java/fr/ifremer/isisfish/entities/RegionImportJson.java 2016-11-09 18:44:50 UTC (rev 4372)
+++ trunk/src/main/java/fr/ifremer/isisfish/entities/RegionImportJson.java 2016-11-10 13:08:37 UTC (rev 4373)
@@ -50,21 +50,71 @@
*/
public class RegionImportJson {
+ /**
+ * Use during import to merge imported data in existing Region
+ */
static public interface RegionMerge {
- static public enum AnwserType {
- NONE,
- REUSE, REUSE_ONE_TYPE, REUSE_ONE,
- IMPORT, IMPORT_ALL_TYPE, IMPORT_ALL,
+ static public enum AnswerType {
+ NONE, REUSE, IMPORT, ABORT
+ };
+
+ static public class Answer {
+ public AnswerType type;
+ public TopiaEntity entity;
+
+ public Answer(AnswerType type, TopiaEntity entity) {
+ this.type = type;
+ this.entity = entity;
+ }
+
+ public boolean isAbort() {
+ return type == AnswerType.ABORT;
+ }
+ }
+
+ /**
+ * Get last answer return by choice
+ * @return last answer return by previous call to choice
+ */
+ public Answer getLastAnswer();
+
+ /**
+ * Ask how to merge entity
+ * @param id
+ * @param toString
+ * @param details
+ * @return
+ */
+ public Answer choice(String id, String toString, Map<String, Object> details);
+
+ /**
+ * If exist return entity in existing Region with id in parameter
+ * @param id
+ * @return null or TopiaEntity
+ */
+ public TopiaEntity getEntity(String id);
+ }
+
+ static public class RegionMergeDatabase implements RegionMerge {
+ static public enum ComplexeAnswerType {
+ NONE, NONE_THIS_TYPE, NONE_ALL,
+ REUSE, REUSE_THIS_TYPE, REUSE_ALL,
+ IMPORT, IMPORT_THIS_TYPE, IMPORT_ALL,
ABORT
};
+ /**
+ * Object used to keep recurent answer (abort, import All, reuse All, ...)
+ */
static public class RegionMergeContext {
protected boolean abort = false;
- protected boolean reuseOne = false;
- protected Set<Class> reuseOneType = new HashSet<Class>();
+ protected boolean noneAll = false;
+ protected Set<Class> noneType = new HashSet<Class>();
+ protected boolean reuseAll = false;
+ protected Set<Class> reuseType = new HashSet<Class>();
protected boolean importAll = false;
- protected Set<Class> importAllType = new HashSet<Class>();
- protected AnwserType lastAnswer = null;
+ protected Set<Class> importType = new HashSet<Class>();
+ protected AnswerType lastAnswer = null;
protected TopiaEntity reuseEntity;
protected Class currentType;
@@ -75,66 +125,83 @@
this.importAll = importAll;
}
+ public RegionMerge.Answer getAnswer() {
+ RegionMerge.Answer result = null;
+ if (lastAnswer != null) {
+ result = new RegionMerge.Answer(lastAnswer, reuseEntity);
+ }
+ return result;
+ }
+
public Class getCurrentType() {
return currentType;
}
- public TopiaEntity getReuseEntity() {
- return reuseEntity;
- }
-
- public AnwserType getLastAnswer() {
- return lastAnswer;
- }
-
- protected void setLastAnswer(AnwserType lastAnswer) {
+ protected void setLastAnswer(ComplexeAnswerType lastAnswer) {
this.setLastAnswer(lastAnswer, null);
}
- protected void setLastAnswer(AnwserType lastAnswer, TopiaEntity e) {
- this.lastAnswer = lastAnswer;
+ protected void setLastAnswer(ComplexeAnswerType lastAnswer, TopiaEntity e) {
this.reuseEntity = e;
switch (lastAnswer) {
+ case NONE:
+ this.lastAnswer = AnswerType.NONE;
+ break;
+ case NONE_THIS_TYPE:
+ noneType.add(currentType);
+ this.lastAnswer = AnswerType.NONE;
+ break;
+ case NONE_ALL:
+ noneAll = true;
+ this.lastAnswer = AnswerType.NONE;
+ break;
case ABORT:
abort = true;
+ this.lastAnswer = AnswerType.ABORT;
break;
- case IMPORT_ALL_TYPE:
- importAllType.add(currentType);
- this.lastAnswer = AnwserType.IMPORT;
+ case IMPORT:
+ this.lastAnswer = AnswerType.IMPORT;
break;
+ case IMPORT_THIS_TYPE:
+ importType.add(currentType);
+ this.lastAnswer = AnswerType.IMPORT;
+ break;
case IMPORT_ALL:
importAll = true;
- this.lastAnswer = AnwserType.IMPORT;
+ this.lastAnswer = AnswerType.IMPORT;
break;
- case REUSE_ONE_TYPE:
- reuseOneType.add(currentType);
- this.lastAnswer = AnwserType.REUSE;
+ case REUSE:
+ this.lastAnswer = AnswerType.REUSE;
break;
- case REUSE_ONE:
- reuseOne = true;
- this.lastAnswer = AnwserType.REUSE;
+ case REUSE_THIS_TYPE:
+ reuseType.add(currentType);
+ this.lastAnswer = AnswerType.REUSE;
break;
- default:
+ case REUSE_ALL:
+ reuseAll = true;
+ this.lastAnswer = AnswerType.REUSE;
break;
}
}
- public AnwserType initAnswer(Class type) {
+ public AnswerType initAnswer(Class type) {
lastAnswer = null;
currentType = type;
if (abort) {
- lastAnswer = AnwserType.ABORT;
- } else if (importAll || importAllType.contains(type)) {
- lastAnswer = AnwserType.IMPORT;
+ lastAnswer = AnswerType.ABORT;
+ } else if (noneAll || noneType.contains(type)) {
+ lastAnswer = AnswerType.NONE;
+ } else if (importAll || importType.contains(type)) {
+ lastAnswer = AnswerType.IMPORT;
}
return lastAnswer;
}
- public AnwserType initAnswer(Class type, Set<TopiaEntity> possible) {
+ public AnswerType initAnswer(Class type, Set<TopiaEntity> possible) {
lastAnswer = null;
currentType = type;
- if (possible != null && possible.size() == 1 && (reuseOne || reuseOneType.contains(type))) {
- lastAnswer = AnwserType.REUSE;
+ if (possible != null && possible.size() == 1 && (reuseAll || reuseType.contains(type))) {
+ lastAnswer = AnswerType.REUSE;
this.reuseEntity = possible.iterator().next();
}
return lastAnswer;
@@ -141,25 +208,10 @@
}
}
- /**
- * Ask how to merge entity
- * @param context
- * @param id
- * @param toString
- * @param details
- * @return
- */
- public RegionMergeContext choice(RegionMergeContext context, String id, String toString, Map<String, Object> details);
- /**
- * If exist return entity with id in parameter
- * @param id
- * @return null or TopiaEntity
- */
- public TopiaEntity getEntity(String id);
- }
- static public class RegionMergeDatabase implements RegionMerge {
protected TopiaContext tx;
+ protected RegionMergeContext context;
+ protected Answer lastAnswer;
public RegionMergeDatabase(TopiaContext tx) {
this.tx = tx;
@@ -166,6 +218,11 @@
}
@Override
+ public Answer getLastAnswer() {
+ return lastAnswer;
+ }
+
+ @Override
public TopiaEntity getEntity(String id) {
TopiaEntity result = tx.findByTopiaId(id);
return result;
@@ -172,65 +229,58 @@
}
@Override
- public RegionMergeContext choice(RegionMergeContext context, String id, String toString, Map<String, Object> details) {
+ public Answer choice(String id, String toString, Map<String, Object> details) {
Class type = TopiaId.getClassName(id);
if (context == null) {
context = new RegionMergeContext();
- } else {
- context.initAnswer(type);
}
- if (context.lastAnswer != null) {
- return context;
- }
+ if (context.initAnswer(type) == null) {
+ TopiaDAO<TopiaEntity> dao = IsisFishDAOHelper.getDAO(tx, type);
- TopiaDAO<TopiaEntity> dao = IsisFishDAOHelper.getDAO(tx, type);
+ LinkedHashSet<TopiaEntity> possible = new LinkedHashSet<TopiaEntity>();
- LinkedHashSet<TopiaEntity> possible = new LinkedHashSet<TopiaEntity>();
-
- possible.add(dao.findByTopiaId(id));
- if (Equation.class.isAssignableFrom(context.getCurrentType())) {
- if (details.containsKey("name") && details.containsKey("category")) {
- possible.addAll(dao.findAllByProperties("name", details.get("name"), "category", details.get("category")));
+ possible.add(dao.findByTopiaId(id));
+ if (Equation.class.isAssignableFrom(context.getCurrentType())) {
+ if (details.containsKey("name") && details.containsKey("category")) {
+ possible.addAll(dao.findAllByProperties("name", details.get("name"), "category", details.get("category")));
+ }
+ } else {
+ if (details.containsKey("name")) {
+ possible.addAll(dao.findAllByProperty("name", details.get("name")));
+ }
}
- } else {
- if (details.containsKey("name")) {
- possible.addAll(dao.findAllByProperty("name", details.get("name")));
- }
- }
- context.initAnswer(type, possible);
-
- if (context.lastAnswer != null) {
- return context;
- }
+ if (context.initAnswer(type, possible) == null) {
+ if (Equation.class.isAssignableFrom(context.getCurrentType())) {
+ if (details.containsKey("category")) {
+ possible.addAll(dao.findAllByProperty("category", details.get("category")));
+ }
+ } else {
+ possible.addAll(dao.findAll());
+ }
- if (Equation.class.isAssignableFrom(context.getCurrentType())) {
- if (details.containsKey("category")) {
- possible.addAll(dao.findAllByProperty("category", details.get("category")));
+ ask(context, toString, details, possible);
}
- } else {
- possible.addAll(dao.findAll());
}
- ask(context, toString, details, possible);
-
- return context;
+ return lastAnswer = context.getAnswer();
}
protected void ask(RegionMergeContext context, String toString,
Map<String, Object> details, Collection<TopiaEntity> possible) {
- System.out.println("Choice between:" + toString + " and " + possible);
JComboBox select = new JComboBox(possible.toArray());
Object[] options = new Object[] {
"None", // for null
+ "None same type", // for null
+ "None all", // for null
"Reuse selected object",
- "Reuse if only one (same type)",
- "Reuse if only one (all type)",
+ "Reuse same type",
+ "Reuse all",
"Import",
- "Import All this type",
+ "Import same type",
"Import All",
"Abort"
};
@@ -242,15 +292,17 @@
ClassUtils.getShortClassName(context.getCurrentType())),
"",
"Possible answer are:",
- "None: don't import nor reuse object",
+ "None: don't import nor reuse an object.",
+ "None same type: auto response None for this answer and all next answer for same object type.",
+ "None all: auto response None for this answer and all next answer for all object type.",
"Reuse: use object already in current Region (make your choice in next combobox).",
select,
- "Reuse if only one (same type): Answer Reuse and for next time use object found in current Region if only one match 'id' or 'name' for this type",
- "Reuse if only one (all type): Answer Reuse and for next time use object found in current Region if only one match 'id' or 'name' for all object",
+ "Reuse same type: auto response Reuse for this answer and all next answer if only one object found ('id' or 'name') in current Region for this type.",
+ "Reuse all: auto response Reuse for this answer and all next answer if only one object found ('id' or 'name') in current Region for all object.",
"Import: import object from file.",
- "Import All this type: auto answer Import for this question and all next question for same object type",
- "Import All: auto answer Import for this question and all next question.",
- "Abort: Cancel this import (import nothing)"
+ "Import same type: auto response Import for this answer and all next answer for same object type.",
+ "Import All: auto response Import for this answer and all next answer for all object.",
+ "Abort: Cancel this import (import nothing)."
},
"Make a choice",
JOptionPane.DEFAULT_OPTION,
@@ -262,29 +314,35 @@
TopiaEntity selectEntity = (TopiaEntity)select.getSelectedItem();
switch (result) {
case 0:
- context.setLastAnswer(AnwserType.NONE);
+ context.setLastAnswer(ComplexeAnswerType.NONE);
break;
case 1:
- context.setLastAnswer(AnwserType.REUSE, selectEntity);
+ context.setLastAnswer(ComplexeAnswerType.NONE_THIS_TYPE);
break;
case 2:
- context.setLastAnswer(AnwserType.REUSE_ONE_TYPE, selectEntity);
+ context.setLastAnswer(ComplexeAnswerType.NONE_ALL);
break;
case 3:
- context.setLastAnswer(AnwserType.REUSE_ONE, selectEntity);
+ context.setLastAnswer(ComplexeAnswerType.REUSE, selectEntity);
break;
case 4:
- context.setLastAnswer(AnwserType.IMPORT);
+ context.setLastAnswer(ComplexeAnswerType.REUSE_THIS_TYPE, selectEntity);
break;
case 5:
- context.setLastAnswer(AnwserType.IMPORT_ALL_TYPE);
+ context.setLastAnswer(ComplexeAnswerType.REUSE_ALL, selectEntity);
break;
case 6:
- context.setLastAnswer(AnwserType.IMPORT_ALL);
+ context.setLastAnswer(ComplexeAnswerType.IMPORT);
break;
case 7:
- context.setLastAnswer(AnwserType.ABORT);
+ context.setLastAnswer(ComplexeAnswerType.IMPORT_THIS_TYPE);
break;
+ case 8:
+ context.setLastAnswer(ComplexeAnswerType.IMPORT_ALL);
+ break;
+ case 9:
+ context.setLastAnswer(ComplexeAnswerType.ABORT);
+ break;
default:
ask(context, toString, details, possible);
break;
@@ -293,18 +351,22 @@
}
static public class RegionMergeImportAll implements RegionMerge {
+ protected Answer lastAnswer;
+
@Override
+ public Answer getLastAnswer() {
+ return lastAnswer;
+ }
+
+
+ @Override
public TopiaEntity getEntity(String id) {
return null;
}
@Override
- public RegionMergeContext choice(RegionMergeContext context, String id, String toString, Map<String, Object> details) {
- if (context == null) {
- context = new RegionMergeContext();
- }
- context.setLastAnswer(AnwserType.IMPORT);
- return context;
+ public Answer choice(String id, String toString, Map<String, Object> details) {
+ return lastAnswer = new Answer(AnswerType.IMPORT, null);
}
}
@@ -313,7 +375,6 @@
static private Log log = LogFactory.getLog(RegionImportJson.class);
protected RegionMerge merge;
- protected RegionMerge.RegionMergeContext mergeContext;
protected JsonNode json;
protected LinkedHashMap<String, TopiaEntity> entities;
protected JsonNode jsonEntities;
@@ -328,7 +389,6 @@
public RegionImportJson(Reader r, RegionMerge merge) {
try {
this.merge = merge != null ? merge : new RegionMergeImportAll();
- this.mergeContext = new RegionMerge.RegionMergeContext();
entities = new LinkedHashMap<String, TopiaEntity>();
@@ -351,7 +411,7 @@
if (!isRegion) {
// il n'y a qu'un objet dans le json, on le deserialize
String rootId = info.get("rootId").asText();
- RegionVisitor v = new RegionVisitor(merge, mergeContext, rootId, entities, jsonEntities);
+ RegionVisitor v = new RegionVisitor(merge, rootId, entities, jsonEntities);
// no assigment, collect of entities is done during deserialize
v.loadEntity();
} else {
@@ -362,7 +422,7 @@
// les champs commencant par des # sont #info et #entities
if (!field.getKey().startsWith("#")) {
for (JsonNode id : field.getValue()) {
- RegionVisitor v = new RegionVisitor(merge, mergeContext, id.asText(), entities, jsonEntities);
+ RegionVisitor v = new RegionVisitor(merge, id.asText(), entities, jsonEntities);
// no assigment, collect of entities is done during deserialize
v.loadEntity();
}
@@ -379,17 +439,14 @@
static protected class RegionVisitor implements EntityVisitor {
protected RegionMerge merge;
- protected RegionMerge.RegionMergeContext mergeContext;
protected JsonNode nodeEntity;
protected TopiaEntity currentEntity;
protected LinkedHashMap<String, TopiaEntity> entities;
protected JsonNode jsonEntities;
- public RegionVisitor(RegionMerge merge, RegionMerge.RegionMergeContext mergeContext,
- String idEntity,
+ public RegionVisitor(RegionMerge merge, String idEntity,
LinkedHashMap<String, TopiaEntity> entities, JsonNode jsonEntities) {
this.merge = merge;
- this.mergeContext = mergeContext;
this.nodeEntity = jsonEntities.get(idEntity);
this.entities = entities;
this.jsonEntities = jsonEntities;
@@ -440,8 +497,8 @@
TopiaEntity result;
String toString = node.get("#toString").asText();
- merge.choice(mergeContext, id, toString, details);
- switch (mergeContext.lastAnswer) {
+ RegionMerge.Answer answer = merge.choice(id, toString, details);
+ switch (answer.type) {
case ABORT:
throw new IsisFishRuntimeException("Import aborted by user");
case NONE:
@@ -448,7 +505,7 @@
result = null;
break;
case REUSE:
- result = mergeContext.getReuseEntity();
+ result = answer.entity;
break;
default:
result = convertJsonToEntity(node);
@@ -464,7 +521,7 @@
result = null;
} else if (TopiaEntity.class.isAssignableFrom(type)) {
String id = value.asText();
- RegionVisitor child = new RegionVisitor(merge, mergeContext, id, entities, jsonEntities);
+ RegionVisitor child = new RegionVisitor(merge, id, entities, jsonEntities);
result = child.loadEntity();
} else if (MatrixND.class.isAssignableFrom(type)) {
String mat = value.asText();
@@ -473,7 +530,7 @@
new EntitySemanticsDecorator.EntityProvider() {
@Override
public Object findById(String id) {
- RegionVisitor child = new RegionVisitor(merge, mergeContext, id, entities, jsonEntities);
+ RegionVisitor child = new RegionVisitor(merge, id, entities, jsonEntities);
Object result = child.loadEntity();
return result;
}
@@ -542,7 +599,9 @@
}
for (JsonNode currentValue : jsonValue) {
Object v = readValue(type, currentValue);
- c.add(v);
+ if (v != null) {
+ c.add(v);
+ }
}
BeanUtils.setProperty(entity, propertyName, c);
}
Modified: trunk/src/main/java/fr/ifremer/isisfish/ui/input/InputHandler.java
===================================================================
--- trunk/src/main/java/fr/ifremer/isisfish/ui/input/InputHandler.java 2016-11-09 18:44:50 UTC (rev 4372)
+++ trunk/src/main/java/fr/ifremer/isisfish/ui/input/InputHandler.java 2016-11-10 13:08:37 UTC (rev 4373)
@@ -436,10 +436,12 @@
if (file != null) {
FisheryRegion fisheryRegion = inputUI.getContextValue(FisheryRegion.class);
TopiaContext tx = fisheryRegion.getTopiaContext();
+ RegionImportJson.RegionMergeDatabase merge =
+ new RegionImportJson.RegionMergeDatabase(tx);
try (InputStream in = new BufferedInputStream(
new GZIPInputStream(new FileInputStream(file)))) {
RegionImportJson json = new RegionImportJson(new InputStreamReader(in, "UTF-8"),
- new RegionImportJson.RegionMergeDatabase(tx));
+ merge);
Collection<TopiaEntity> entities = json.getEntities();
log.info("Entities to importe: " + entities.size());
for (TopiaEntity e : entities) {
@@ -448,7 +450,9 @@
tx.commitTransaction();
} catch (Exception eee) {
tx.rollbackTransaction();
- throw eee;
+ if (!merge.getLastAnswer().isAbort()) {
+ throw eee;
+ }
}
}
} catch (Exception eee) {
1
0
r4372 - trunk/src/main/java/fr/ifremer/isisfish/entities
by bpoussin@users.forge.codelutin.com 09 Nov '16
by bpoussin@users.forge.codelutin.com 09 Nov '16
09 Nov '16
Author: bpoussin
Date: 2016-11-09 19:44:50 +0100 (Wed, 09 Nov 2016)
New Revision: 4372
Url: http://forge.codelutin.com/projects/isis-fish/repository/revisions/4372
Log:
amelioration des propositions
Evolution #8737: Export / Import des ?\195?\169l?\195?\169ments d'une r?\195?\169gion, soit pour import dans une autre r?\195?\169gion, soit pour fusion de deux r?\195?\169gions
Modified:
trunk/src/main/java/fr/ifremer/isisfish/entities/RegionImportJson.java
Modified: trunk/src/main/java/fr/ifremer/isisfish/entities/RegionImportJson.java
===================================================================
--- trunk/src/main/java/fr/ifremer/isisfish/entities/RegionImportJson.java 2016-11-09 17:45:08 UTC (rev 4371)
+++ trunk/src/main/java/fr/ifremer/isisfish/entities/RegionImportJson.java 2016-11-09 18:44:50 UTC (rev 4372)
@@ -25,6 +25,7 @@
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import org.apache.commons.beanutils.BeanUtils;
+import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuiton.math.matrix.MatrixND;
@@ -50,10 +51,17 @@
public class RegionImportJson {
static public interface RegionMerge {
- static public enum AnwserType {ABORT, IMPORT, IMPORT_ALL_TYPE, IMPORT_ALL, REUSE, NONE};
+ static public enum AnwserType {
+ NONE,
+ REUSE, REUSE_ONE_TYPE, REUSE_ONE,
+ IMPORT, IMPORT_ALL_TYPE, IMPORT_ALL,
+ ABORT
+ };
static public class RegionMergeContext {
protected boolean abort = false;
+ protected boolean reuseOne = false;
+ protected Set<Class> reuseOneType = new HashSet<Class>();
protected boolean importAll = false;
protected Set<Class> importAllType = new HashSet<Class>();
protected AnwserType lastAnswer = null;
@@ -67,6 +75,10 @@
this.importAll = importAll;
}
+ public Class getCurrentType() {
+ return currentType;
+ }
+
public TopiaEntity getReuseEntity() {
return reuseEntity;
}
@@ -75,13 +87,13 @@
return lastAnswer;
}
- protected void setLastAnswer(TopiaEntity e) {
- this.lastAnswer = AnwserType.REUSE;
- this.reuseEntity = e;
+ protected void setLastAnswer(AnwserType lastAnswer) {
+ this.setLastAnswer(lastAnswer, null);
}
- protected void setLastAnswer(AnwserType lastAnswer) {
+ protected void setLastAnswer(AnwserType lastAnswer, TopiaEntity e) {
this.lastAnswer = lastAnswer;
+ this.reuseEntity = e;
switch (lastAnswer) {
case ABORT:
abort = true;
@@ -94,6 +106,14 @@
importAll = true;
this.lastAnswer = AnwserType.IMPORT;
break;
+ case REUSE_ONE_TYPE:
+ reuseOneType.add(currentType);
+ this.lastAnswer = AnwserType.REUSE;
+ break;
+ case REUSE_ONE:
+ reuseOne = true;
+ this.lastAnswer = AnwserType.REUSE;
+ break;
default:
break;
}
@@ -109,6 +129,16 @@
}
return lastAnswer;
}
+
+ public AnwserType initAnswer(Class type, Set<TopiaEntity> possible) {
+ lastAnswer = null;
+ currentType = type;
+ if (possible != null && possible.size() == 1 && (reuseOne || reuseOneType.contains(type))) {
+ lastAnswer = AnwserType.REUSE;
+ this.reuseEntity = possible.iterator().next();
+ }
+ return lastAnswer;
+ }
}
/**
@@ -159,11 +189,30 @@
LinkedHashSet<TopiaEntity> possible = new LinkedHashSet<TopiaEntity>();
possible.add(dao.findByTopiaId(id));
- if (details.containsKey("name")) {
- possible.addAll(dao.findAllByProperty("name", details.get("name")));
+ if (Equation.class.isAssignableFrom(context.getCurrentType())) {
+ if (details.containsKey("name") && details.containsKey("category")) {
+ possible.addAll(dao.findAllByProperties("name", details.get("name"), "category", details.get("category")));
+ }
+ } else {
+ if (details.containsKey("name")) {
+ possible.addAll(dao.findAllByProperty("name", details.get("name")));
+ }
}
-// possible.addAll(dao.findAllByProperties(details));
+ context.initAnswer(type, possible);
+
+ if (context.lastAnswer != null) {
+ return context;
+ }
+
+ if (Equation.class.isAssignableFrom(context.getCurrentType())) {
+ if (details.containsKey("category")) {
+ possible.addAll(dao.findAllByProperty("category", details.get("category")));
+ }
+ } else {
+ possible.addAll(dao.findAll());
+ }
+
ask(context, toString, details, possible);
return context;
@@ -178,6 +227,8 @@
Object[] options = new Object[] {
"None", // for null
"Reuse selected object",
+ "Reuse if only one (same type)",
+ "Reuse if only one (all type)",
"Import",
"Import All this type",
"Import All",
@@ -187,12 +238,15 @@
int result = JOptionPane.showOptionDialog(null,
new Object[]{
- String.format("You try to import '%s'", toString),
+ String.format("You try to import '%s' (%s)", toString,
+ ClassUtils.getShortClassName(context.getCurrentType())),
"",
"Possible answer are:",
"None: don't import nor reuse object",
"Reuse: use object already in current Region (make your choice in next combobox).",
select,
+ "Reuse if only one (same type): Answer Reuse and for next time use object found in current Region if only one match 'id' or 'name' for this type",
+ "Reuse if only one (all type): Answer Reuse and for next time use object found in current Region if only one match 'id' or 'name' for all object",
"Import: import object from file.",
"Import All this type: auto answer Import for this question and all next question for same object type",
"Import All: auto answer Import for this question and all next question.",
@@ -205,23 +259,30 @@
options,
options[0]);
+ TopiaEntity selectEntity = (TopiaEntity)select.getSelectedItem();
switch (result) {
case 0:
context.setLastAnswer(AnwserType.NONE);
break;
case 1:
- context.setLastAnswer((TopiaEntity)select.getSelectedItem());
+ context.setLastAnswer(AnwserType.REUSE, selectEntity);
break;
case 2:
+ context.setLastAnswer(AnwserType.REUSE_ONE_TYPE, selectEntity);
+ break;
+ case 3:
+ context.setLastAnswer(AnwserType.REUSE_ONE, selectEntity);
+ break;
+ case 4:
context.setLastAnswer(AnwserType.IMPORT);
break;
- case 3:
+ case 5:
context.setLastAnswer(AnwserType.IMPORT_ALL_TYPE);
break;
- case 4:
+ case 6:
context.setLastAnswer(AnwserType.IMPORT_ALL);
break;
- case 5:
+ case 7:
context.setLastAnswer(AnwserType.ABORT);
break;
default:
1
0
r4371 - trunk/src/main/java/fr/ifremer/isisfish/entities
by bpoussin@users.forge.codelutin.com 09 Nov '16
by bpoussin@users.forge.codelutin.com 09 Nov '16
09 Nov '16
Author: bpoussin
Date: 2016-11-09 18:45:08 +0100 (Wed, 09 Nov 2016)
New Revision: 4371
Url: http://forge.codelutin.com/projects/isis-fish/repository/revisions/4371
Log:
premiere version fonctionnel
Evolution #8737: Export / Import des ?\195?\169l?\195?\169ments d'une r?\195?\169gion, soit pour import dans une autre r?\195?\169gion, soit pour fusion de deux r?\195?\169gions
Modified:
trunk/src/main/java/fr/ifremer/isisfish/entities/MetierImpl.java
trunk/src/main/java/fr/ifremer/isisfish/entities/PopulationImpl.java
trunk/src/main/java/fr/ifremer/isisfish/entities/RegionImportJson.java
trunk/src/main/java/fr/ifremer/isisfish/entities/SpeciesImpl.java
Modified: trunk/src/main/java/fr/ifremer/isisfish/entities/MetierImpl.java
===================================================================
--- trunk/src/main/java/fr/ifremer/isisfish/entities/MetierImpl.java 2016-11-09 15:24:51 UTC (rev 4370)
+++ trunk/src/main/java/fr/ifremer/isisfish/entities/MetierImpl.java 2016-11-09 17:45:08 UTC (rev 4371)
@@ -31,7 +31,9 @@
import fr.ifremer.isisfish.datastore.RegionStorage;
import fr.ifremer.isisfish.datastore.StorageException;
import fr.ifremer.isisfish.types.Month;
+import java.util.List;
+
/**
* Implantation des operations pour l'entité Metier.
*
@@ -59,6 +61,18 @@
}
}
+ /**
+ * Surcharge car avec une aggregation (lien fort) il ne faut pas remplacer
+ * la collection, mais vider celle qui existe et y mettre les nouveaux elements
+ * sinon hibernate perd la tete et leve des exceptions (detecter lors de l'implantation
+ * de l'import json)
+ */
+ @Override
+ public void setMetierSeasonInfo(List<MetierSeasonInfo> metierSeasonInfo) {
+ clearMetierSeasonInfo();
+ addAllMetierSeasonInfo(metierSeasonInfo);
+ }
+
/*
* @see fr.ifremer.isisfish.entities.Metier#getMetierSeasonInfo(fr.ifremer.isisfish.types.Month)
*/
Modified: trunk/src/main/java/fr/ifremer/isisfish/entities/PopulationImpl.java
===================================================================
--- trunk/src/main/java/fr/ifremer/isisfish/entities/PopulationImpl.java 2016-11-09 15:24:51 UTC (rev 4370)
+++ trunk/src/main/java/fr/ifremer/isisfish/entities/PopulationImpl.java 2016-11-09 17:45:08 UTC (rev 4371)
@@ -78,6 +78,31 @@
private static final long serialVersionUID = 1L;
/**
+ * Surcharge car avec une aggregation (lien fort) il ne faut pas remplacer
+ * la collection, mais vider celle qui existe et y mettre les nouveaux elements
+ * sinon hibernate perd la tete et leve des exceptions (detecter lors de l'implantation
+ * de l'import json)
+ * @param populationGroup
+ */
+ @Override
+ public void setPopulationGroup(List<PopulationGroup> populationGroup) {
+ clearPopulationGroup();
+ addAllPopulationGroup(populationGroup);
+ }
+
+ /**
+ * Surcharge car avec une aggregation (lien fort) il ne faut pas remplacer
+ * la collection, mais vider celle qui existe et y mettre les nouveaux elements
+ * sinon hibernate perd la tete et leve des exceptions (detecter lors de l'implantation
+ * de l'import json)
+ */
+ @Override
+ public void setPopulationSeasonInfo(List<PopulationSeasonInfo> populationSeasonInfo) {
+ clearPopulationSeasonInfo();
+ addAllPopulationSeasonInfo(populationSeasonInfo);
+ }
+
+ /**
* Overwrite delete.
* @throws TopiaException
*/
Modified: trunk/src/main/java/fr/ifremer/isisfish/entities/RegionImportJson.java
===================================================================
--- trunk/src/main/java/fr/ifremer/isisfish/entities/RegionImportJson.java 2016-11-09 15:24:51 UTC (rev 4370)
+++ trunk/src/main/java/fr/ifremer/isisfish/entities/RegionImportJson.java 2016-11-09 17:45:08 UTC (rev 4371)
@@ -159,7 +159,9 @@
LinkedHashSet<TopiaEntity> possible = new LinkedHashSet<TopiaEntity>();
possible.add(dao.findByTopiaId(id));
- possible.addAll(dao.findAllByProperty("name", details.get("name")));
+ if (details.containsKey("name")) {
+ possible.addAll(dao.findAllByProperty("name", details.get("name")));
+ }
// possible.addAll(dao.findAllByProperties(details));
ask(context, toString, details, possible);
@@ -186,6 +188,7 @@
int result = JOptionPane.showOptionDialog(null,
new Object[]{
String.format("You try to import '%s'", toString),
+ "",
"Possible answer are:",
"None: don't import nor reuse object",
"Reuse: use object already in current Region (make your choice in next combobox).",
@@ -245,26 +248,6 @@
}
-// static protected class TopiaTypeResolver extends SimpleAbstractTypeResolver {
-// static private Log log = LogFactory.getLog(TopiaTypeResolver.class);
-// @Override
-// public JavaType findTypeMapping(DeserializationConfig config, JavaType type) {
-// JavaType result = super.findTypeMapping(config, type);
-// if (result == null) {
-// Class<?> src = type.getRawClass();
-// if (TopiaEntity.class.isAssignableFrom(src) && !src.getSimpleName().endsWith("Impl")) {
-// try {
-// Class<?> dest = Class.forName(src.getName() + "Impl");
-// result = config.getTypeFactory().constructSpecializedType(type, dest);
-// } catch (ClassNotFoundException eee) {
-// log.error("Can't create class for " + src.getName() + "Impl", eee);
-// }
-// }
-// }
-// return result;
-// }
-// }
-
/** to use log facility, just put in your code: log.info(\"...\"); */
static private Log log = LogFactory.getLog(RegionImportJson.class);
@@ -273,7 +256,6 @@
protected JsonNode json;
protected LinkedHashMap<String, TopiaEntity> entities;
protected JsonNode jsonEntities;
-// protected ObjectMapper mapper;
protected TopiaEntity currentEntity;
/**
@@ -289,17 +271,6 @@
entities = new LinkedHashMap<String, TopiaEntity>();
-// mapper = new ObjectMapper();
-// mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
-// SimpleModule isisModule = new SimpleModule("IsisModule");
-// isisModule.setAbstractTypes(new TopiaTypeResolver());
-// isisModule.addDeserializer(Month.class, new MonthJsonDeserializer())
-// .addDeserializer(TimeUnit.class, new TimeUnitJsonDeserializer())
-// .addDeserializer(RangeOfValues.class, new RangeOfValuesJsonDeserializer())
-// .addDeserializer(MatrixND.class, new MatrixNDJsonDeserializer(this))
-// .addDeserializer(TopiaEntity.class, new TopiaEntityJsonDeserializer(this));
-// mapper.registerModule(isisModule);
-
ObjectMapper m = new ObjectMapper();
json = m.readTree(r);
jsonEntities = json.get("#entities");
@@ -344,57 +315,6 @@
}
}
-// /**
-// * Return entity with given id. If not already converted, convert it and
-// * add it in global entities converted pool
-// * @param id
-// * @return
-// */
-// private TopiaEntity getEntity(String id, DeserializationContext ctxt) {
-// try {
-// TopiaEntity result = entities.get(id);
-// if (result == null) {
-// JsonNode node = jsonEntities.get(id);
-//
-// Map<String, Object> details = new HashMap<String, Object>();
-// for (Iterator<Entry<String, JsonNode>> i = node.fields(); i.hasNext();) {
-// Entry<String, JsonNode> e = i.next();
-// if (!e.getKey().startsWith("#")) {
-// details.put(e.getKey(), e.getValue().asText());
-// }
-// }
-//
-// TopiaEntity o;
-// String toString = node.get("#toString").asText();
-// merge.choice(mergeContext, id, toString, details);
-// switch (mergeContext.lastAnswer) {
-// case ABORT:
-// throw new IsisFishRuntimeException("Import aborted by user");
-// case NONE:
-// o = null;
-// break;
-// case REUSE:
-// o = mergeContext.getReuseEntity();
-// break;
-// default:
-// // import
-// Class clazz = Class.forName(node.get("#class").asText());
-// o = (TopiaEntity)clazz.newInstance();
-// JavaType type = ctxt.constructType(clazz);
-// JsonDeserializer<Object> des = ctxt.findNonContextualValueDeserializer(type);
-// o = (TopiaEntity)des.deserialize(node.traverse(), ctxt, o);
-//// o = (TopiaEntity)mapper.convertValue(node, clazz);
-// break;
-// }
-//
-// entities.put(id, o);
-// }
-// return result;
-// } catch (Exception eee) {
-// throw new RuntimeException(eee);
-// }
-// }
-
static protected class RegionVisitor implements EntityVisitor {
protected RegionMerge merge;
@@ -579,89 +499,4 @@
public void clear() {
}
}
-
-
-
-//
-// private static class MatrixNDJsonDeserializer extends JsonDeserializer<MatrixND> {
-//
-// protected RegionImportJson importer;
-//
-// public MatrixNDJsonDeserializer(final RegionImportJson importer) {
-// this.importer = importer;
-// }
-//
-// @Override
-// public MatrixND deserialize(JsonParser p, final DeserializationContext ctxt) throws IOException, JsonProcessingException {
-// ObjectCodec oc = p.getCodec();
-// JsonNode node = oc.readTree(p);
-// String mat = node.asText();
-//
-// MatrixCSVHelper matrixCSVHelper = new MatrixCSVHelper(new EntitySemanticsDecorator(
-// new EntitySemanticsDecorator.EntityProvider() {
-// @Override
-// public Object findById(String id) {
-// Object result = importer.getEntity(id, ctxt);
-// return result;
-// }
-// }));
-//
-// MatrixND result = matrixCSVHelper.readMatrix(mat);
-// return result;
-// }
-//
-// }
-//
-// private static class TopiaEntityJsonDeserializer extends JsonDeserializer<TopiaEntity> {
-//
-// protected RegionImportJson importer;
-//
-// public TopiaEntityJsonDeserializer(RegionImportJson importer) {
-// this.importer = importer;
-// }
-//
-// @Override
-// public TopiaEntity deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
-// ObjectCodec oc = p.getCodec();
-// JsonNode node = oc.readTree(p);
-// String id = node.asText();
-// TopiaEntity result = importer.getEntity(id, ctxt);
-// return result;
-// }
-//
-// }
-//
-// private static class MonthJsonDeserializer extends JsonDeserializer<Month> {
-// @Override
-// public Month deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
-// ObjectCodec oc = p.getCodec();
-// JsonNode node = oc.readTree(p);
-// int m = node.asInt();
-// Month result = Month.MONTH[m];
-// return result;
-// }
-// }
-//
-// private static class TimeUnitJsonDeserializer extends JsonDeserializer<TimeUnit> {
-// @Override
-// public TimeUnit deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
-// ObjectCodec oc = p.getCodec();
-// JsonNode node = oc.readTree(p);
-// int v = node.asInt();
-// TimeUnit result = new TimeUnit(v);
-// return result;
-// }
-// }
-//
-// private static class RangeOfValuesJsonDeserializer extends JsonDeserializer<RangeOfValues> {
-// @Override
-// public RangeOfValues deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
-// ObjectCodec oc = p.getCodec();
-// JsonNode node = oc.readTree(p);
-// String v = node.asText();
-// RangeOfValues result = new RangeOfValues(v);
-// return result;
-// }
-// }
-
}
Modified: trunk/src/main/java/fr/ifremer/isisfish/entities/SpeciesImpl.java
===================================================================
--- trunk/src/main/java/fr/ifremer/isisfish/entities/SpeciesImpl.java 2016-11-09 15:24:51 UTC (rev 4370)
+++ trunk/src/main/java/fr/ifremer/isisfish/entities/SpeciesImpl.java 2016-11-09 17:45:08 UTC (rev 4371)
@@ -28,6 +28,7 @@
import fr.ifremer.isisfish.IsisFishRuntimeException;
import fr.ifremer.isisfish.datastore.RegionStorage;
import fr.ifremer.isisfish.datastore.StorageException;
+import java.util.Collection;
/**
* Implantation des operations pour l'entité Species.
@@ -55,6 +56,18 @@
}
}
+ /**
+ * Surcharge car avec une aggregation (lien fort) il ne faut pas remplacer
+ * la collection, mais vider celle qui existe et y mettre les nouveaux elements
+ * sinon hibernate perd la tete et leve des exceptions (detecter lors de l'implantation
+ * de l'import json)
+ */
+ @Override
+ public void setPopulation(Collection<Population> population) {
+ clearPopulation();
+ addAllPopulation(population);
+ }
+
/*
* @see fr.ifremer.isisfish.entities.FisheryRegionAbstract#toString()
*/
1
0
r4370 - in trunk/src/main/java/fr/ifremer/isisfish: entities ui/input
by bpoussin@users.forge.codelutin.com 09 Nov '16
by bpoussin@users.forge.codelutin.com 09 Nov '16
09 Nov '16
Author: bpoussin
Date: 2016-11-09 16:24:51 +0100 (Wed, 09 Nov 2016)
New Revision: 4370
Url: http://forge.codelutin.com/projects/isis-fish/repository/revisions/4370
Log:
work in progress: Evolution #8737: Export / Import des ?\195?\169l?\195?\169ments d'une r?\195?\169gion, soit pour import dans une autre r?\195?\169gion, soit pour fusion de deux r?\195?\169gions.
Modified:
trunk/src/main/java/fr/ifremer/isisfish/entities/RegionExportJson.java
trunk/src/main/java/fr/ifremer/isisfish/entities/RegionImportJson.java
trunk/src/main/java/fr/ifremer/isisfish/ui/input/InputHandler.java
Modified: trunk/src/main/java/fr/ifremer/isisfish/entities/RegionExportJson.java
===================================================================
--- trunk/src/main/java/fr/ifremer/isisfish/entities/RegionExportJson.java 2016-11-04 15:05:55 UTC (rev 4369)
+++ trunk/src/main/java/fr/ifremer/isisfish/entities/RegionExportJson.java 2016-11-09 15:24:51 UTC (rev 4370)
@@ -364,33 +364,4 @@
decorator = null;
}
-
-// abstract public void visit(Cell v);
-// abstract public void visit(EffortDescription v);
-// abstract public void visit(FisheryRegion region);
-// abstract public void visit(Gear v);
-// abstract public void visit(Metier v);
-// abstract public void visit(MetierSeasonInfo v);
-// abstract public void visit(PopulationGroup v);
-// abstract public void visit(Population v);
-// abstract public void visit(PopulationSeasonInfo v);
-// abstract public void visit(Port v);
-// abstract public void visit(Season v);
-// abstract public void visit(Selectivity v);
-// abstract public void visit(SetOfVessels v);
-// abstract public void visit(Species v);
-// abstract public void visit(Strategy v);
-// abstract public void visit(StrategyMonthInfo v);
-// abstract public void visit(TargetSpecies v);
-// abstract public void visit(TripType v);
-// abstract public void visit(Variable v);
-// abstract public void visit(VariableType v);
-// abstract public void visit(VesselType v);
-// abstract public void visit(Zone v);
-// abstract public void visit(Equation v);
-// abstract public void visit(MatrixND v);
-// abstract public void visit(String v);
-// abstract public void visit(int v);
-// abstract public void visit(float v);
-
}
Modified: trunk/src/main/java/fr/ifremer/isisfish/entities/RegionImportJson.java
===================================================================
--- trunk/src/main/java/fr/ifremer/isisfish/entities/RegionImportJson.java 2016-11-04 15:05:55 UTC (rev 4369)
+++ trunk/src/main/java/fr/ifremer/isisfish/entities/RegionImportJson.java 2016-11-09 15:24:51 UTC (rev 4370)
@@ -1,15 +1,10 @@
package fr.ifremer.isisfish.entities;
-import com.fasterxml.jackson.core.JsonParser;
-import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.core.ObjectCodec;
-import com.fasterxml.jackson.databind.DeserializationContext;
-import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.module.SimpleModule;
import fr.ifremer.isisfish.IsisFishDAOHelper;
+import fr.ifremer.isisfish.IsisFishRuntimeException;
import fr.ifremer.isisfish.types.Month;
import fr.ifremer.isisfish.types.RangeOfValues;
import fr.ifremer.isisfish.types.TimeUnit;
@@ -18,15 +13,26 @@
import java.io.IOException;
import java.io.Reader;
import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
+import java.util.LinkedList;
+import java.util.Map;
import java.util.Map.Entry;
+import java.util.Set;
+import javax.swing.JComboBox;
+import javax.swing.JOptionPane;
+import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuiton.math.matrix.MatrixND;
import org.nuiton.topia.TopiaContext;
+import org.nuiton.topia.persistence.EntityVisitor;
+import org.nuiton.topia.persistence.TopiaDAO;
import org.nuiton.topia.persistence.TopiaEntity;
+import org.nuiton.topia.persistence.TopiaId;
/**
* Classe permettant d'importer des données dans une region a partir d'un
@@ -44,246 +50,255 @@
public class RegionImportJson {
static public interface RegionMerge {
- public TopiaEntity choice(TopiaEntity e);
- }
+ static public enum AnwserType {ABORT, IMPORT, IMPORT_ALL_TYPE, IMPORT_ALL, REUSE, NONE};
- static public class RegionMergeDatabase implements RegionMerge {
- protected TopiaContext tx;
+ static public class RegionMergeContext {
+ protected boolean abort = false;
+ protected boolean importAll = false;
+ protected Set<Class> importAllType = new HashSet<Class>();
+ protected AnwserType lastAnswer = null;
+ protected TopiaEntity reuseEntity;
+ protected Class currentType;
- public RegionMergeDatabase(TopiaContext tx) {
- this.tx = tx;
- }
+ public RegionMergeContext() {
+ }
- @Override
- public TopiaEntity choice(TopiaEntity e) {
- TopiaEntity result;
-
- if (e instanceof FisheryRegion) {
- result = choice((FisheryRegion)e);
- } else if (e instanceof Cell) {
- result = choice((Cell)e);
- } else if (e instanceof Zone) {
- result = choice((Zone)e);
- } else if (e instanceof Port) {
- result = choice((Port)e);
- } else if (e instanceof Species) {
- result = choice((Species)e);
- } else if (e instanceof Population) {
- result = choice((Population)e);
- } else if (e instanceof Gear) {
- result = choice((Gear)e);
- } else if (e instanceof Metier) {
- result = choice((Metier)e);
- } else if (e instanceof TripType) {
- result = choice((TripType)e);
- } else if (e instanceof VesselType) {
- result = choice((VesselType)e);
- } else if (e instanceof SetOfVessels) {
- result = choice((SetOfVessels)e);
- } else if (e instanceof Strategy) {
- result = choice((Strategy)e);
- } else if (e instanceof Observation) {
- result = choice((Observation)e);
- } else {
- log.info("Not supported entity type: " + e.getClass().getSimpleName());
- result = genericChoice(e);
+ public RegionMergeContext(boolean importAll) {
+ this.importAll = importAll;
}
- return result;
- }
+ public TopiaEntity getReuseEntity() {
+ return reuseEntity;
+ }
- protected TopiaEntity ask(TopiaEntity e, Collection<TopiaEntity> possible) {
- System.out.println("Choice between:" + e + " and " + possible);
- return e; // FIXME
- }
+ public AnwserType getLastAnswer() {
+ return lastAnswer;
+ }
- protected TopiaEntity genericChoice(TopiaEntity e) {
- LinkedHashSet<TopiaEntity> possible = new LinkedHashSet<TopiaEntity>();
+ protected void setLastAnswer(TopiaEntity e) {
+ this.lastAnswer = AnwserType.REUSE;
+ this.reuseEntity = e;
+ }
- possible.add(tx.findByTopiaId(e.getTopiaId()));
+ protected void setLastAnswer(AnwserType lastAnswer) {
+ this.lastAnswer = lastAnswer;
+ switch (lastAnswer) {
+ case ABORT:
+ abort = true;
+ break;
+ case IMPORT_ALL_TYPE:
+ importAllType.add(currentType);
+ this.lastAnswer = AnwserType.IMPORT;
+ break;
+ case IMPORT_ALL:
+ importAll = true;
+ this.lastAnswer = AnwserType.IMPORT;
+ break;
+ default:
+ break;
+ }
+ }
- TopiaEntity result = ask(e, possible);
- return result;
+ public AnwserType initAnswer(Class type) {
+ lastAnswer = null;
+ currentType = type;
+ if (abort) {
+ lastAnswer = AnwserType.ABORT;
+ } else if (importAll || importAllType.contains(type)) {
+ lastAnswer = AnwserType.IMPORT;
+ }
+ return lastAnswer;
+ }
}
- protected TopiaEntity choice(FisheryRegion e) {
- FisheryRegionDAO dao = IsisFishDAOHelper.getFisheryRegionDAO(tx);
- LinkedHashSet<TopiaEntity> possible = new LinkedHashSet<TopiaEntity>();
+ /**
+ * Ask how to merge entity
+ * @param context
+ * @param id
+ * @param toString
+ * @param details
+ * @return
+ */
+ public RegionMergeContext choice(RegionMergeContext context, String id, String toString, Map<String, Object> details);
+ /**
+ * If exist return entity with id in parameter
+ * @param id
+ * @return null or TopiaEntity
+ */
+ public TopiaEntity getEntity(String id);
+ }
- possible.add(dao.findByTopiaId(e.getTopiaId()));
- possible.addAll(dao.findAllByName(e.getName()));
+ static public class RegionMergeDatabase implements RegionMerge {
+ protected TopiaContext tx;
- TopiaEntity result = ask(e, possible);
- return result;
+ public RegionMergeDatabase(TopiaContext tx) {
+ this.tx = tx;
}
- protected TopiaEntity choice(Cell e) {
- CellDAO dao = IsisFishDAOHelper.getCellDAO(tx);
- LinkedHashSet<TopiaEntity> possible = new LinkedHashSet<TopiaEntity>();
-
- possible.add(dao.findByTopiaId(e.getTopiaId()));
- possible.addAll(dao.findAllByName(e.getName()));
-
- TopiaEntity result = ask(e, possible);
+ @Override
+ public TopiaEntity getEntity(String id) {
+ TopiaEntity result = tx.findByTopiaId(id);
return result;
}
-
- protected TopiaEntity choice(Zone e) {
- ZoneDAO dao = IsisFishDAOHelper.getZoneDAO(tx);
- LinkedHashSet<TopiaEntity> possible = new LinkedHashSet<TopiaEntity>();
- possible.add(dao.findByTopiaId(e.getTopiaId()));
- possible.addAll(dao.findAllByName(e.getName()));
+ @Override
+ public RegionMergeContext choice(RegionMergeContext context, String id, String toString, Map<String, Object> details) {
+ Class type = TopiaId.getClassName(id);
+ if (context == null) {
+ context = new RegionMergeContext();
+ } else {
+ context.initAnswer(type);
+ }
- TopiaEntity result = ask(e, possible);
- return result;
- }
+ if (context.lastAnswer != null) {
+ return context;
+ }
- protected TopiaEntity choice(Port e) {
- PortDAO dao = IsisFishDAOHelper.getPortDAO(tx);
- LinkedHashSet<TopiaEntity> possible = new LinkedHashSet<TopiaEntity>();
+ TopiaDAO<TopiaEntity> dao = IsisFishDAOHelper.getDAO(tx, type);
- possible.add(dao.findByTopiaId(e.getTopiaId()));
- possible.addAll(dao.findAllByName(e.getName()));
-
- TopiaEntity result = ask(e, possible);
- return result;
- }
-
- protected TopiaEntity choice(Species e) {
- SpeciesDAO dao = IsisFishDAOHelper.getSpeciesDAO(tx);
LinkedHashSet<TopiaEntity> possible = new LinkedHashSet<TopiaEntity>();
- possible.add(dao.findByTopiaId(e.getTopiaId()));
- possible.addAll(dao.findAllByName(e.getName()));
+ possible.add(dao.findByTopiaId(id));
+ possible.addAll(dao.findAllByProperty("name", details.get("name")));
+// possible.addAll(dao.findAllByProperties(details));
- TopiaEntity result = ask(e, possible);
- return result;
- }
+ ask(context, toString, details, possible);
- protected TopiaEntity choice(Population e) {
- PopulationDAO dao = IsisFishDAOHelper.getPopulationDAO(tx);
- LinkedHashSet<TopiaEntity> possible = new LinkedHashSet<TopiaEntity>();
-
- possible.add(dao.findByTopiaId(e.getTopiaId()));
- possible.addAll(dao.findAllByName(e.getName()));
-
- TopiaEntity result = ask(e, possible);
- return result;
+ return context;
}
- protected TopiaEntity choice(Gear e) {
- GearDAO dao = IsisFishDAOHelper.getGearDAO(tx);
- LinkedHashSet<TopiaEntity> possible = new LinkedHashSet<TopiaEntity>();
+ protected void ask(RegionMergeContext context, String toString,
+ Map<String, Object> details, Collection<TopiaEntity> possible) {
+ System.out.println("Choice between:" + toString + " and " + possible);
- possible.add(dao.findByTopiaId(e.getTopiaId()));
- possible.addAll(dao.findAllByName(e.getName()));
+ JComboBox select = new JComboBox(possible.toArray());
- TopiaEntity result = ask(e, possible);
- return result;
- }
+ Object[] options = new Object[] {
+ "None", // for null
+ "Reuse selected object",
+ "Import",
+ "Import All this type",
+ "Import All",
+ "Abort"
+ };
- protected TopiaEntity choice(Metier e) {
- MetierDAO dao = IsisFishDAOHelper.getMetierDAO(tx);
- LinkedHashSet<TopiaEntity> possible = new LinkedHashSet<TopiaEntity>();
- possible.add(dao.findByTopiaId(e.getTopiaId()));
- possible.addAll(dao.findAllByName(e.getName()));
+ int result = JOptionPane.showOptionDialog(null,
+ new Object[]{
+ String.format("You try to import '%s'", toString),
+ "Possible answer are:",
+ "None: don't import nor reuse object",
+ "Reuse: use object already in current Region (make your choice in next combobox).",
+ select,
+ "Import: import object from file.",
+ "Import All this type: auto answer Import for this question and all next question for same object type",
+ "Import All: auto answer Import for this question and all next question.",
+ "Abort: Cancel this import (import nothing)"
+ },
+ "Make a choice",
+ JOptionPane.DEFAULT_OPTION,
+ JOptionPane.QUESTION_MESSAGE,
+ null,
+ options,
+ options[0]);
- TopiaEntity result = ask(e, possible);
- return result;
+ switch (result) {
+ case 0:
+ context.setLastAnswer(AnwserType.NONE);
+ break;
+ case 1:
+ context.setLastAnswer((TopiaEntity)select.getSelectedItem());
+ break;
+ case 2:
+ context.setLastAnswer(AnwserType.IMPORT);
+ break;
+ case 3:
+ context.setLastAnswer(AnwserType.IMPORT_ALL_TYPE);
+ break;
+ case 4:
+ context.setLastAnswer(AnwserType.IMPORT_ALL);
+ break;
+ case 5:
+ context.setLastAnswer(AnwserType.ABORT);
+ break;
+ default:
+ ask(context, toString, details, possible);
+ break;
+ }
}
+ }
- protected TopiaEntity choice(TripType e) {
- TripTypeDAO dao = IsisFishDAOHelper.getTripTypeDAO(tx);
- LinkedHashSet<TopiaEntity> possible = new LinkedHashSet<TopiaEntity>();
-
- possible.add(dao.findByTopiaId(e.getTopiaId()));
- possible.addAll(dao.findAllByName(e.getName()));
-
- TopiaEntity result = ask(e, possible);
- return result;
+ static public class RegionMergeImportAll implements RegionMerge {
+ @Override
+ public TopiaEntity getEntity(String id) {
+ return null;
}
- protected TopiaEntity choice(VesselType e) {
- VesselTypeDAO dao = IsisFishDAOHelper.getVesselTypeDAO(tx);
- LinkedHashSet<TopiaEntity> possible = new LinkedHashSet<TopiaEntity>();
-
- possible.add(dao.findByTopiaId(e.getTopiaId()));
- possible.addAll(dao.findAllByName(e.getName()));
-
- TopiaEntity result = ask(e, possible);
- return result;
+ @Override
+ public RegionMergeContext choice(RegionMergeContext context, String id, String toString, Map<String, Object> details) {
+ if (context == null) {
+ context = new RegionMergeContext();
+ }
+ context.setLastAnswer(AnwserType.IMPORT);
+ return context;
}
- protected TopiaEntity choice(SetOfVessels e) {
- SetOfVesselsDAO dao = IsisFishDAOHelper.getSetOfVesselsDAO(tx);
- LinkedHashSet<TopiaEntity> possible = new LinkedHashSet<TopiaEntity>();
-
- possible.add(dao.findByTopiaId(e.getTopiaId()));
- possible.addAll(dao.findAllByName(e.getName()));
-
- TopiaEntity result = ask(e, possible);
- return result;
- }
-
- protected TopiaEntity choice(Strategy e) {
- StrategyDAO dao = IsisFishDAOHelper.getStrategyDAO(tx);
- LinkedHashSet<TopiaEntity> possible = new LinkedHashSet<TopiaEntity>();
-
- possible.add(dao.findByTopiaId(e.getTopiaId()));
- possible.addAll(dao.findAllByName(e.getName()));
-
- TopiaEntity result = ask(e, possible);
- return result;
- }
-
- protected TopiaEntity choice(Observation e) {
- ObservationDAO dao = IsisFishDAOHelper.getObservationDAO(tx);
- LinkedHashSet<TopiaEntity> possible = new LinkedHashSet<TopiaEntity>();
-
- possible.add(dao.findByTopiaId(e.getTopiaId()));
- possible.addAll(dao.findAllByName(e.getName()));
-
- TopiaEntity result = ask(e, possible);
- return result;
- }
}
- static public class RegionMergeNoMerge implements RegionMerge {
- @Override
- public TopiaEntity choice(TopiaEntity e) {
- return e;
- }
- }
+// static protected class TopiaTypeResolver extends SimpleAbstractTypeResolver {
+// static private Log log = LogFactory.getLog(TopiaTypeResolver.class);
+// @Override
+// public JavaType findTypeMapping(DeserializationConfig config, JavaType type) {
+// JavaType result = super.findTypeMapping(config, type);
+// if (result == null) {
+// Class<?> src = type.getRawClass();
+// if (TopiaEntity.class.isAssignableFrom(src) && !src.getSimpleName().endsWith("Impl")) {
+// try {
+// Class<?> dest = Class.forName(src.getName() + "Impl");
+// result = config.getTypeFactory().constructSpecializedType(type, dest);
+// } catch (ClassNotFoundException eee) {
+// log.error("Can't create class for " + src.getName() + "Impl", eee);
+// }
+// }
+// }
+// return result;
+// }
+// }
/** to use log facility, just put in your code: log.info(\"...\"); */
static private Log log = LogFactory.getLog(RegionImportJson.class);
protected RegionMerge merge;
+ protected RegionMerge.RegionMergeContext mergeContext;
protected JsonNode json;
protected LinkedHashMap<String, TopiaEntity> entities;
protected JsonNode jsonEntities;
- protected ObjectMapper mapper;
+// protected ObjectMapper mapper;
+ protected TopiaEntity currentEntity;
/**
*
* @param r reader contains json
- * @param merge use to choice entity in Json or entity in current region
+ * @param merge use to choice entity in Json or entity in current region,
+ * if no merge object is passed in arguement "import all" object in reader
*/
public RegionImportJson(Reader r, RegionMerge merge) {
try {
- this.merge = merge != null ? merge : new RegionMergeNoMerge();
+ this.merge = merge != null ? merge : new RegionMergeImportAll();
+ this.mergeContext = new RegionMerge.RegionMergeContext();
+
entities = new LinkedHashMap<String, TopiaEntity>();
- mapper = new ObjectMapper();
- SimpleModule isisModule = new SimpleModule("IsisModule")
- .addDeserializer(Month.class, new MonthJsonDeserializer())
- .addDeserializer(TimeUnit.class, new TimeUnitJsonDeserializer())
- .addDeserializer(RangeOfValues.class, new RangeOfValuesJsonDeserializer())
- .addDeserializer(MatrixND.class, new MatrixNDJsonDeserializer(this))
- .addDeserializer(TopiaEntity.class, new TopiaEntityJsonDeserializer(this));
- mapper.registerModule(isisModule);
+// mapper = new ObjectMapper();
+// mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
+// SimpleModule isisModule = new SimpleModule("IsisModule");
+// isisModule.setAbstractTypes(new TopiaTypeResolver());
+// isisModule.addDeserializer(Month.class, new MonthJsonDeserializer())
+// .addDeserializer(TimeUnit.class, new TimeUnitJsonDeserializer())
+// .addDeserializer(RangeOfValues.class, new RangeOfValuesJsonDeserializer())
+// .addDeserializer(MatrixND.class, new MatrixNDJsonDeserializer(this))
+// .addDeserializer(TopiaEntity.class, new TopiaEntityJsonDeserializer(this));
+// mapper.registerModule(isisModule);
ObjectMapper m = new ObjectMapper();
json = m.readTree(r);
@@ -297,7 +312,7 @@
* Return all entities available in Json reader
* @return
*/
- public Collection<TopiaEntity> getEntity() {
+ public Collection<TopiaEntity> getEntities() {
try {
JsonNode info = json.with("#info");
boolean isRegion = info.get("region").asBoolean(false);
@@ -304,7 +319,9 @@
if (!isRegion) {
// il n'y a qu'un objet dans le json, on le deserialize
String rootId = info.get("rootId").asText();
- getEntity(rootId);
+ RegionVisitor v = new RegionVisitor(merge, mergeContext, rootId, entities, jsonEntities);
+ // no assigment, collect of entities is done during deserialize
+ v.loadEntity();
} else {
// il y a toute une region, on lit les listes d'id des objets
// principaux (cell, zone, port, ...)
@@ -313,7 +330,9 @@
// les champs commencant par des # sont #info et #entities
if (!field.getKey().startsWith("#")) {
for (JsonNode id : field.getValue()) {
- getEntity(id.asText());
+ RegionVisitor v = new RegionVisitor(merge, mergeContext, id.asText(), entities, jsonEntities);
+ // no assigment, collect of entities is done during deserialize
+ v.loadEntity();
}
}
}
@@ -325,104 +344,324 @@
}
}
- /**
- * Return entity with given id. If not already converted, convert it and
- * add it in global entities converted pool
- * @param id
- * @return
- */
- public TopiaEntity getEntity(String id) {
- try {
- TopiaEntity result = entities.get(id);
- if (result == null) {
- JsonNode node = jsonEntities.get(id);
- Class clazz = Class.forName(node.get("#class").asText());
- TopiaEntity o = (TopiaEntity)mapper.convertValue(node, clazz);
- o = merge.choice(o);
- entities.put(id, o);
+// /**
+// * Return entity with given id. If not already converted, convert it and
+// * add it in global entities converted pool
+// * @param id
+// * @return
+// */
+// private TopiaEntity getEntity(String id, DeserializationContext ctxt) {
+// try {
+// TopiaEntity result = entities.get(id);
+// if (result == null) {
+// JsonNode node = jsonEntities.get(id);
+//
+// Map<String, Object> details = new HashMap<String, Object>();
+// for (Iterator<Entry<String, JsonNode>> i = node.fields(); i.hasNext();) {
+// Entry<String, JsonNode> e = i.next();
+// if (!e.getKey().startsWith("#")) {
+// details.put(e.getKey(), e.getValue().asText());
+// }
+// }
+//
+// TopiaEntity o;
+// String toString = node.get("#toString").asText();
+// merge.choice(mergeContext, id, toString, details);
+// switch (mergeContext.lastAnswer) {
+// case ABORT:
+// throw new IsisFishRuntimeException("Import aborted by user");
+// case NONE:
+// o = null;
+// break;
+// case REUSE:
+// o = mergeContext.getReuseEntity();
+// break;
+// default:
+// // import
+// Class clazz = Class.forName(node.get("#class").asText());
+// o = (TopiaEntity)clazz.newInstance();
+// JavaType type = ctxt.constructType(clazz);
+// JsonDeserializer<Object> des = ctxt.findNonContextualValueDeserializer(type);
+// o = (TopiaEntity)des.deserialize(node.traverse(), ctxt, o);
+//// o = (TopiaEntity)mapper.convertValue(node, clazz);
+// break;
+// }
+//
+// entities.put(id, o);
+// }
+// return result;
+// } catch (Exception eee) {
+// throw new RuntimeException(eee);
+// }
+// }
+
+ static protected class RegionVisitor implements EntityVisitor {
+
+ protected RegionMerge merge;
+ protected RegionMerge.RegionMergeContext mergeContext;
+ protected JsonNode nodeEntity;
+ protected TopiaEntity currentEntity;
+ protected LinkedHashMap<String, TopiaEntity> entities;
+ protected JsonNode jsonEntities;
+
+ public RegionVisitor(RegionMerge merge, RegionMerge.RegionMergeContext mergeContext,
+ String idEntity,
+ LinkedHashMap<String, TopiaEntity> entities, JsonNode jsonEntities) {
+ this.merge = merge;
+ this.mergeContext = mergeContext;
+ this.nodeEntity = jsonEntities.get(idEntity);
+ this.entities = entities;
+ this.jsonEntities = jsonEntities;
+ }
+
+ public TopiaEntity loadEntity() {
+ String id = nodeEntity.get("#id").asText();
+ currentEntity = entities.get(id);
+ if (currentEntity == null) {
+ currentEntity = ask(id);
}
+ return currentEntity;
+ }
+
+ protected TopiaEntity convertJsonToEntity(JsonNode node) {
+ TopiaEntity result;
+ String id = node.get("#id").asText();
+ String className = node.get("#class").asText();
+ try {
+ // try to reuse entity
+ result = merge.getEntity(id);
+ if (result == null) {
+ // if no entity with same id, create new one
+ Class clazz = Class.forName(className);
+ result = (TopiaEntity)clazz.newInstance();
+ result.setTopiaId(id);
+ }
+ // add in list save after import
+ entities.put(id, result);
+
+ result.accept(this);
+ } catch (Exception eee) {
+ throw new IsisFishRuntimeException("Can't instanciate entity: " + className, eee);
+ }
return result;
- } catch (Exception eee) {
- throw new RuntimeException(eee);
}
- }
- private static class MatrixNDJsonDeserializer extends JsonDeserializer<MatrixND> {
+ protected TopiaEntity ask(String id) {
+ JsonNode node = jsonEntities.get(id);
- protected MatrixCSVHelper matrixCSVHelper;
-
- public MatrixNDJsonDeserializer(final RegionImportJson importer) {
- matrixCSVHelper = new MatrixCSVHelper(new EntitySemanticsDecorator(
- new EntitySemanticsDecorator.EntityProvider() {
- @Override
- public Object findById(String id) {
- Object result = importer.getEntity(id);
- return result;
+ Map<String, Object> details = new HashMap<String, Object>();
+ for (Iterator<Entry<String, JsonNode>> i = node.fields(); i.hasNext();) {
+ Entry<String, JsonNode> e = i.next();
+ if (!e.getKey().startsWith("#")) {
+ details.put(e.getKey(), e.getValue().asText());
}
- }));
- }
+ }
- @Override
- public MatrixND deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
- ObjectCodec oc = p.getCodec();
- JsonNode node = oc.readTree(p);
- String mat = node.asText();
- MatrixND result = matrixCSVHelper.readMatrix(mat);
+ TopiaEntity result;
+ String toString = node.get("#toString").asText();
+ merge.choice(mergeContext, id, toString, details);
+ switch (mergeContext.lastAnswer) {
+ case ABORT:
+ throw new IsisFishRuntimeException("Import aborted by user");
+ case NONE:
+ result = null;
+ break;
+ case REUSE:
+ result = mergeContext.getReuseEntity();
+ break;
+ default:
+ result = convertJsonToEntity(node);
+ break;
+ }
return result;
}
- }
+ protected Object readValue(Class<?> type, JsonNode value) {
+ Object result;
+ try {
+ if (value == null || value.isNull() || value.isMissingNode()) {
+ result = null;
+ } else if (TopiaEntity.class.isAssignableFrom(type)) {
+ String id = value.asText();
+ RegionVisitor child = new RegionVisitor(merge, mergeContext, id, entities, jsonEntities);
+ result = child.loadEntity();
+ } else if (MatrixND.class.isAssignableFrom(type)) {
+ String mat = value.asText();
- private static class TopiaEntityJsonDeserializer extends JsonDeserializer<TopiaEntity> {
+ MatrixCSVHelper matrixCSVHelper = new MatrixCSVHelper(new EntitySemanticsDecorator(
+ new EntitySemanticsDecorator.EntityProvider() {
+ @Override
+ public Object findById(String id) {
+ RegionVisitor child = new RegionVisitor(merge, mergeContext, id, entities, jsonEntities);
+ Object result = child.loadEntity();
+ return result;
+ }
+ }));
- protected RegionImportJson importer;
+ result = matrixCSVHelper.readMatrix(mat);
- public TopiaEntityJsonDeserializer(RegionImportJson importer) {
- this.importer = importer;
+ } else if (TimeUnit.class.isAssignableFrom(type)) {
+ result = new TimeUnit(value.asDouble());
+ } else if (RangeOfValues.class.isAssignableFrom(type)) {
+ String v = value.asText();
+ result = new RangeOfValues(v);
+ } else if (Month.class.isAssignableFrom(type)) {
+ int m = value.asInt();
+ result = Month.MONTH[m];
+ } else if (Class.class.isAssignableFrom(type)) {
+ String className = value.asText();
+ result = Class.forName(className);
+ } else if (String.class.isAssignableFrom(type)) {
+ result = value.asText();
+ } else if (Boolean.class.isAssignableFrom(type) || Boolean.TYPE.isAssignableFrom(type)) {
+ result = value.asBoolean();
+ } else if (Number.class.isAssignableFrom(type) || type.isPrimitive()) { // Number must be after Boolean
+ // all other possible primitive type is number (void and char are not admited in json)
+ result = value.asDouble();
+ } else {
+ String message = String.format("Unsupported type: '%s' value class '%s' value '%s'", type, value.getClass().getName(), value);
+ log.error(message);
+ throw new IsisFishRuntimeException(message);
+ }
+ } catch (Exception eee) {
+ throw new IsisFishRuntimeException("Can't convert json value:" + value, eee);
+ }
+ return result;
}
@Override
- public TopiaEntity deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
- ObjectCodec oc = p.getCodec();
- JsonNode node = oc.readTree(p);
- String id = node.asText();
- TopiaEntity result = importer.getEntity(id);
- return result;
+ public void start(TopiaEntity entity) {
}
- }
+ @Override
+ public void end(TopiaEntity entity) {
+ }
- private static class MonthJsonDeserializer extends JsonDeserializer<Month> {
@Override
- public Month deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
- ObjectCodec oc = p.getCodec();
- JsonNode node = oc.readTree(p);
- int m = node.asInt();
- Month result = Month.MONTH[m];
- return result;
+ public void visit(TopiaEntity entity, String propertyName, Class<?> type, Object value) {
+ JsonNode jsonValue = nodeEntity.get(propertyName);
+ try {
+ value = readValue(type, jsonValue);
+ BeanUtils.setProperty(entity, propertyName, value);
+ } catch (Exception eee) {
+ throw new IsisFishRuntimeException(String.format("Can't set property '%s' from json value: '%s'", propertyName, jsonValue), eee);
+ }
}
- }
- private static class TimeUnitJsonDeserializer extends JsonDeserializer<TimeUnit> {
@Override
- public TimeUnit deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
- ObjectCodec oc = p.getCodec();
- JsonNode node = oc.readTree(p);
- int v = node.asInt();
- TimeUnit result = new TimeUnit(v);
- return result;
+ public void visit(TopiaEntity entity, String propertyName, Class<?> collectionType, Class<?> type, Object value) {
+ JsonNode jsonValue = nodeEntity.get(propertyName);
+ try {
+ if (value != null) {
+ Collection c;
+ if (Set.class.isAssignableFrom(collectionType)) {
+ c = new HashSet();
+ } else {
+ c = new LinkedList();
+ }
+ for (JsonNode currentValue : jsonValue) {
+ Object v = readValue(type, currentValue);
+ c.add(v);
+ }
+ BeanUtils.setProperty(entity, propertyName, c);
+ }
+ } catch (Exception eee) {
+ throw new IsisFishRuntimeException(String.format("Can't set property '%s' from json value: '%s'", propertyName, jsonValue), eee);
+ }
}
- }
- private static class RangeOfValuesJsonDeserializer extends JsonDeserializer<RangeOfValues> {
@Override
- public RangeOfValues deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
- ObjectCodec oc = p.getCodec();
- JsonNode node = oc.readTree(p);
- String v = node.asText();
- RangeOfValues result = new RangeOfValues(v);
- return result;
+ public void visit(TopiaEntity entity, String propertyName, Class<?> collectionType, Class<?> type, int index, Object value) {
+ throw new UnsupportedOperationException("FIXME a faire, implantation des properties array");
}
+
+ @Override
+ public void clear() {
+ }
}
+
+
+//
+// private static class MatrixNDJsonDeserializer extends JsonDeserializer<MatrixND> {
+//
+// protected RegionImportJson importer;
+//
+// public MatrixNDJsonDeserializer(final RegionImportJson importer) {
+// this.importer = importer;
+// }
+//
+// @Override
+// public MatrixND deserialize(JsonParser p, final DeserializationContext ctxt) throws IOException, JsonProcessingException {
+// ObjectCodec oc = p.getCodec();
+// JsonNode node = oc.readTree(p);
+// String mat = node.asText();
+//
+// MatrixCSVHelper matrixCSVHelper = new MatrixCSVHelper(new EntitySemanticsDecorator(
+// new EntitySemanticsDecorator.EntityProvider() {
+// @Override
+// public Object findById(String id) {
+// Object result = importer.getEntity(id, ctxt);
+// return result;
+// }
+// }));
+//
+// MatrixND result = matrixCSVHelper.readMatrix(mat);
+// return result;
+// }
+//
+// }
+//
+// private static class TopiaEntityJsonDeserializer extends JsonDeserializer<TopiaEntity> {
+//
+// protected RegionImportJson importer;
+//
+// public TopiaEntityJsonDeserializer(RegionImportJson importer) {
+// this.importer = importer;
+// }
+//
+// @Override
+// public TopiaEntity deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
+// ObjectCodec oc = p.getCodec();
+// JsonNode node = oc.readTree(p);
+// String id = node.asText();
+// TopiaEntity result = importer.getEntity(id, ctxt);
+// return result;
+// }
+//
+// }
+//
+// private static class MonthJsonDeserializer extends JsonDeserializer<Month> {
+// @Override
+// public Month deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
+// ObjectCodec oc = p.getCodec();
+// JsonNode node = oc.readTree(p);
+// int m = node.asInt();
+// Month result = Month.MONTH[m];
+// return result;
+// }
+// }
+//
+// private static class TimeUnitJsonDeserializer extends JsonDeserializer<TimeUnit> {
+// @Override
+// public TimeUnit deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
+// ObjectCodec oc = p.getCodec();
+// JsonNode node = oc.readTree(p);
+// int v = node.asInt();
+// TimeUnit result = new TimeUnit(v);
+// return result;
+// }
+// }
+//
+// private static class RangeOfValuesJsonDeserializer extends JsonDeserializer<RangeOfValues> {
+// @Override
+// public RangeOfValues deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
+// ObjectCodec oc = p.getCodec();
+// JsonNode node = oc.readTree(p);
+// String v = node.asText();
+// RangeOfValues result = new RangeOfValues(v);
+// return result;
+// }
+// }
+
}
Modified: trunk/src/main/java/fr/ifremer/isisfish/ui/input/InputHandler.java
===================================================================
--- trunk/src/main/java/fr/ifremer/isisfish/ui/input/InputHandler.java 2016-11-04 15:05:55 UTC (rev 4369)
+++ trunk/src/main/java/fr/ifremer/isisfish/ui/input/InputHandler.java 2016-11-09 15:24:51 UTC (rev 4370)
@@ -440,12 +440,15 @@
new GZIPInputStream(new FileInputStream(file)))) {
RegionImportJson json = new RegionImportJson(new InputStreamReader(in, "UTF-8"),
new RegionImportJson.RegionMergeDatabase(tx));
- Collection<TopiaEntity> entities = json.getEntity();
+ Collection<TopiaEntity> entities = json.getEntities();
log.info("Entities to importe: " + entities.size());
for (TopiaEntity e : entities) {
tx.add(e);
}
tx.commitTransaction();
+ } catch (Exception eee) {
+ tx.rollbackTransaction();
+ throw eee;
}
}
} catch (Exception eee) {
1
0