001: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
002: * This code is licensed under the GPL 2.0 license, availible at the root
003: * application directory.
004: */
005: package org.vfny.geoserver.form.data;
006:
007: import org.apache.struts.action.ActionError;
008: import org.apache.struts.action.ActionErrors;
009: import org.apache.struts.action.ActionForm;
010: import org.apache.struts.action.ActionMapping;
011: import org.vfny.geoserver.util.DataStoreUtils;
012: import java.util.Collections;
013: import java.util.List;
014: import java.util.SortedSet;
015: import java.util.TreeSet;
016: import java.util.regex.Pattern;
017: import javax.servlet.http.HttpServletRequest;
018:
019: /**
020: * Used to accept information from user for a New DataStore Action.
021: *
022: * <p>
023: * This form contains a convience property getDataStoreDescrptions() which is
024: * simply to make writing the JSP easier.
025: * </p>
026: *
027: * @author User, Refractions Research, Inc.
028: * @author $Author: jive $ (last modification)
029: * @version $Id: DataDataStoresNewForm.java 8471 2008-02-27 14:41:37Z aaime $
030: */
031: public class DataDataStoresNewForm extends ActionForm {
032: private static final Pattern idPattern = Pattern.compile("^\\a$");
033:
034: /** Description provided by selected Datastore Factory */
035: private String selectedDescription;
036:
037: /** User provided dataStoreID */
038: private String dataStoreID;
039:
040: /**
041: * Default state of New form
042: *
043: * @param mapping DOCUMENT ME!
044: * @param request DOCUMENT ME!
045: */
046: public void reset(ActionMapping mapping, HttpServletRequest request) {
047: super .reset(mapping, request);
048: selectedDescription = "";
049: dataStoreID = "";
050: }
051:
052: /**
053: * List of available DataStoreDescriptions.
054: *
055: * <p>
056: * Convience method for DataStureUtils.listDataStoresDescriptions().
057: * </p>
058: *
059: * @return Sorted set of DataStore Descriptions.
060: */
061: public List getDescriptions() {
062: List descriptions = DataStoreUtils.listDataStoresDescriptions();
063:
064: if ((descriptions == null) || descriptions.isEmpty()) {
065: return Collections.EMPTY_LIST;
066: }
067:
068: String enableVersioning = (String) getServlet()
069: .getServletContext().getInitParameter(
070: "enableVersioning");
071: if (enableVersioning == null
072: || !"TRUE".equals(enableVersioning.toUpperCase())) {
073: descriptions.remove("Versioning Postgis");
074: }
075: return descriptions;
076: }
077:
078: /**
079: * Check NewForm for correct use
080: *
081: * @param mapping DOCUMENT ME!
082: * @param request DOCUMENT ME!
083: *
084: * @return DOCUMENT ME!
085: */
086: public ActionErrors validate(ActionMapping mapping,
087: HttpServletRequest request) {
088: ActionErrors errors = new ActionErrors();
089:
090: if (!getDescriptions().contains(getSelectedDescription())) {
091: errors.add("selectedDescription", new ActionError(
092: "error.dataStoreFactory.invalid",
093: getSelectedDescription()));
094: }
095:
096: if ((getDataStoreID() == null) || getDataStoreID().equals("")) {
097: errors.add("dataStoreID", new ActionError(
098: "error.dataStoreId.required", getDataStoreID()));
099: } else if (!Pattern
100: .matches("^\\w(\\w|\\.)*$", getDataStoreID())) {
101: errors.add("dataStoreID", new ActionError(
102: "error.dataStoreId.invalid", getDataStoreID()));
103: }
104:
105: return errors;
106: }
107:
108: public String getDataStoreID() {
109: return dataStoreID;
110: }
111:
112: public String getSelectedDescription() {
113: return selectedDescription;
114: }
115:
116: public void setDataStoreID(String string) {
117: dataStoreID = string;
118: }
119:
120: public void setSelectedDescription(String string) {
121: selectedDescription = string;
122: }
123:
124: /*
125: * Allows the JSP page to easily access the list of dataStore Descriptions
126: */
127: public SortedSet getDataStoreDescriptions() {
128: return new TreeSet(getDescriptions());
129: }
130: }
|