All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.adaptrex.core.rest.RestModel Maven / Gradle / Ivy

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 data, UriInfo uriInfo, String factoryName) {
		ExtConfig extConfig = new ExtConfig(className, factoryName);
		
		/*
		 * Process parameters
		 */
		if (uriInfo != null) {
			MultivaluedMap 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 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;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy