Search in sources :

Example 1 with XmlId

use of org.eclipse.winery.common.ids.XmlId in project winery by eclipse.

the class BackendUtils method synchronizeReferences.

/**
 * Synchronizes the known plans with the data in the XML. When there is a stored file, but no known entry in the
 * XML, we guess "BPEL" as language and "build plan" as type.
 */
public static void synchronizeReferences(ServiceTemplateId id) throws IOException {
    final IRepository repository = RepositoryFactory.getRepository();
    final TServiceTemplate serviceTemplate = repository.getElement(id);
    // locally stored plans
    TPlans plans = serviceTemplate.getPlans();
    // plans stored in the repository
    PlansId plansContainerId = new PlansId(id);
    SortedSet<PlanId> nestedPlans = repository.getNestedIds(plansContainerId, PlanId.class);
    Set<PlanId> plansToAdd = new HashSet<>();
    plansToAdd.addAll(nestedPlans);
    if (nestedPlans.isEmpty()) {
        if (plans == null) {
            // data on the file system equals the data -> no plans
            return;
        } else {
        // noinspection StatementWithEmptyBody
        // we have to check for equality later
        }
    }
    if (plans == null) {
        plans = new TPlans();
        serviceTemplate.setPlans(plans);
    }
    for (Iterator<TPlan> iterator = plans.getPlan().iterator(); iterator.hasNext(); ) {
        TPlan plan = iterator.next();
        if (plan.getPlanModel() != null) {
            // in case, a plan is directly contained in a Model element, we do not need to do anything
            continue;
        }
        TPlan.PlanModelReference planModelReference;
        if ((planModelReference = plan.getPlanModelReference()) != null) {
            String ref = planModelReference.getReference();
            if ((ref == null) || ref.startsWith("../")) {
                // special case (due to errors in the importer): empty PlanModelReference field
                if (plan.getId() == null) {
                    // invalid plan entry: no id.
                    // we remove the entry
                    iterator.remove();
                    continue;
                }
                PlanId planId = new PlanId(plansContainerId, new XmlId(plan.getId(), false));
                if (nestedPlans.contains(planId)) {
                    // everything allright
                    // we do NOT need to add the plan on the HDD to the XML
                    plansToAdd.remove(planId);
                } else {
                    // no local storage for the plan, we remove it from the XML
                    iterator.remove();
                }
            }
        }
    }
    // add all plans locally stored, but not contained in the XML, as plan element to the plans of the service template.
    List<TPlan> thePlans = plans.getPlan();
    for (PlanId planId : plansToAdd) {
        SortedSet<RepositoryFileReference> files = repository.getContainedFiles(planId);
        if (files.size() != 1) {
            throw new IllegalStateException("Currently, only one file per plan is supported.");
        }
        RepositoryFileReference ref = files.iterator().next();
        TPlan plan = new TPlan();
        plan.setId(planId.getXmlId().getDecoded());
        plan.setName(planId.getXmlId().getDecoded());
        plan.setPlanType(org.eclipse.winery.repository.Constants.TOSCA_PLANTYPE_BUILD_PLAN);
        plan.setPlanLanguage(Namespaces.URI_BPEL20_EXECUTABLE);
        // create a PlanModelReferenceElement pointing to that file
        String path = Util.getUrlPath(ref);
        // path is relative from the definitions element
        path = "../" + path;
        TPlan.PlanModelReference pref = new TPlan.PlanModelReference();
        pref.setReference(path);
        plan.setPlanModelReference(pref);
        thePlans.add(plan);
    }
    if (serviceTemplate.getPlans().getPlan().isEmpty()) {
        serviceTemplate.setPlans(null);
    }
    RepositoryFactory.getRepository().setElement(id, serviceTemplate);
}
Also used : TPlans(org.eclipse.winery.model.tosca.TPlans) PlanId(org.eclipse.winery.common.ids.elements.PlanId) PlansId(org.eclipse.winery.common.ids.elements.PlansId) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) TPlan(org.eclipse.winery.model.tosca.TPlan) XmlId(org.eclipse.winery.common.ids.XmlId) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate) HashSet(java.util.HashSet)

Example 2 with XmlId

use of org.eclipse.winery.common.ids.XmlId in project winery by eclipse.

the class Showcases method zipTypeTest.

@Test
public void zipTypeTest() throws Exception {
    String name = "Showcase.csar";
    InputStream inputStream = new FileInputStream(path + File.separator + name);
    Converter converter = new Converter();
    converter.convertY2X(inputStream);
    TServiceTemplate serviceTemplate = RepositoryFactory.getRepository().getElement(new ServiceTemplateId(new Namespace(Namespaces.DEFAULT_NS, false), new XmlId("Showcase", false)));
    Assert.assertNotNull(serviceTemplate);
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Converter(org.eclipse.winery.yaml.converter.Converter) XmlId(org.eclipse.winery.common.ids.XmlId) ServiceTemplateId(org.eclipse.winery.common.ids.definitions.ServiceTemplateId) FileInputStream(java.io.FileInputStream) Namespace(org.eclipse.winery.common.ids.Namespace) TServiceTemplate(org.eclipse.winery.model.tosca.TServiceTemplate) Test(org.junit.Test)

Example 3 with XmlId

use of org.eclipse.winery.common.ids.XmlId in project winery by eclipse.

the class ImportUtilsTest method getLocationForImportTest.

@Test
public void getLocationForImportTest() throws Exception {
    this.setRevisionTo("5fdcffa9ccd17743d5498cab0914081fc33606e9");
    XSDImportId id = new XSDImportId(new Namespace("http://opentosca.org/nodetypes", false), new XmlId("CloudProviderProperties", false));
    Optional<String> importLocation = ImportUtils.getLocation(id);
    Assert.assertEquals(true, importLocation.isPresent());
}
Also used : XSDImportId(org.eclipse.winery.common.ids.definitions.imports.XSDImportId) XmlId(org.eclipse.winery.common.ids.XmlId) Namespace(org.eclipse.winery.common.ids.Namespace) Test(org.junit.Test)

Example 4 with XmlId

use of org.eclipse.winery.common.ids.XmlId in project winery by eclipse.

the class PlanResource method getId.

/**
 * Determines the id of the current resource
 */
private PlanId getId() {
    ServiceTemplateId sId = (ServiceTemplateId) this.getServiceTemplateResource().getId();
    PlansId psId = new PlansId(sId);
    return new PlanId(psId, new XmlId(this.o.getId(), false));
}
Also used : PlanId(org.eclipse.winery.common.ids.elements.PlanId) XmlId(org.eclipse.winery.common.ids.XmlId) ServiceTemplateId(org.eclipse.winery.common.ids.definitions.ServiceTemplateId) PlansId(org.eclipse.winery.common.ids.elements.PlansId)

Example 5 with XmlId

use of org.eclipse.winery.common.ids.XmlId in project winery by eclipse.

the class PlansResource method saveFile.

private Response saveFile(TPlan tPlan, InputStream uploadedInputStream, FormDataContentDisposition fileDetail, FormDataBodyPart body) {
    boolean bpmn4toscaMode = Namespaces.URI_BPMN4TOSCA_20.equals(tPlan.getPlanLanguage());
    if (uploadedInputStream != null || bpmn4toscaMode) {
        // Determine Id
        PlansId plansId = new PlansId((ServiceTemplateId) ((ServiceTemplateResource) this.res).getId());
        PlanId planId = new PlanId(plansId, new XmlId(tPlan.getId(), false));
        // Ensure overwriting
        if (RepositoryFactory.getRepository().exists(planId)) {
            try {
                RepositoryFactory.getRepository().forceDelete(planId);
                // Quick hack to remove the deleted plan from the plans element
                ((ServiceTemplateResource) this.res).synchronizeReferences();
            } catch (IOException e) {
                return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
            }
        }
        String fileName;
        if (bpmn4toscaMode) {
            fileName = tPlan.getId() + Constants.SUFFIX_BPMN4TOSCA;
            RepositoryFileReference ref = new RepositoryFileReference(planId, fileName);
            // Errors are ignored in the following call
            RestUtils.putContentToFile(ref, "{}", MediaTypes.MEDIATYPE_APPLICATION_JSON);
        } else {
            // We use the filename also as local file name. Alternatively, we could use the xml id
            // With URL encoding, this should not be an issue
            fileName = Util.URLencode(fileDetail.getFileName());
            // Really store it
            RepositoryFileReference ref = new RepositoryFileReference(planId, fileName);
            // Errors are ignored in the following call
            RestUtils.putContentToFile(ref, uploadedInputStream, body.getMediaType());
        }
        PlansResource.setPlanModelReference(tPlan, planId, fileName);
    }
    return RestUtils.persist(this.res);
}
Also used : RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) PlanId(org.eclipse.winery.common.ids.elements.PlanId) ServiceTemplateResource(org.eclipse.winery.repository.rest.resources.servicetemplates.ServiceTemplateResource) XmlId(org.eclipse.winery.common.ids.XmlId) IOException(java.io.IOException) PlansId(org.eclipse.winery.common.ids.elements.PlansId)

Aggregations

XmlId (org.eclipse.winery.common.ids.XmlId)9 Namespace (org.eclipse.winery.common.ids.Namespace)4 PlanId (org.eclipse.winery.common.ids.elements.PlanId)4 PlansId (org.eclipse.winery.common.ids.elements.PlansId)4 RepositoryFileReference (org.eclipse.winery.common.RepositoryFileReference)3 IOException (java.io.IOException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ServiceTemplateId (org.eclipse.winery.common.ids.definitions.ServiceTemplateId)2 TServiceTemplate (org.eclipse.winery.model.tosca.TServiceTemplate)2 Test (org.junit.Test)2 ApiOperation (io.swagger.annotations.ApiOperation)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 StringReader (java.io.StringReader)1 Constructor (java.lang.reflect.Constructor)1 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 HashSet (java.util.HashSet)1 Consumes (javax.ws.rs.Consumes)1