Package com.adaptrex.core.rest

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

/*
* Copyright 2012 Adaptrex, LLC
*
* 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.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.AdaptrexRegistry;

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 = AdaptrexRegistry.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 = AdaptrexRegistry.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.