From aa6983c2dcb1cdf697f1aba98e9ea0512040620b Mon Sep 17 00:00:00 2001 From: aedelmann Date: Mon, 7 May 2018 23:07:48 +0800 Subject: [PATCH] Fixes #919 --- .../core/impl/JcrModelRepository.java | 30 +++++--- .../upgrade/AbstractUpgradeTask.java | 43 ++++++++++++ .../repository/upgrade/IUpgradeService.java | 20 ++++++ .../repository/upgrade/IUpgradeTask.java | 45 ++++++++++++ .../upgrade/impl/DefaultUpgradeService.java | 49 +++++++++++++ .../repository/upgrade/UpgradeTaskTest.java | 70 +++++++++++++++++++ .../web/config/RepositoryConfiguration.java | 12 ++++ 7 files changed, 261 insertions(+), 8 deletions(-) create mode 100644 server/repo/repository-server/src/main/java/org/eclipse/vorto/repository/upgrade/AbstractUpgradeTask.java create mode 100644 server/repo/repository-server/src/main/java/org/eclipse/vorto/repository/upgrade/IUpgradeService.java create mode 100644 server/repo/repository-server/src/main/java/org/eclipse/vorto/repository/upgrade/IUpgradeTask.java create mode 100644 server/repo/repository-server/src/main/java/org/eclipse/vorto/repository/upgrade/impl/DefaultUpgradeService.java create mode 100644 server/repo/repository-server/src/test/java/org/eclipse/vorto/repository/upgrade/UpgradeTaskTest.java diff --git a/server/repo/repository-server/src/main/java/org/eclipse/vorto/repository/core/impl/JcrModelRepository.java b/server/repo/repository-server/src/main/java/org/eclipse/vorto/repository/core/impl/JcrModelRepository.java index 78511d9b02..2feebd29d3 100644 --- a/server/repo/repository-server/src/main/java/org/eclipse/vorto/repository/core/impl/JcrModelRepository.java +++ b/server/repo/repository-server/src/main/java/org/eclipse/vorto/repository/core/impl/JcrModelRepository.java @@ -21,7 +21,6 @@ import java.util.Iterator; import java.util.LinkedList; import java.util.List; -import java.util.Optional; import java.util.UUID; import javax.annotation.PostConstruct; @@ -128,7 +127,7 @@ public List search(final String expression) { throw new RuntimeException("Could not create query manager", e); } } - + private ModelInfo createMinimalModelInfo(Node node) throws RepositoryException { ModelInfo resource = new ModelInfo(ModelIdHelper.fromPath(node.getParent().getPath()), ModelType.valueOf(node.getProperty("vorto:type").getString())); @@ -138,12 +137,12 @@ private ModelInfo createMinimalModelInfo(Node node) throws RepositoryException { if (node.hasProperty("vorto:author")) { resource.setAuthor(node.getProperty("vorto:author").getString()); } - + NodeIterator imageNodeIterator = node.getParent().getNodes("img.png*"); if (imageNodeIterator.hasNext()) { resource.setHasImage(true); } - + return resource; } @@ -176,7 +175,7 @@ private ModelInfo createModelResource(Node node) throws RepositoryException { Node referencedByFileNode = prop.getParent(); final ModelId referencedById = ModelIdHelper.fromPath(referencedByFileNode.getParent().getPath()); resource.getReferencedBy().add(referencedById); - + if (referencedByFileNode.getName().endsWith(ModelType.Mapping.getExtension())) { ModelEMFResource emfResource = getEMFResource(referencedById); resource.addPlatformMapping(emfResource.getTargetPlatform(), referencedById); @@ -377,7 +376,7 @@ private boolean isTargetPlatformMapping(ModelInfo model, String targetPlatform) } } - private ModelEMFResource getEMFResource(ModelId modelId) { + public ModelEMFResource getEMFResource(ModelId modelId) { try { ModelIdHelper modelIdHelper = new ModelIdHelper(modelId); @@ -462,15 +461,30 @@ public ModelInfo updateMeta(ModelInfo model) { Node fileNode = folderNode.getNodes("*.type | *.fbmodel | *.infomodel | *.mapping").hasNext() ? folderNode.getNodes("*.type | *.fbmodel | *.infomodel | *.mapping").nextNode() : null; fileNode.setProperty("vorto:author", model.getAuthor()); - + session.save(); - + return model; } catch (RepositoryException e) { throw new FatalModelRepositoryException("Problem occured removing the model", e); } } + public void saveModel(ModelEMFResource resource) { + try { + Node folderNode = createNodeForModelId(resource.getId()); + Node fileNode = folderNode.getNodes("*.type | *.fbmodel | *.infomodel | *.mapping").hasNext() + ? folderNode.getNodes("*.type | *.fbmodel | *.infomodel | *.mapping").nextNode() : null; + Node contentNode = fileNode.getNode("jcr:content"); + Binary binary = session.getValueFactory() + .createBinary(new ByteArrayInputStream(resource.toDSL())); + contentNode.setProperty("jcr:data", binary); + session.save(); + } catch (Exception e) { + throw new FatalModelRepositoryException("Problem occured removing the model", e); + } + } + public ModelSearchUtil getModelSearchUtil() { return modelSearchUtil; } diff --git a/server/repo/repository-server/src/main/java/org/eclipse/vorto/repository/upgrade/AbstractUpgradeTask.java b/server/repo/repository-server/src/main/java/org/eclipse/vorto/repository/upgrade/AbstractUpgradeTask.java new file mode 100644 index 0000000000..78dc5fe625 --- /dev/null +++ b/server/repo/repository-server/src/main/java/org/eclipse/vorto/repository/upgrade/AbstractUpgradeTask.java @@ -0,0 +1,43 @@ +/** + * Copyright (c) 2015-2018 Bosch Software Innovations GmbH and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * The Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Bosch Software Innovations GmbH - Please refer to git log + */ +package org.eclipse.vorto.repository.upgrade; + +import org.eclipse.vorto.repository.api.ModelId; +import org.eclipse.vorto.repository.core.IModelRepository; +import org.eclipse.vorto.repository.core.impl.JcrModelRepository; +import org.eclipse.vorto.repository.core.impl.ModelEMFResource; + +public abstract class AbstractUpgradeTask implements IUpgradeTask { + + private IModelRepository modelRepository; + + public AbstractUpgradeTask(IModelRepository repository) { + this.modelRepository = repository; + } + + protected IModelRepository getModelRepository() { + return modelRepository; + } + + protected ModelEMFResource getModel(ModelId modelId) { + return ((JcrModelRepository)modelRepository).getEMFResource(modelId); + } + + protected void saveModel(ModelEMFResource resource) { + ((JcrModelRepository)modelRepository).saveModel(resource); + } + + +} diff --git a/server/repo/repository-server/src/main/java/org/eclipse/vorto/repository/upgrade/IUpgradeService.java b/server/repo/repository-server/src/main/java/org/eclipse/vorto/repository/upgrade/IUpgradeService.java new file mode 100644 index 0000000000..d9663f48d3 --- /dev/null +++ b/server/repo/repository-server/src/main/java/org/eclipse/vorto/repository/upgrade/IUpgradeService.java @@ -0,0 +1,20 @@ +/** + * Copyright (c) 2015-2018 Bosch Software Innovations GmbH and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * The Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Bosch Software Innovations GmbH - Please refer to git log + */ +package org.eclipse.vorto.repository.upgrade; + +public interface IUpgradeService { + + void installUpgrades(); +} diff --git a/server/repo/repository-server/src/main/java/org/eclipse/vorto/repository/upgrade/IUpgradeTask.java b/server/repo/repository-server/src/main/java/org/eclipse/vorto/repository/upgrade/IUpgradeTask.java new file mode 100644 index 0000000000..d47793069f --- /dev/null +++ b/server/repo/repository-server/src/main/java/org/eclipse/vorto/repository/upgrade/IUpgradeTask.java @@ -0,0 +1,45 @@ +/** + * Copyright (c) 2015-2018 Bosch Software Innovations GmbH and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * The Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Bosch Software Innovations GmbH - Please refer to git log + */ +package org.eclipse.vorto.repository.upgrade; + +/** + * An UpgradeTask is executed when the repository is started up. + * + */ +public interface IUpgradeTask { + + /** + * Performs the actual upgrade task + * @throws UpgradeProblem + */ + void doUpgrade() throws UpgradeProblem; + + /** + * @return a short description of the task being performed + */ + String getShortDescription(); + + public class UpgradeProblem extends RuntimeException { + + /** + * + */ + private static final long serialVersionUID = 1L; + + public UpgradeProblem(String msg, Throwable t) { + super(msg, t); + } + } +} diff --git a/server/repo/repository-server/src/main/java/org/eclipse/vorto/repository/upgrade/impl/DefaultUpgradeService.java b/server/repo/repository-server/src/main/java/org/eclipse/vorto/repository/upgrade/impl/DefaultUpgradeService.java new file mode 100644 index 0000000000..7da8212477 --- /dev/null +++ b/server/repo/repository-server/src/main/java/org/eclipse/vorto/repository/upgrade/impl/DefaultUpgradeService.java @@ -0,0 +1,49 @@ +/** + * Copyright (c) 2015-2018 Bosch Software Innovations GmbH and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * The Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Bosch Software Innovations GmbH - Please refer to git log + */ +package org.eclipse.vorto.repository.upgrade.impl; + +import java.util.Arrays; +import java.util.List; + +import org.eclipse.vorto.repository.upgrade.IUpgradeService; +import org.eclipse.vorto.repository.upgrade.IUpgradeTask; +import org.eclipse.vorto.repository.upgrade.IUpgradeTask.UpgradeProblem; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class DefaultUpgradeService implements IUpgradeService { + + private List tasks; + + private static final Logger logger = LoggerFactory.getLogger(DefaultUpgradeService.class); + + @Override + public void installUpgrades() { + logger.info("Performing upgrade to the Vorto Repository and its content..."); + for (IUpgradeTask task : tasks) { + logger.info("Executing task - "+task.getShortDescription()); + try { + task.doUpgrade(); + } catch (UpgradeProblem problem ) { + logger.error("Problem executing upgrade task",problem); + } + } + } + + public void addTasks(IUpgradeTask...tasks) { + this.tasks = Arrays.asList(tasks); + } + +} diff --git a/server/repo/repository-server/src/test/java/org/eclipse/vorto/repository/upgrade/UpgradeTaskTest.java b/server/repo/repository-server/src/test/java/org/eclipse/vorto/repository/upgrade/UpgradeTaskTest.java new file mode 100644 index 0000000000..06d48db2e3 --- /dev/null +++ b/server/repo/repository-server/src/test/java/org/eclipse/vorto/repository/upgrade/UpgradeTaskTest.java @@ -0,0 +1,70 @@ +/** + * Copyright (c) 2015-2018 Bosch Software Innovations GmbH and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * The Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Bosch Software Innovations GmbH - Please refer to git log + */ +package org.eclipse.vorto.repository.upgrade; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.eclipse.vorto.repository.AbstractIntegrationTest; +import org.eclipse.vorto.repository.api.ModelInfo; +import org.eclipse.vorto.repository.core.IModelRepository; +import org.eclipse.vorto.repository.core.impl.ModelEMFResource; +import org.eclipse.vorto.repository.upgrade.impl.DefaultUpgradeService; +import org.junit.Test; + +public class UpgradeTaskTest extends AbstractIntegrationTest { + + @Test + public void testUpgradeFileContent() { + checkinModel("Color.type", "alex"); + ModelEMFResource resource = (ModelEMFResource)modelRepository.getEMFResource(modelRepository.search("*").get(0).getId()); + assertNull(resource.getModel().getCategory()); + DefaultUpgradeService service = new DefaultUpgradeService(); + service.addTasks(new AddCategoryUpgradeTask(this.modelRepository)); + + service.installUpgrades(); + + resource = (ModelEMFResource)modelRepository.getEMFResource(modelRepository.search("*").get(0).getId()); + assertEquals("iot",resource.getModel().getCategory()); + + } + + + private class AddCategoryUpgradeTask extends AbstractUpgradeTask { + + public AddCategoryUpgradeTask(IModelRepository repo) { + super(repo); + } + + @Override + public void doUpgrade() throws UpgradeProblem { + for (ModelInfo model : modelRepository.search("*")) { + ModelEMFResource resource = getModel(model.getId()); + if (resource.getModel().getCategory() == null) { + resource.getModel().setCategory("iot"); + modelRepository.saveModel(resource); + } + + } + } + + @Override + public String getShortDescription() { + return "Adding category 'iot' to models without category."; + } + + } + +} diff --git a/server/repo/repository-web/src/main/java/org/eclipse/vorto/repository/web/config/RepositoryConfiguration.java b/server/repo/repository-web/src/main/java/org/eclipse/vorto/repository/web/config/RepositoryConfiguration.java index 48671a1c38..588f5140a8 100644 --- a/server/repo/repository-web/src/main/java/org/eclipse/vorto/repository/web/config/RepositoryConfiguration.java +++ b/server/repo/repository-web/src/main/java/org/eclipse/vorto/repository/web/config/RepositoryConfiguration.java @@ -14,7 +14,10 @@ */ package org.eclipse.vorto.repository.web.config; +import org.eclipse.vorto.repository.core.IModelRepository; import org.eclipse.vorto.repository.sso.boschid.EidpUtils; +import org.eclipse.vorto.repository.upgrade.IUpgradeService; +import org.eclipse.vorto.repository.upgrade.impl.DefaultUpgradeService; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -52,4 +55,13 @@ public AccessTokenProvider accessTokenProvider() { return EidpUtils.accessTokenProvider(); } } + + @Bean + public IUpgradeService upgradeService(IModelRepository modelRepository) { + DefaultUpgradeService upgradeService = new DefaultUpgradeService(); + /* + * ADD UPGRADE TASKS HERE + */ + return upgradeService; + } }