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

src.java.org.sakaiproject.entitybroker.EntityView Maven / Gradle / Ivy

The newest version!
/**
 * $Id$
 * $URL$
 * EntityView.java - entity-broker - Apr 10, 2008 6:26:47 PM - azeckoski
 **************************************************************************
 * Copyright (c) 2008, 2009 The Sakai Foundation
 *
 * Licensed under the Educational Community 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.opensource.org/licenses/ECL-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 org.sakaiproject.entitybroker;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.sakaiproject.entitybroker.entityprovider.extension.Formats;
import org.sakaiproject.entitybroker.util.TemplateParseUtil;
import org.sakaiproject.entitybroker.util.TemplateParseUtil.PreProcessedTemplate;
import org.sakaiproject.entitybroker.util.TemplateParseUtil.ProcessedTemplate;
import org.sakaiproject.entitybroker.util.TemplateParseUtil.Template;


/**
 * Defines an entity view (a specific way to looking at entity data, e.g. LIST of entities, SHOW a single entity, 
 * UPDATE an entity, DELETE an entity, create a NEW entity),
 * each view has a unique view key constant related to it (e.g. {@link #VIEW_LIST}) 
* The view contains all the known information about a view request including * the entity prefix, reference, full path and segments, format (extension), * method (POST, GET, etc.), and view key (type). The Entity View can * generate the URL for this view based on the data it contains.
* Views use URL templates which can be controlled via custom templates if desired.
* NOTE: For those using custom actions, the view is typically LIST if you are returning data * of an unspecified size. If you want to perform write operations (POST, UPDATE, DELETE), you will need * to use the appropriate view constant. See the docs on each constant for more detail. * * @author Aaron Zeckoski ([email protected]) */ public class EntityView implements Cloneable, Serializable { public final static long serialVersionUID = 1l; public static final char SEPARATOR = '/'; public static final char PERIOD = '.'; public static final String PREFIX = "prefix"; public static final String ID = "id"; public static final String DIRECT = "direct"; public static final String DIRECT_PREFIX = SEPARATOR + DIRECT; /** * Represents HTTP methods (GET, POST, etc.) */ public static enum Method { POST, GET, PUT, DELETE, HEAD } /** * Defines the view for the "list" (index) or collection operation, * access a list/collection of all entities of a type (possibly filtered by search params), * represents a {@link Method#GET} to the entity space/collection, * also indicates an action related to reading a collection of entities */ public static final String VIEW_LIST = "list"; /** * Defines the view for the "show" (read) operation, * access data or a view of an entity, * represents a {@link Method#GET} to a specific entity, * also indicates an action related to reading a specific entity */ public static final String VIEW_SHOW = "show"; /** * Defines the view for the "new" (create) operation, * create a new record or access a form for creating a new record, * represents a {@link Method#POST} (or PUT) to the entity space/collection, * also indicates an action related to writing a collection of entities */ public static final String VIEW_NEW = "new"; /** * Defines the view for the "edit" (update) operation, * update an entity or access a form for updating an entity, * represents a {@link Method#PUT} (or POST) to a specific entity, * also indicates an action related to writing a specific entity */ public static final String VIEW_EDIT = "edit"; /** * Defines the view for the "delete" (destroy) operation, * remove an entity or access a form for removing an entity, * represents a {@link Method#DELETE} to a specific entity */ public static final String VIEW_DELETE = "delete"; private String originalEntityURL; /** * Special use only, * normally you should use {@link #toString()} or {@link #getEntityURL(String, String)} * * @return the original entity URL which was used to create this entity view, * includes the optional pieces from the URL, will be null if this was created * without using a constructor that takes an entityUrl */ public String getOriginalEntityUrl() { return originalEntityURL; } protected void setOriginalEntityURL(String entityUrl) { checkEntityURL(entityUrl); this.originalEntityURL = entityUrl; } private String extension; /** * The extension for this view which defines the type of data that will be returned for this view, * examples: html, xml, json * NOTE: you should assume html return format when this is null * * @return the extension for this view if there is one, * this will be null if there was no extension in the original entityUrl */ public String getExtension() { return extension; } public void setExtension(String extension) { this.extension = extension; } /** * @return the format (from {@link Formats}) that is being used for this view, * will return {@link Formats#HTML} if none set */ public String getFormat() { String format = extension; if (format == null) { format = Formats.HTML; } return format; } private Method method = Method.GET; /** * @return the method (GET, POST, etc.) (from {@link Method}) being used for this view, * defaults to GET if none was set explicitly */ public String getMethod() { return method.name(); } public void setMethod(Method method) { if (method != null) { this.method = method; } } private String viewKey; /** * @return the key which uniquely identifies the view we are associated with, * uses the constants like {@link #VIEW_LIST} and {@link #VIEW_NEW} */ public String getViewKey() { return viewKey; } public void setViewKey(String viewKey) { TemplateParseUtil.validateTemplateKey(viewKey); this.viewKey = viewKey; } private EntityReference entityReference; /** * @return the entity reference object which indicates which entity this view related to */ public EntityReference getEntityReference() { return entityReference; } /** * Allows for easy chained construction of EntityViews by setting an EntityReference, * does not set the viewkey or extension unless they are unset, maintains current extension */ public EntityView setEntityReference(EntityReference ref) { if (ref == null) { throw new IllegalArgumentException("ref cannot be null"); } if (this.pathSegments == null) { this.pathSegments = new HashMap(); } this.pathSegments.put(PREFIX, ref.getPrefix()); if (ref.getId() != null) { this.pathSegments.put(ID, ref.getId()); } if (this.viewKey == null) { String viewKey = VIEW_LIST; if (ref.getId() != null) { viewKey = VIEW_SHOW; } setViewKey(viewKey); } else { // fix up the viewKey so that it makes sense if (VIEW_SHOW.equals(this.viewKey) && ref.getId() == null) { this.viewKey = VIEW_LIST; } } this.entityReference = ref; return this; } /** * Contains all path segments for this entity reference, * e.g. /prefix/id/thing would cause this to contain 3 segments * for the prefix, id, and thing as long as one of the parse templates * supported that path */ private Map pathSegments; /** * Contains the parsing templates for this entity reference */ private List