Skip to content

Commit

Permalink
Introducing HasIdInIdOrNameField and HasName
Browse files Browse the repository at this point in the history
Signed-off-by: Oliver Kopp <kopp.dev@gmail.com>
  • Loading branch information
koppor committed Aug 11, 2017
1 parent 4af7815 commit d0fa901
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 67 deletions.
Expand Up @@ -17,7 +17,7 @@
import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

public abstract class HasId extends TExtensibleElements {
public abstract class HasId extends TExtensibleElements implements HasIdInIdOrNameField {

@XmlAttribute(name = "id", required = true)
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
Expand Down
@@ -0,0 +1,42 @@
/*******************************************************************************
* Copyright (c) 2017 University of Stuttgart.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and the Apache License 2.0 which both accompany this distribution,
* and are available at http://www.eclipse.org/legal/epl-v10.html
* and http://www.apache.org/licenses/LICENSE-2.0
*
* Contributors:
* Oliver Kopp - initial API and implementation
*******************************************************************************/
package org.eclipse.winery.model.tosca;

public interface HasIdInIdOrNameField {

/**
* Returns the id
*/
default String getId() {
if (this instanceof HasId) {
return ((HasId) this).getId();
} else {
return ((HasName) this).getName();
}
}

/**
* Sets the id using the given string.
* In case the class implements HasId, the id is set using HasId.setId.
* Otherwise, the name attribute is set
*
* @param id the id to set
*/
default void setId(String id) {
if (this instanceof HasId) {
((HasId) this).setId(id);
} else {
((HasName) this).setName(id);
}
}

}
@@ -0,0 +1,20 @@
/*******************************************************************************
* Copyright (c) 2017 University of Stuttgart.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and the Apache License 2.0 which both accompany this distribution,
* and are available at http://www.eclipse.org/legal/epl-v10.html
* and http://www.apache.org/licenses/LICENSE-2.0
*
* Contributors:
* Oliver Kopp - initial API and implementation
*******************************************************************************/
package org.eclipse.winery.model.tosca;

public interface HasName extends HasIdInIdOrNameField {

String getName();

void setName(String value);

}
Expand Up @@ -13,13 +13,12 @@
package org.eclipse.winery.repository.rest.resources.entitytemplates.policytemplates;

import javax.ws.rs.core.Response;
import javax.xml.namespace.QName;

import org.eclipse.winery.common.ids.definitions.PolicyTemplateId;
import org.eclipse.winery.common.ids.definitions.TOSCAComponentId;
import org.eclipse.winery.model.tosca.TExtensibleElements;
import org.eclipse.winery.model.tosca.TPolicyTemplate;
import org.eclipse.winery.repository.backend.BackendUtils;
import org.eclipse.winery.repository.rest.Utils;
import org.eclipse.winery.repository.rest.resources.AbstractComponentInstanceResource;
import org.eclipse.winery.repository.rest.resources.IHasName;
import org.eclipse.winery.repository.rest.resources.entitytemplates.IEntityTemplateResource;
Expand Down Expand Up @@ -52,23 +51,6 @@ protected TExtensibleElements createNewElement() {
return new TPolicyTemplate();
}

@Override
public QName getType() {
return this.getPolicyTemplate().getType();
}

@Override
public Response setType(QName type) {
this.getPolicyTemplate().setType(type);
return Utils.persist(this);
}

@Override
public Response setType(String typeStr) {
this.getPolicyTemplate().setType(QName.valueOf(typeStr));
return Utils.persist(this);
}

@Override
public PropertiesResource getPropertiesResource() {
return new PropertiesResource(this.getPolicyTemplate(), this);
Expand Down
Expand Up @@ -86,20 +86,4 @@ public void copyIdToFields(TOSCAComponentId id) {
this.getNTI().setName(id.getXmlId().getDecoded());
}

@Override
public QName getType() {
return this.getNTI().getNodeType();
}

@Override
public Response setType(QName type) {
this.getNTI().setNodeType(type);
return Utils.persist(this);
}

@Override
public Response setType(String typeStr) {
QName type = QName.valueOf(typeStr);
return this.setType(type);
}
}
Expand Up @@ -13,15 +13,12 @@
package org.eclipse.winery.repository.rest.resources.entitytypeimplementations.relationshiptypeimplementations;

import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import javax.xml.namespace.QName;

import org.eclipse.winery.common.ids.definitions.RelationshipTypeImplementationId;
import org.eclipse.winery.common.ids.definitions.TOSCAComponentId;
import org.eclipse.winery.model.tosca.TExtensibleElements;
import org.eclipse.winery.model.tosca.TImplementationArtifacts;
import org.eclipse.winery.model.tosca.TRelationshipTypeImplementation;
import org.eclipse.winery.repository.backend.BackendUtils;
import org.eclipse.winery.repository.rest.resources.INodeTypeImplementationResourceOrRelationshipTypeImplementationResource;
import org.eclipse.winery.repository.rest.resources.artifacts.ImplementationArtifactsResource;
import org.eclipse.winery.repository.rest.resources.entitytypeimplementations.EntityTypeImplementationResource;
Expand Down Expand Up @@ -63,20 +60,4 @@ public void copyIdToFields(TOSCAComponentId id) {
this.getRTI().setName(id.getXmlId().getDecoded());
}

@Override
public QName getType() {
return this.getRTI().getRelationshipType();
}

@Override
public Response setType(QName type) {
this.getRTI().setRelationshipType(type);
return Utils.persist(this);
}

@Override
public Response setType(String typeStr) {
QName qname = QName.valueOf(typeStr);
return this.setType(qname);
}
}
Expand Up @@ -19,19 +19,16 @@

import javax.xml.bind.Unmarshaller;

import jdk.nashorn.internal.runtime.regexp.joni.constants.OPCode;
import org.apache.tika.mime.MediaType;

import org.eclipse.winery.common.RepositoryFileReference;
import org.eclipse.winery.common.ids.GenericId;
import org.eclipse.winery.common.ids.definitions.TOSCAComponentId;
import org.eclipse.winery.model.tosca.Definitions;
import org.eclipse.winery.model.tosca.TDefinitions;
import org.eclipse.winery.repository.Constants;
import org.eclipse.winery.repository.JAXBSupport;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.io.IOUtils;
import org.apache.tika.mime.MediaType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -54,8 +51,6 @@ private RepositoryFileReference getMimeFileRef(RepositoryFileReference ref) {
}

/**
* {@inheritDoc}
*
* This is a simple implementation using the information put by
* setMimeType(RepositoryFileReference ref) or determining the mime type
* using Utils.getMimeType. If the latter is done, the mime type is
Expand All @@ -72,15 +67,18 @@ public String getMimeType(RepositoryFileReference ref) throws IOException {
} else {
// repository has been manipulated manually,
// create mimetype information
MediaType mediaType;
try (InputStream is = this.newInputStream(ref);
BufferedInputStream bis = new BufferedInputStream(is)) {
mimeType = BackendUtils.getMimeType(bis, ref.getFileName());
mediaType = BackendUtils.getMimeType(bis, ref.getFileName());
}
if (mimeType != null) {
if (mediaType != null) {
// successful execution
this.setMimeType(ref, MediaType.parse(mimeType));
this.setMimeType(ref, mediaType);
mimeType = mediaType.toString();
} else {
AbstractRepository.LOGGER.debug("Could not determine mimetype");
mimeType = null;
}
}
return mimeType;
Expand Down
Expand Up @@ -241,8 +241,7 @@ public void rename(TOSCAComponentId oldId, TOSCAComponentId newId) throws IOExce
return;
}

AbstractComponentInstanceResource componentInstanceResource = AbstractComponentsResource.getComponentInstaceResource(oldId);
//AbstractComponentInstanceResource newComponentInstanceResource = AbstractComponentsResource.getComponentInstaceResource(newId);
Definitions definitions = this.getDefinitions(oldId).get();

RepositoryFileReference oldRef = BackendUtils.getRefOfDefinitions(oldId);
RepositoryFileReference newRef = BackendUtils.getRefOfDefinitions(newId);
Expand All @@ -256,7 +255,6 @@ public void rename(TOSCAComponentId oldId, TOSCAComponentId newId) throws IOExce
org.apache.commons.io.FileUtils.moveDirectory(oldDir, newDir);

// Update definitions and store it
Definitions definitions = componentInstanceResource.getDefinitions();

// This also updates the definitions of componentInstanceResource
BackendUtils.updateWrapperDefinitions(newId, definitions);
Expand Down

0 comments on commit d0fa901

Please sign in to comment.