--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2012 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.db.layer0.migration;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.text.DateFormat;\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.Date;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.binding.mutable.Variant;\r
+import org.simantics.databoard.container.DataContainer;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.WriteOnlyGraph;\r
+import org.simantics.db.common.request.WriteResultRequest;\r
+import org.simantics.db.common.utils.Logger;\r
+import org.simantics.db.exception.AssumptionException;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor;\r
+import org.simantics.db.layer0.internal.SimanticsInternal;\r
+import org.simantics.db.layer0.util.Layer0Utils;\r
+import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest;\r
+import org.simantics.db.layer0.util.TransferableGraphConfiguration2;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.db.service.ManagementSupport;\r
+import org.simantics.db.service.SerialisationSupport;\r
+import org.simantics.graph.db.IImportAdvisor;\r
+import org.simantics.graph.db.IImportAdvisor2;\r
+import org.simantics.graph.db.StreamingTransferableGraphFileReader;\r
+import org.simantics.graph.db.TGStatusMonitor;\r
+import org.simantics.graph.db.TransferableGraphImporter;\r
+import org.simantics.graph.db.TransferableGraphSource;\r
+import org.simantics.graph.db.TransferableGraphs;\r
+import org.simantics.graph.db.WrapperAdvisor;\r
+import org.simantics.graph.representation.ByteFileReader;\r
+import org.simantics.graph.representation.TransferableGraph1;\r
+import org.simantics.graph.representation.TransferableGraphFileReader;\r
+import org.simantics.layer0.Layer0;\r
+\r
+public class MigrationStateImpl implements MigrationState {\r
+\r
+ final private HashMap<String, Object> properties = new HashMap<String, Object>();\r
+ \r
+ @SuppressWarnings("unchecked")\r
+ @Override\r
+ public <T> T probeProperty(String key) throws DatabaseException {\r
+ return (T)properties.get(key);\r
+ }\r
+\r
+ @SuppressWarnings("unchecked")\r
+ @Override\r
+ public <T> T getProperty(String key) throws DatabaseException {\r
+ \r
+ T property = (T)properties.get(key);\r
+ if(property != null) return property;\r
+ \r
+ if(MigrationStateKeys.BASE_URI.equals(key)) {\r
+ throw new IllegalStateException("Base URI needs to be supplied for migration.");\r
+ } else if (MigrationStateKeys.SESSION.equals(key)) {\r
+ throw new IllegalStateException("Session needs to be supplied for migration.");\r
+ } else if (MigrationStateKeys.MODEL_FILE.equals(key)) {\r
+ throw new IllegalStateException("Model file needs to be supplied for migration.");\r
+ } else if (MigrationStateKeys.CURRENT_TG.equals(key)) {\r
+ \r
+ final Resource resource = probeProperty(MigrationStateKeys.CURRENT_RESOURCE);\r
+ final Collection<Resource> roots = probeProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES);\r
+ if(roots != null) {\r
+ Session session = getProperty(MigrationStateKeys.SESSION);\r
+ TransferableGraph1 tg = session.syncRequest(new Read<TransferableGraph1>() {\r
+ @Override\r
+ public TransferableGraph1 perform(ReadGraph graph) throws DatabaseException {\r
+ TransferableGraphConfiguration2 conf = new TransferableGraphConfiguration2(graph, roots, true, false);\r
+ TransferableGraphSource source = graph.syncRequest(new ModelTransferableGraphSourceRequest(conf));\r
+ return TransferableGraphs.create(graph, source);\r
+ }\r
+ });\r
+ if (resource != null)\r
+ MigrationUtils.clearTempResource(session, resource);\r
+ setProperty(MigrationStateKeys.CURRENT_RESOURCE, null);\r
+ setProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES, null);\r
+ setProperty(MigrationStateKeys.DATABASE_REVISION_AFTER_TG_IMPORT, null);\r
+ setProperty(MigrationStateKeys.CURRENT_TG, tg);\r
+ return (T)tg;\r
+ }\r
+ \r
+ TransferableGraphFileReader reader = null;\r
+ try {\r
+ File modelFile = getProperty(MigrationStateKeys.MODEL_FILE);\r
+ reader = new TransferableGraphFileReader(modelFile);\r
+ TransferableGraph1 tg = reader.readTG();\r
+ setProperty(MigrationStateKeys.CURRENT_TG, tg);\r
+ return (T)tg;\r
+ } catch (DatabaseException e) {\r
+ throw e;\r
+ } catch (Throwable t) {\r
+ throw new DatabaseException(t);\r
+ } finally {\r
+ uncheckedClose(reader);\r
+ }\r
+ \r
+ } else if (MigrationStateKeys.CURRENT_TGS.equals(key)) {\r
+\r
+ File modelFile = getProperty(MigrationStateKeys.MODEL_FILE);\r
+ \r
+ try {\r
+ StreamingTransferableGraphFileReader reader = new StreamingTransferableGraphFileReader(modelFile);\r
+ TransferableGraphSource tgs = reader.readTG();\r
+ setProperty(MigrationStateKeys.CURRENT_TGS_READER, reader);\r
+ setProperty(MigrationStateKeys.CURRENT_TGS, tgs);\r
+ return (T)tgs;\r
+ } catch (DatabaseException e) {\r
+ throw e;\r
+ } catch (IOException e) {\r
+ throw new DatabaseException("An I/O exception occurred during reading '" + modelFile.getAbsolutePath() + "'", e);\r
+ } catch (Throwable t) {\r
+ throw new DatabaseException(t);\r
+ }\r
+\r
+ } else if (MigrationStateKeys.CURRENT_DATA_CONTAINER.equals(key)) {\r
+ \r
+ try {\r
+ \r
+ TransferableGraphSource tgs = getProperty(MigrationStateKeys.CURRENT_TGS);\r
+ DataContainer dc = tgs.getHeader();\r
+ setProperty(MigrationStateKeys.CURRENT_DATA_CONTAINER, dc);\r
+ return (T)dc;\r
+ \r
+ } catch (DatabaseException e) {\r
+ throw e;\r
+ } catch (Throwable t) {\r
+ throw new DatabaseException(t);\r
+ }\r
+ \r
+ } else if (MigrationStateKeys.TG_EXTENSIONS.equals(key)) {\r
+ \r
+ try {\r
+ \r
+ TransferableGraphSource tgs = getProperty(MigrationStateKeys.CURRENT_TGS);\r
+ Map<String,Variant> extensions = tgs.getExtensions();\r
+ setProperty(MigrationStateKeys.TG_EXTENSIONS, extensions);\r
+ return (T)extensions;\r
+ \r
+ } catch (DatabaseException e) {\r
+ throw e;\r
+ } catch (Throwable t) {\r
+ throw new DatabaseException(t);\r
+ }\r
+\r
+ } else if (MigrationStateKeys.CURRENT_RESOURCE.equals(key) || MigrationStateKeys.CURRENT_ROOT_RESOURCES.equals(key)) {\r
+ \r
+ final Session session = getProperty(MigrationStateKeys.SESSION);\r
+ final IProgressMonitor monitor = probeProperty(MigrationStateKeys.PROGRESS_MONITOR);\r
+ final boolean updateDependencies = MigrationUtils.getProperty(this, MigrationStateKeys.UPDATE_DEPENDENCIES, Boolean.TRUE);\r
+ \r
+ final TransferableGraph1 tg = probeProperty(MigrationStateKeys.CURRENT_TG);\r
+ if(tg != null) {\r
+ \r
+ final Resource indexRoot = session.syncRequest(new WriteResultRequest<Resource>() {\r
+ @Override\r
+ public Resource perform(WriteGraph graph) throws DatabaseException {\r
+ if(!updateDependencies)\r
+ Layer0Utils.setDependenciesIndexingDisabled(graph, true);\r
+ return createTemporaryRoot(graph);\r
+ }\r
+ });\r
+\r
+ IImportAdvisor baseAdvisor = MigrationUtils.getProperty(this, MigrationStateKeys.IMPORT_ADVISOR, new DefaultPasteImportAdvisor(indexRoot));\r
+ IImportAdvisor2 advisor = new WrapperAdvisor(baseAdvisor) {\r
+ @Override\r
+ public void beforeWrite(WriteOnlyGraph graph, TransferableGraphImporter process) throws DatabaseException {\r
+ super.beforeWrite(graph, process);\r
+ if(!updateDependencies)\r
+ Layer0Utils.setDependenciesIndexingDisabled(graph, true);\r
+ }\r
+ @Override\r
+ public void afterWrite(WriteOnlyGraph graph, TransferableGraphImporter process) throws DatabaseException {\r
+ super.afterWrite(graph, process);\r
+ Boolean storeResources = probeProperty(MigrationStateKeys.GET_RESOURCE_IDS);\r
+ if(storeResources != null && storeResources) {\r
+ long[] ids = process.getResourceIds(session.getService(SerialisationSupport.class));\r
+ setProperty(MigrationStateKeys.RESOURCE_IDS, ids);\r
+ }\r
+ }\r
+ };\r
+ // Make sure that the supplied advisor is redirected to temp\r
+ advisor.redirect(indexRoot);\r
+ \r
+ TransferableGraphs.importGraph1WithMonitor(session, tg, advisor, new TGStatusMonitor() {\r
+ @Override\r
+ public void status(int percentage) {\r
+ monitor.subTask("Importing model from file (" + percentage + "%)");\r
+ }\r
+ @Override\r
+ public boolean isCanceled() {\r
+ return monitor.isCanceled();\r
+ }\r
+ });\r
+ \r
+ setProperty(MigrationStateKeys.CURRENT_RESOURCE, indexRoot);\r
+ setProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES, new ArrayList<>(advisor.getRoots()));\r
+ setProperty(MigrationStateKeys.DATABASE_REVISION_AFTER_TG_IMPORT, session.getService(ManagementSupport.class).getHeadRevisionId());\r
+ setProperty(MigrationStateKeys.CURRENT_TG, null);\r
+\r
+ return getProperty(key);\r
+ }\r
+\r
+ final TransferableGraphSource tgs = getProperty(MigrationStateKeys.CURRENT_TGS);\r
+ if(tgs != null) {\r
+\r
+ final Resource indexRoot = session.syncRequest(new WriteResultRequest<Resource>() {\r
+ @Override\r
+ public Resource perform(WriteGraph graph) throws DatabaseException {\r
+ if(!updateDependencies)\r
+ Layer0Utils.setDependenciesIndexingDisabled(graph, true);\r
+ return createTemporaryRoot(graph);\r
+ }\r
+ });\r
+\r
+ IImportAdvisor baseAdvisor = MigrationUtils.getProperty(this, MigrationStateKeys.IMPORT_ADVISOR, new DefaultPasteImportAdvisor(indexRoot));\r
+ IImportAdvisor2 advisor = new WrapperAdvisor(baseAdvisor) {\r
+ @Override\r
+ public Resource getTarget() {\r
+ return indexRoot;\r
+ }\r
+ @Override\r
+ public void beforeWrite(WriteOnlyGraph graph, TransferableGraphImporter process) throws DatabaseException {\r
+ super.beforeWrite(graph, process);\r
+ if(!updateDependencies)\r
+ Layer0Utils.setDependenciesIndexingDisabled(graph, true);\r
+ }\r
+ @Override\r
+ public void afterWrite(WriteOnlyGraph graph, TransferableGraphImporter process) throws DatabaseException {\r
+ super.afterWrite(graph, process);\r
+ Boolean storeResources = probeProperty(MigrationStateKeys.GET_RESOURCE_IDS);\r
+ if(storeResources != null && storeResources) {\r
+ long[] ids = process.getResourceIds(session.getService(SerialisationSupport.class));\r
+ setProperty(MigrationStateKeys.RESOURCE_IDS, ids);\r
+ }\r
+ }\r
+ };\r
+ // Make sure that the supplied advisor is redirected to temp\r
+ advisor.redirect(indexRoot);\r
+ \r
+ TransferableGraphs.importGraph1(session, tgs, advisor, new TGStatusMonitor() {\r
+ @Override\r
+ public void status(int percentage) {\r
+ monitor.subTask("Importing model from file (" + percentage + "%)");\r
+ }\r
+ @Override\r
+ public boolean isCanceled() {\r
+ return monitor.isCanceled();\r
+ }\r
+ });\r
+\r
+ setProperty(MigrationStateKeys.CURRENT_RESOURCE, indexRoot);\r
+ setProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES, new ArrayList<>(advisor.getRoots()));\r
+ setProperty(MigrationStateKeys.DATABASE_REVISION_AFTER_TG_IMPORT, session.getService(ManagementSupport.class).getHeadRevisionId());\r
+ setProperty(MigrationStateKeys.CURRENT_TG, null);\r
+\r
+ return getProperty(key);\r
+\r
+ }\r
+\r
+ } else if (MigrationStateKeys.UPDATE_DEPENDENCIES.equals(key)) {\r
+\r
+ return null;\r
+\r
+ }\r
+\r
+ return null;\r
+\r
+ }\r
+\r
+ @Override\r
+ public <T> void setProperty(String key, T value) {\r
+ properties.put(key, value);\r
+ }\r
+\r
+ @Override\r
+ public void dispose() {\r
+ // Close all possible open file handles\r
+ try {\r
+ StreamingTransferableGraphFileReader tgs = probeProperty(MigrationStateKeys.CURRENT_TGS_READER);\r
+ uncheckedClose(tgs);\r
+ } catch (DatabaseException e) {\r
+ Logger.defaultLogError(e);\r
+ }\r
+ }\r
+\r
+ private static void uncheckedClose(ByteFileReader closeable) {\r
+ try {\r
+ if (closeable != null)\r
+ closeable.close();\r
+ } catch (IOException e) {\r
+ //ignore\r
+ }\r
+ }\r
+\r
+ private Resource createTemporaryRoot(WriteGraph graph) throws DatabaseException {\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ Resource project = SimanticsInternal.getProject();\r
+ Resource root = graph.getPossibleObject(project, L0.PartOf);\r
+ Resource temp = Layer0Utils.getPossibleChild(graph, root, "Temp");\r
+ if (temp == null) \r
+ throw new AssumptionException("Temporary folder 'Temp' not found under " + graph.getPossibleURI(root));\r
+\r
+ Resource indexRoot = graph.newResource();\r
+ String indexRootName = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(new Date());\r
+ graph.claim(indexRoot, L0.InstanceOf, L0.IndexRoot);\r
+ graph.addLiteral(indexRoot, L0.HasName, L0.String, indexRootName, Bindings.STRING);\r
+ graph.claim(temp, L0.ConsistsOf, indexRoot);\r
+ return indexRoot;\r
+ }\r
+\r
+}\r