com.chiralbehaviors.CoRE.access.resource.CollectionResource.java Source code

Java tutorial

Introduction

Here is the source code for com.chiralbehaviors.CoRE.access.resource.CollectionResource.java

Source

/** 
 * (C) Copyright 2012 Chiral Behaviors, LLC. All Rights Reserved
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *     
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
package com.chiralbehaviors.CoRE.access.resource;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import com.chiralbehaviors.CoRE.Ruleform;
import com.chiralbehaviors.CoRE.meta.Model;
import com.chiralbehaviors.CoRE.product.Product;
import com.chiralbehaviors.CoRE.product.ProductNetwork;
import com.chiralbehaviors.CoRE.relationship.Relationship;
import com.chiralbehaviors.CoRE.utils.Util;
import com.fasterxml.jackson.core.JsonProcessingException;

/**
 * A REST resource for processing atomic transactions full of multiple objects
 * of many types.
 * 
 * @author hparry
 * 
 */
@Path("/v{version : \\d+}/services/data/collection")
public class CollectionResource extends TraversalResource {

    public CollectionResource(EntityManagerFactory emf) {
        super(emf);
    }

    @POST
    @Path("{parentId}")
    @Produces(MediaType.APPLICATION_JSON)
    public Product createNewProduct(@PathParam("parentId") String parentId, @QueryParam("relId") String relId,
            Product child) throws JsonProcessingException {
        return perform(new Transactionally<Product>() {

            @Override
            public Product exec(Model model) throws SQLException {
                EntityManager em = model.getEntityManager();
                Product parent = em.find(Product.class, parentId);
                Relationship rel = em.find(Relationship.class, relId);
                em.persist(child);
                ProductNetwork net = new ProductNetwork(parent, rel, child, parent.getUpdatedBy());
                em.persist(net);
                em.flush();
                em.refresh(child);
                return child;
            }
        });
    }

    // @GET
    // @Path("/{id}")
    // @Produces(MediaType.APPLICATION_JSON)
    // public SerializableGraph get(@PathParam("id") long id, @QueryParam("rel")
    // List<String> relIds) throws JsonProcessingException {
    //
    // Product p = new Product();
    // p.setId(id);
    // List<Ruleform> nodes = new LinkedList<Ruleform>();
    // nodes.add(p);
    // List<Relationship> rels = new LinkedList<Relationship>();
    // for (String rid : relIds) {
    // Relationship r = em.find(Relationship.class, rid);
    // rels.add(r);
    // }
    // GraphQuery ng = getNetwork(nodes, rels);
    // SerializableGraph sg = new SerializableGraph(ng);
    // return sg;
    // }
    //
    // public GraphQuery getNetwork(List<Ruleform> nodes, List<Relationship>
    // relationships) throws JsonProcessingException {
    // GraphQuery pg = new GraphQuery(nodes, relationships, em);
    // return pg;
    //
    // }

    @GET
    @Path("{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Product> get(@PathParam("id") String id, @QueryParam("relId") List<String> relIds)
            throws JsonProcessingException {

        Product p = new Product();
        p.setId(UUID.fromString(id));
        List<Ruleform> nodes = new LinkedList<>();
        nodes.add(p);
        List<Relationship> rels = new LinkedList<>();
        for (String rid : relIds) {
            Relationship r = readOnlyModel.getEntityManager().find(Relationship.class, rid);
            rels.add(r);
        }
        return readOnlyModel.getProductModel().getChildren(p, rels.get(0));

    }

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    public Ruleform post(Ruleform graph) throws JsonProcessingException {
        return perform((Model model) -> Util.smartMerge(model.getEntityManager(), graph, new HashMap<>()));
    }
}