com.adaptrex.core.rest.RestModel Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of adaptrex-core Show documentation
Show all versions of adaptrex-core Show documentation
The Core Adaptrex Framework
The newest version!
/*
* 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.servlet.ServletContext;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriInfo;
import com.adaptrex.core.ext.ExtConfig;
import com.adaptrex.core.ext.ModelInstance;
import com.adaptrex.core.persistence.api.AdaptrexPersistenceManager;
public class RestModel {
private ModelInstance modelInstance;
private ExtConfig extConfig;
private AdaptrexPersistenceManager orm;
public RestModel(ServletContext context, Class> clazz) {
this(context, clazz, null, null);
}
public RestModel(ServletContext context, Class> clazz, String factoryName) {
this(context, clazz, null, factoryName);
}
public RestModel(ServletContext context, Class> clazz, UriInfo uriInfo) {
this(context, clazz, uriInfo, null);
}
public RestModel(ServletContext context, Class> clazz, UriInfo uriInfo, String factoryName) {
String className = clazz.getSimpleName();
extConfig = new ExtConfig(context, className, factoryName);
orm = extConfig.getORMPersistenceManager();
if (uriInfo != null) {
MultivaluedMap params = uriInfo.getQueryParameters();
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));
}
}
}
public RestModel read(Object id) {
try {
this.modelInstance = orm.getModelInstance(extConfig, id);
} catch (Exception e) {
throw new RuntimeException(e);
}
return this;
}
public RestModel read(String key, Object value) {
try {
this.modelInstance = orm.getModelInstance(extConfig, key, value);
} catch (Exception e) {
throw new RuntimeException(e);
}
return this;
}
public RestModel create(Map data) {
this.modelInstance = orm.createModelInstance(extConfig, data);
return this;
}
public RestModel update(Object id, Map data) {
try {
this.modelInstance = orm.updateModelInstance(extConfig, id, data);
} catch (Exception e) {
throw new RuntimeException(e);
}
return this;
}
public RestModel update(String key, Object value, Map data) {
try {
this.modelInstance = orm.updateModelInstance(extConfig, key, value, data);
} catch (Exception e) {
throw new RuntimeException(e);
}
return this;
}
public RestModel delete(Object id) {
try {
this.modelInstance = orm.deleteModelInstance(extConfig, id);
} catch (Exception e) {
throw new RuntimeException(e);
}
return this;
}
public RestModel delete(String key, Object value) {
try {
this.modelInstance = orm.deleteModelInstance(extConfig, key, value);
} catch (Exception e) {
throw new RuntimeException(e);
}
return this;
}
public ModelInstance getModelInstance() {
return this.modelInstance;
}
}