Add winery source code
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.repository / src / main / java / org / eclipse / winery / repository / resources / interfaces / OperationsResource.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.interfaces;
13
14 import java.util.List;
15
16 import javax.ws.rs.Consumes;
17 import javax.ws.rs.FormParam;
18 import javax.ws.rs.POST;
19 import javax.ws.rs.Produces;
20 import javax.ws.rs.core.MediaType;
21 import javax.ws.rs.core.Response;
22 import javax.ws.rs.core.Response.Status;
23
24 import org.apache.commons.lang3.StringUtils;
25 import org.eclipse.winery.model.tosca.TOperation;
26 import org.eclipse.winery.common.Util;
27 import org.eclipse.winery.repository.backend.BackendUtils;
28 import org.eclipse.winery.repository.resources._support.IPersistable;
29 import org.eclipse.winery.repository.resources._support.collections.withid.EntityWithIdCollectionResource;
30 import org.restdoc.annotations.RestDocParam;
31
32 import com.sun.jersey.api.view.Viewable;
33
34 public class OperationsResource extends EntityWithIdCollectionResource<OperationResource, TOperation> {
35         
36         public OperationsResource(List<TOperation> list, IPersistable res) {
37                 super(OperationResource.class, TOperation.class, list, res);
38         }
39         
40         @Override
41         public String getId(TOperation entity) {
42                 return entity.getName();
43         }
44         
45         @Override
46         public Viewable getHTML() {
47                 throw new IllegalStateException("Not yet implemented.");
48         }
49         
50         @POST
51         @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
52         @Produces(MediaType.TEXT_PLAIN)
53         public Response createOperation(@FormParam("name") @RestDocParam(description = "used as name and id") String operationName) {
54                 if (StringUtils.isEmpty(operationName)) {
55                         return Response.status(Status.BAD_REQUEST).entity("operationName not provided").build();
56                 }
57                 
58                 operationName = Util.URLdecode(operationName);
59                 
60                 // TODO: check for duplicates as in instance states
61                 
62                 TOperation operation = new TOperation();
63                 operation.setName(operationName);
64                 this.list.add(operation);
65                 
66                 return BackendUtils.persist(this.res);
67         }
68         
69 }