Package com.adaptrex.core.rest

Source Code of com.adaptrex.core.rest.RestModel

package com.adaptrex.core.rest;

import java.util.Map;

import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriInfo;

import com.adaptrex.core.ext.ExtConfig;
import com.adaptrex.core.persistence.api.ORMModelInstance;
import com.adaptrex.core.persistence.api.ORMPersistenceManager;
import com.adaptrex.core.services.AdaptrexServices;

public class RestModel {

  private ORMModelInstance modelInstance;
  private Object entity;
 
  public RestModel(String className, Map<String,Object> data, UriInfo uriInfo, String factoryName) {
    ExtConfig extConfig = new ExtConfig(className, factoryName);
   
    /*
     * Process parameters
     */
    if (uriInfo != null) {
      MultivaluedMap<String,String> params = uriInfo.getQueryParameters();
      /*
       * Add include/exclude/associations to the config
       */
      if (params.containsKey("include")) {
        extConfig.setIncludes(params.get("include").get(0));
      }
      if (params.containsKey("exclude")) {
        extConfig.setExcludes(params.get("exclude").get(0));
      }
      if (params.containsKey("associations")) {
        extConfig.setAssociations(params.get("associations").get(0));
      }     
    }
   
    ORMPersistenceManager orm = AdaptrexServices.getPersistenceManager(factoryName);
    this.modelInstance = orm.createModelinstance(extConfig, data);
  }
 
  public RestModel(String className, Object id, UriInfo uriInfo, String factoryName) {
    ExtConfig extConfig = new ExtConfig(className, factoryName);
    ORMPersistenceManager orm = AdaptrexServices.getPersistenceManager(factoryName);
    entity = orm.getEntity(extConfig.getEntityClass(), id);
   
    /*
     * Process parameters
     */
    if (uriInfo != null) {
      MultivaluedMap<String,String> params = uriInfo.getQueryParameters();
      /*
       * Add include/exclude/associations to the config
       */
      if (params.containsKey("include")) {
        extConfig.setIncludes(params.get("include").get(0));
      }
      if (params.containsKey("exclude")) {
        extConfig.setExcludes(params.get("exclude").get(0));
      }
      if (params.containsKey("associations")) {
        extConfig.setAssociations(params.get("associations").get(0));
      }     
    }
   
    this.modelInstance = orm.getModelInstance(extConfig, entity);
  }
 
  public ORMModelInstance getModelInstance() {
    return this.modelInstance;
  }
 
  public RestModel updateEntity(RequestBody modelData) {
    this.modelInstance.update(modelData.getData());
    return this;
  }

  public RestModel deleteEntity() {
    this.modelInstance.delete();
    return this;
  }
 
  public Object getEntity() {
    return this.entity;
  }
}
TOP

Related Classes of com.adaptrex.core.rest.RestModel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.