Merge "Fix build errors in autorelease full clean build"
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.repository / src / main / java / org / eclipse / winery / repository / resources / artifacts / ImplementationArtifactResource.java
1 /*******************************************************************************
2  * Copyright (c) 2012-2013 University of Stuttgart.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * and the Apache License 2.0 which both accompany this distribution,
6  * and are available at http://www.eclipse.org/legal/epl-v10.html
7  * and http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Contributors:
10  *     Oliver Kopp - initial API and implementation
11  *******************************************************************************/
12 package org.eclipse.winery.repository.resources.artifacts;
13
14 import java.util.List;
15
16 import org.eclipse.winery.common.ids.definitions.ArtifactTemplateId;
17 import org.eclipse.winery.common.ids.definitions.ArtifactTypeId;
18 import org.eclipse.winery.model.tosca.TImplementationArtifacts.ImplementationArtifact;
19 import org.eclipse.winery.repository.backend.BackendUtils;
20 import org.eclipse.winery.repository.resources._support.IPersistable;
21 import org.eclipse.winery.repository.resources._support.collections.IIdDetermination;
22
23 public class ImplementationArtifactResource extends GenericArtifactResource<ImplementationArtifact> {
24         
25         private ImplementationArtifact a;
26         
27         
28         /**
29          * Converts the given artifactId to an ImplementArtifact.
30          * 
31          * <em>SIDE EFFECT</em> Adds it to the implementationArtifacts list if it
32          * does not yet exist.
33          */
34         private static ImplementationArtifact getImplementationArtifact(String artifactId, List<ImplementationArtifact> implementationArtifacts) {
35                 if (artifactId == null) {
36                         throw new IllegalArgumentException("artifactId must not be null");
37                 }
38                 if (implementationArtifacts == null) {
39                         throw new IllegalArgumentException("implementationArtifacts must not be null");
40                 }
41                 for (ImplementationArtifact ia : implementationArtifacts) {
42                         // ia.getName() might be null as TOSCA COS01 does not forsee a name on the implementation artifact
43                         // Therefore, we begin the test with "artifactId"
44                         if (artifactId.equals(ia.getName())) {
45                                 return ia;
46                         }
47                 }
48                 // IA does not exist in list
49                 ImplementationArtifact ia = new ImplementationArtifact();
50                 ia.setName(artifactId);
51                 implementationArtifacts.add(ia);
52                 return ia;
53         }
54         
55         public ImplementationArtifactResource(String artifactId, List<ImplementationArtifact> implementationArtifacts, IPersistable res) {
56                 this(ImplementationArtifactResource.getImplementationArtifact(artifactId, implementationArtifacts), implementationArtifacts, res);
57         }
58         
59         public ImplementationArtifactResource(IIdDetermination<ImplementationArtifact> idDetermination, ImplementationArtifact o, int idx, List<ImplementationArtifact> list, IPersistable res) {
60                 super(idDetermination, o, idx, list, res);
61                 this.a = o;
62         }
63         
64         public ImplementationArtifactResource(ImplementationArtifact a, List<ImplementationArtifact> implementationArtifacts, IPersistable res) {
65                 this(new IIdDetermination<ImplementationArtifact>() {
66                         
67                         @Override
68                         public String getId(ImplementationArtifact e) {
69                                 return e.getName();
70                         }
71                 }, a, implementationArtifacts.indexOf(a), implementationArtifacts, res);
72         }
73         
74         public ImplementationArtifact getImplementationArtifact() {
75                 return this.a;
76         }
77         
78         @Override
79         public void setArtifactType(ArtifactTypeId artifactTypeId) {
80                 this.getImplementationArtifact().setArtifactType(artifactTypeId.getQName());
81                 BackendUtils.persist(this.res);
82         }
83         
84         @Override
85         public void setArtifactTemplate(ArtifactTemplateId artifactTemplateId) {
86                 this.getImplementationArtifact().setArtifactRef(artifactTemplateId.getQName());
87                 BackendUtils.persist(this.res);
88         }
89         
90         @Override
91         public ImplementationArtifact getA() {
92                 return this.a;
93         }
94         
95 }