001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.gwt.client.property; 029 030import org.opencms.gwt.client.ui.input.form.CmsForm; 031import org.opencms.gwt.client.ui.input.form.I_CmsFormSubmitHandler; 032import org.opencms.gwt.shared.property.CmsClientProperty; 033import org.opencms.gwt.shared.property.CmsPropertyModification; 034 035import java.util.ArrayList; 036import java.util.Collection; 037import java.util.HashMap; 038import java.util.HashSet; 039import java.util.List; 040import java.util.Map; 041import java.util.Set; 042 043/** 044 * This class handles form submits from property forms and passes the form data to a property editor handler.<p> 045 */ 046public class CmsPropertySubmitHandler implements I_CmsFormSubmitHandler { 047 048 /** The property editor handler. */ 049 private I_CmsPropertyEditorHandler m_handler; 050 051 /** 052 * Creates a new instance.<p> 053 * 054 * @param handler the property editor handler 055 */ 056 public CmsPropertySubmitHandler(I_CmsPropertyEditorHandler handler) { 057 058 m_handler = handler; 059 } 060 061 /** 062 * @see org.opencms.gwt.client.ui.input.form.I_CmsFormSubmitHandler#onSubmitForm(org.opencms.gwt.client.ui.input.form.CmsForm, java.util.Map, java.util.Set) 063 */ 064 public void onSubmitForm(CmsForm form, Map<String, String> fieldValues, Set<String> editedFields) { 065 066 CmsReloadMode reloadMode = getReloadMode(fieldValues, editedFields); 067 Map<String, String> changedPropValues = removeTabSuffixes(fieldValues); 068 Set<String> editedModels = removeTabSuffixes(editedFields); 069 changedPropValues.keySet().retainAll(editedModels); 070 List<CmsPropertyModification> propChanges = getPropertyChanges(changedPropValues); 071 if (!m_handler.hasEditableName()) { 072 // The root element's name can't be edited 073 m_handler.handleSubmit( 074 "", 075 null, 076 propChanges, 077 editedFields.contains(A_CmsPropertyEditor.FIELD_URLNAME), 078 reloadMode); 079 return; 080 } 081 final String urlNameValue = getAndRemoveValue(fieldValues, A_CmsPropertyEditor.FIELD_URLNAME); 082 fieldValues.remove(A_CmsPropertyEditor.FIELD_LINK); 083 m_handler.handleSubmit( 084 urlNameValue, 085 null, 086 propChanges, 087 editedFields.contains(A_CmsPropertyEditor.FIELD_URLNAME), 088 reloadMode); 089 } 090 091 /** 092 * Helper method which retrieves a value for a given key from a map and then deletes the entry for the key.<p> 093 * 094 * @param map the map from which to retrieve the value 095 * @param key the key 096 * 097 * @return the removed value 098 */ 099 protected String getAndRemoveValue(Map<String, String> map, String key) { 100 101 String value = map.get(key); 102 if (value != null) { 103 map.remove(key); 104 } 105 return value; 106 } 107 108 /** 109 * Converts a map of field values to a list of property changes.<p> 110 * 111 * @param fieldValues the field values 112 * @return the property changes 113 */ 114 protected List<CmsPropertyModification> getPropertyChanges(Map<String, String> fieldValues) { 115 116 List<CmsPropertyModification> result = new ArrayList<CmsPropertyModification>(); 117 for (Map.Entry<String, String> entry : fieldValues.entrySet()) { 118 String key = entry.getKey(); 119 String value = entry.getValue(); 120 if (key.contains("/")) { 121 CmsPropertyModification propChange = new CmsPropertyModification(key, value); 122 result.add(propChange); 123 } 124 } 125 return result; 126 } 127 128 /** 129 * Removes the tab suffix from a field id.<p> 130 * 131 * @param fieldId a field id 132 * 133 * @return the field id without the suffix 134 */ 135 protected String removeTabSuffix(String fieldId) { 136 137 return fieldId.replaceAll("#.*$", ""); 138 } 139 140 /** 141 * Removes the tab suffixes from each field id of a collection.<p> 142 * 143 * @param fieldIds the field ids from which to remove the tab suffix 144 * 145 * @return a new collection of field ids without tab suffixes 146 */ 147 protected Set<String> removeTabSuffixes(Collection<String> fieldIds) { 148 149 Set<String> result = new HashSet<String>(); 150 for (String fieldId : fieldIds) { 151 result.add(removeTabSuffix(fieldId)); 152 } 153 return result; 154 } 155 156 /** 157 * Removes the tab suffixes from the keys of a map.<p> 158 * 159 * @param fieldValues a map of field values 160 * 161 * @return a new map of field values, with tab suffixes removed from the keys 162 */ 163 protected Map<String, String> removeTabSuffixes(Map<String, String> fieldValues) { 164 165 Map<String, String> result = new HashMap<String, String>(); 166 for (Map.Entry<String, String> entry : fieldValues.entrySet()) { 167 String key = entry.getKey(); 168 String newKey = removeTabSuffix(key); 169 result.put(newKey, entry.getValue()); 170 } 171 return result; 172 } 173 174 /** 175 * Check if a field name belongs to one of a given list of properties.<p> 176 * 177 * @param fieldName the field name 178 * @param propNames the property names 179 * 180 * @return true if the field name matches one of the property names 181 */ 182 private boolean checkContains(String fieldName, String... propNames) { 183 184 for (String propName : propNames) { 185 if (fieldName.contains("/" + propName + "/")) { 186 return true; 187 } 188 } 189 return false; 190 } 191 192 /** 193 * Returns the reload mode to use for the given changes.<p> 194 * 195 * @param fieldValues the field values 196 * @param editedFields the set of edited fields 197 * 198 * @return the reload mode 199 */ 200 private CmsReloadMode getReloadMode(Map<String, String> fieldValues, Set<String> editedFields) { 201 202 for (String fieldName : editedFields) { 203 if (checkContains(fieldName, CmsClientProperty.PROPERTY_DEFAULTFILE, CmsClientProperty.PROPERTY_NAVPOS)) { 204 return CmsReloadMode.reloadParent; 205 206 } 207 } 208 return CmsReloadMode.reloadEntry; 209 } 210}