Package com.adaptrex.core.rest

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

/*
* 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.ArrayList;
import java.util.List;
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.ext.Filter;
import com.adaptrex.core.ext.Sorter;
import com.adaptrex.core.persistence.api.ORMStoreData;
import com.adaptrex.core.AdaptrexRegistry;
import com.adaptrex.core.utilities.StringUtilities;

public class RestStore {

  private ORMStoreData storeData;
  private String factoryName;
 
  public RestStore(String className, UriInfo uriInfo, String factoryName) {
    ExtConfig extConfig = new ExtConfig(className, factoryName);
    this.factoryName = factoryName;
   
    /*
     * Process parameters
     */
    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));
    }   
   
    storeData = AdaptrexRegistry.getPersistenceManager(factoryName).getStoreData(extConfig);
   
    if (params.containsKey("start")) storeData.setStart(getInt(params.get("start")));
    if (params.containsKey("limit")) storeData.setLimit(getInt(params.get("limit")));
   
    if (params.containsKey("sort")) {
      List<Sorter> sorters = new ArrayList<Sorter>();
      @SuppressWarnings("unchecked")
      List<Map<String, String>> sort = (List<Map<String, String>>)
          StringUtilities.fromJson(params.get("sort").get(0), List.class);
      for (Map<String,String> sortItem : sort) {
        sorters.add(new Sorter(sortItem.get("property"), sortItem.get("direction"), null));
      }
      storeData.setSorters(sorters);
    }
   
    if (params.containsKey("group")) {
      List<Sorter> groupers = new ArrayList<Sorter>();
      @SuppressWarnings("unchecked")
      List<Map<String, String>> group = (List<Map<String, String>>)
          StringUtilities.fromJson(params.get("group").get(0), List.class);
      for (Map<String,String> groupItem : group) {
        groupers.add(new Sorter(groupItem.get("property"), groupItem.get("direction"), null));
      }
      storeData.setGroupers(groupers);
    }
   
    if (params.containsKey("filter")) {
      List<Filter> filters = new ArrayList<Filter>();
      @SuppressWarnings("unchecked")
      List<Map<String, String>> filter = (List<Map<String, String>>)
          StringUtilities.fromJson(params.get("filter").get(0), List.class);
      for (Map<String,String> filterItem : filter) {
        filters.add(new Filter(filterItem.get("property"), filterItem.get("value")));
      }
      storeData.setFilters(filters);
    }   
  }
 
  public RestStore setWhere(String where) {
    this.storeData.setWhere(where);
    return this;
  }

  public RestStore addParam(String key, Object value) {
    this.storeData.getParams().put(key, value);
    return this;
  }
 
  public RestStore setParams(Map<String,Object> params) {
    this.storeData.setParams(params);
    return this;
  }
 
  public String getFactoryName() {
    return this.factoryName;
  }
 
  public ORMStoreData getStoreData() {
    return this.storeData;
  }
 
  private Integer getInt(List<String> param) {
    return Integer.valueOf((String) param.get(0));
  }
 
}
TOP

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

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.