Skip to content

Commit

Permalink
Fixes #919
Browse files Browse the repository at this point in the history
  • Loading branch information
aedelmann committed May 7, 2018
1 parent 7ea1ccf commit aa6983c
Show file tree
Hide file tree
Showing 7 changed files with 261 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -128,7 +127,7 @@ public List<ModelInfo> 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()));
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}


}
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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<IUpgradeTask> 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);
}

}
Original file line number Diff line number Diff line change
@@ -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.";
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}

0 comments on commit aa6983c

Please sign in to comment.