Skip to content

Commit

Permalink
Added checks to prevent duplicateresources from being created
Browse files Browse the repository at this point in the history
Signed-off-by: shiv12095 <shiv12095@iiitd.ac.in>
  • Loading branch information
shiv12095 committed Aug 15, 2016
1 parent 5df90ac commit b71e257
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 42 deletions.
Expand Up @@ -8,6 +8,7 @@
import org.eclipse.vorto.editor.web.resource.WebEditorResourceSetProvider;
import org.eclipse.vorto.server.devtool.exception.ProjectAlreadyExistsException;
import org.eclipse.vorto.server.devtool.exception.ProjectNotFoundException;
import org.eclipse.vorto.server.devtool.exception.ProjectResourceAlreadyExistsException;
import org.eclipse.vorto.server.devtool.models.Project;
import org.eclipse.vorto.server.devtool.models.ProjectResource;
import org.eclipse.vorto.server.devtool.service.IProjectRepositoryService;
Expand Down Expand Up @@ -48,6 +49,12 @@ public void handleProjectNotFoundException() {
@ExceptionHandler(ProjectAlreadyExistsException.class)
public void handleProjectAlreadyExistsException() {

}

@ResponseStatus(value = HttpStatus.BAD_REQUEST, reason = "Resource already exists")
@ExceptionHandler(ProjectResourceAlreadyExistsException.class)
public void handleProjectResourceAlreadyExistsException() {

}

@ApiOperation(value = "Checks whether a Vorto project already exists")
Expand All @@ -63,6 +70,30 @@ public void checkProjectExists(

projectRepositoryService.checkProjectExists(sessionId, projectName);
}

@ApiOperation(value = "Checks whether a resource exists within the Vorto project")
@RequestMapping(value = "/{projectName}/resources/check/{namespace}/{name}/{version:.+}", method = RequestMethod.GET)
public void checkResourceExists(
@ApiParam(value = "ProjectName", required = true) final @PathVariable String projectName,
@ApiParam(value = "Namespace", required = true) final @PathVariable String namespace,
@ApiParam(value = "Name", required = true) final @PathVariable String name,
@ApiParam(value = "Version", required = true) final @PathVariable String version,
@ApiParam(value = "Request", required = true) final HttpServletRequest request)
throws ProjectNotFoundException, ProjectAlreadyExistsException, ProjectResourceAlreadyExistsException {

Objects.requireNonNull(projectName, "projectName must not be null");
Objects.requireNonNull(namespace, "namespace must not be null");
Objects.requireNonNull(name, "namespace must not be null");
Objects.requireNonNull(version, "namespace must not be null");
String sessionId = request.getSession().getId();

ProjectResource projectResource = new ProjectResource();
projectResource.setName(name);
projectResource.setNamespace(namespace);
projectResource.setVersion(version);

projectRepositoryService.checkResourceExists(sessionId, projectName, projectResource);
}

@ApiOperation(value = "Creates a new Vorto project")
@RequestMapping(method = RequestMethod.POST)
Expand Down Expand Up @@ -112,17 +143,27 @@ public ArrayList<Project> getProjects(final HttpServletRequest request) {
}

@ApiOperation(value = "Deletes the resource in the Vorto project")
@RequestMapping(value = "/{projectName}/resources/delete/{resourceId}/", method = RequestMethod.GET)
@RequestMapping(value = "/{projectName}/resources/delete/{namespace}/{name}/{version:.+}", method = RequestMethod.GET)
public void deleteProjectResource(
@ApiParam(value = "ProjectName", required = true) final @PathVariable String projectName,
@ApiParam(value = "ResourceId", required = true) final @PathVariable String resourceId,
@ApiParam(value = "Namespace", required = true) final @PathVariable String namespace,
@ApiParam(value = "Name", required = true) final @PathVariable String name,
@ApiParam(value = "Version", required = true) final @PathVariable String version,
@ApiParam(value = "Request", required = true) final HttpServletRequest request)
throws ProjectNotFoundException {

Objects.requireNonNull(projectName, "projectName must not be null");
Objects.requireNonNull(resourceId, "resourceId must not be null");
Objects.requireNonNull(namespace, "namespace must not be null");
Objects.requireNonNull(name, "namespace must not be null");
Objects.requireNonNull(version, "namespace must not be null");
String sessionId = request.getSession().getId();
projectRepositoryService.deleteResource(sessionId, projectName, resourceId);

ProjectResource projectResource = new ProjectResource();
projectResource.setName(name);
projectResource.setNamespace(namespace);
projectResource.setVersion(version);

projectRepositoryService.deleteResource(sessionId, projectName, projectResource);
}

@ApiOperation(value = "Creates a new resource in the Vorto project")
Expand All @@ -131,7 +172,7 @@ public void createProjectResource(
@RequestBody ProjectResource projectResoure,
@ApiParam(value = "ProjectName", required = true) final @PathVariable String projectName,
@ApiParam(value = "Request", required = true) final HttpServletRequest request)
throws ProjectNotFoundException {
throws ProjectNotFoundException, ProjectResourceAlreadyExistsException {

Objects.requireNonNull(projectName, "projectName must not be null");
String sessionId = request.getSession().getId();
Expand Down
@@ -0,0 +1,5 @@
package org.eclipse.vorto.server.devtool.exception;

public class ProjectResourceAlreadyExistsException extends Exception{

}
Expand Up @@ -4,13 +4,16 @@

import org.eclipse.vorto.server.devtool.exception.ProjectAlreadyExistsException;
import org.eclipse.vorto.server.devtool.exception.ProjectNotFoundException;
import org.eclipse.vorto.server.devtool.exception.ProjectResourceAlreadyExistsException;
import org.eclipse.vorto.server.devtool.models.Project;
import org.eclipse.vorto.server.devtool.models.ProjectResource;

public interface IProjectRepositoryService {

void checkProjectExists(String sessionId, String projectName) throws ProjectAlreadyExistsException;

void checkResourceExists(String sessionId, String projectName, ProjectResource projectResource) throws ProjectAlreadyExistsException, ProjectResourceAlreadyExistsException, ProjectNotFoundException;

Project createProject(String sessionId, String projectName) throws ProjectAlreadyExistsException;

Project openProject(String sessionId, String projectName) throws ProjectNotFoundException;
Expand All @@ -19,7 +22,7 @@ public interface IProjectRepositoryService {

ArrayList<ProjectResource> getProjectResources(String sessionId, String projectName) throws ProjectNotFoundException;

void createResource(String sessionId, String projectName, ProjectResource projectResource) throws ProjectNotFoundException;
void createResource(String sessionId, String projectName, ProjectResource projectResource) throws ProjectNotFoundException, ProjectResourceAlreadyExistsException;

void deleteResource(String sessionId, String projectName, String resourceId) throws ProjectNotFoundException;
void deleteResource(String sessionId, String projectName, ProjectResource projectResource) throws ProjectNotFoundException;
}
Expand Up @@ -9,6 +9,7 @@
import org.eclipse.vorto.editor.web.resource.WebEditorResourceSetProvider;
import org.eclipse.vorto.server.devtool.exception.ProjectAlreadyExistsException;
import org.eclipse.vorto.server.devtool.exception.ProjectNotFoundException;
import org.eclipse.vorto.server.devtool.exception.ProjectResourceAlreadyExistsException;
import org.eclipse.vorto.server.devtool.models.Project;
import org.eclipse.vorto.server.devtool.models.ProjectResource;
import org.eclipse.vorto.server.devtool.service.IProjectRepositoryService;
Expand All @@ -34,7 +35,16 @@ public void checkProjectExists(String sessionId, String projectName) throws Proj
throw new ProjectAlreadyExistsException();
}
}


@Override
public void checkResourceExists(String sessionId, String projectName, ProjectResource projectResource)
throws ProjectAlreadyExistsException, ProjectResourceAlreadyExistsException, ProjectNotFoundException {
Project project = openProject(sessionId, projectName);
if(project.getResourceList().contains(projectResource)){
throw new ProjectResourceAlreadyExistsException();
}
}

@Override
public Project createProject(String sessionId, String projectName) throws ProjectAlreadyExistsException {
WebEditorResourceSetProvider webEditorResourceSetProvider = (WebEditorResourceSetProvider) injector.getInstance(IWebResourceSetProvider.class);
Expand Down Expand Up @@ -77,21 +87,37 @@ public ArrayList<Project> getProjects(String sessionId) {
}

@Override
public void createResource(String sessionId, String projectName, ProjectResource projectResource) throws ProjectNotFoundException {
public void createResource(String sessionId, String projectName, ProjectResource projectResource) throws ProjectNotFoundException, ProjectResourceAlreadyExistsException {
Project project = openProject(sessionId, projectName);
ArrayList<ProjectResource> resourceList = project.getResourceList();
if(resourceList.contains(projectResource)){
throw new ProjectResourceAlreadyExistsException();
}
resourceList.add(projectResource);
}

@Override
public void deleteResource(String sessionId, String projectName, String resourceId) throws ProjectNotFoundException {
public void deleteResource(String sessionId, String projectName, ProjectResource projectResource) throws ProjectNotFoundException {
Project project = openProject(sessionId, projectName) ;
URI uri = URI.createURI(resourceId);
ResourceSet resourceSet = project.getResourceSet();
Resource resource = resourceSet.getResource(uri, true);
resourceSet.getResources().remove(resource);
ProjectResource projectResource = new ProjectResource();
projectResource.setResourceId(resourceId);
project.getResourceList().remove(projectResource);
ProjectResource pResource = getProjectResource(projectResource, project.getResourceList());
if(pResource != null){
String resourceId = pResource.getResourceId();
URI uri = URI.createURI(resourceId);
ResourceSet resourceSet = project.getResourceSet();
Resource resource = resourceSet.getResource(uri, true);
resourceSet.getResources().remove(resource);
projectResource.setResourceId(resourceId);
project.getResourceList().remove(projectResource);
}
}

private ProjectResource getProjectResource(ProjectResource projectResource, ArrayList<ProjectResource> resourceList){
for(ProjectResource pResource : resourceList){
if(pResource.equals(projectResource)){
return pResource;
}
}
return null;
}

}
57 changes: 32 additions & 25 deletions server/devtool/src/main/resources/static/dist/js/controllers.js
Expand Up @@ -65,7 +65,7 @@ define(["angular"], function(angular) {
});

$scope.$on("deleteEditor", function(event, tab) {
var url = './project/' + $scope.projectName + '/resources/delete/' + tab.resourceId + '/';
var url = './project/' + $scope.projectName + '/resources/delete/' + tab.namespace + '/' + tab.name + '/' + tab.version;
$http.get(url).success(
function(data, status, headers, config) {
$scope.deleteTab(tab.index);
Expand Down Expand Up @@ -151,28 +151,33 @@ define(["angular"], function(angular) {
}

$scope.addEditor = function(model) {
$scope.counter++;
var tabId = $scope.counter;
var editorParentDivId = "xtext-editor-parent-" + tabId;
var editorDivId = "xtext-editor-" + tabId;
var tab = {
id: tabId,
editorParentDivId: editorParentDivId,
editorDivId: editorDivId,
language: model.language,
name: model.name,
};
$scope.tabs.push(tab);
$scope.selectedTabIndex = $scope.tabs.length - 1;
$scope.selectedTabId = $scope.tabs[$scope.selectedTabIndex]['id'];
var element = angular.element(document).find('#editors');
element.append('<div id="' + editorParentDivId + '" ng-show="selectedTabId==' + tabId + '"><div id="' + editorDivId + '" class="custom-xtext-editor"></div></div>');
$compile(element.contents())($scope);
if (model.language == 'infomodel') {
$scope.addInfoModelEditor(editorDivId, model);
} else if (model.language == 'fbmodel') {
$scope.addFunctionBlockEditor(editorDivId, model);
}
$http.get('./project/' + $scope.projectName + '/resources/check/' + model.namespace + '/' + model.name + '/' + model.version).success(
function(data, status, headers, config) {
$scope.counter++;
var tabId = $scope.counter;
var editorParentDivId = "xtext-editor-parent-" + tabId;
var editorDivId = "xtext-editor-" + tabId;
var tab = {
id: tabId,
editorParentDivId: editorParentDivId,
editorDivId: editorDivId,
language: model.language,
name: model.name,
};
$scope.tabs.push(tab);
$scope.selectedTabIndex = $scope.tabs.length - 1;
$scope.selectedTabId = $scope.tabs[$scope.selectedTabIndex]['id'];
var element = angular.element(document).find('#editors');
element.append('<div id="' + editorParentDivId + '" ng-show="selectedTabId==' + tabId + '"><div id="' + editorDivId + '" class="custom-xtext-editor"></div></div>');
$compile(element.contents())($scope);
if (model.language == 'infomodel') {
$scope.addInfoModelEditor(editorDivId, model);
} else if (model.language == 'fbmodel') {
$scope.addFunctionBlockEditor(editorDivId, model);
}
}).error(function(data, status, headers, config) {
window.alert('File already exists')
});
}

$scope.generateInfoModelContent = function(model) {
Expand Down Expand Up @@ -221,7 +226,9 @@ define(["angular"], function(angular) {
"version": model.version,
"namespace": model.namespace
}).success(
function(data, status, headers, config) {}).error(function(data, status, headers, config) {});
function(data, status, headers, config) {}).error(function(data, status, headers, config) {
window.alert('File already exists')
});
});
});
}
Expand All @@ -247,7 +254,7 @@ define(["angular"], function(angular) {
tab['resourceId'] = resourceId;
tab['name'] = model.name;
tab['version'] = model.version;
tab['namespace'] = model.namespace;
tab['namespace'] = model.namespace;
$http.post('./project/' + $scope.projectName + '/resources/create', {
"name": model.name,
"resourceId": resourceId,
Expand Down

0 comments on commit b71e257

Please sign in to comment.