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.ugc.client; 029 030import org.opencms.ugc.client.export.CmsClientUgcSession; 031import org.opencms.ugc.client.export.CmsXmlContentUgcApi; 032import org.opencms.ugc.client.export.I_CmsErrorCallback; 033import org.opencms.ugc.shared.CmsUgcConstants; 034import org.opencms.util.CmsUUID; 035 036import java.util.List; 037import java.util.Map; 038import java.util.Set; 039 040import com.google.common.base.Function; 041import com.google.common.collect.Lists; 042import com.google.gwt.dom.client.Element; 043import com.google.gwt.dom.client.InputElement; 044import com.google.gwt.dom.client.NodeList; 045import com.google.gwt.event.shared.HandlerRegistration; 046import com.google.gwt.http.client.RequestBuilder; 047import com.google.gwt.user.client.rpc.AsyncCallback; 048import com.google.gwt.user.client.ui.FormPanel; 049 050/** 051 * Widget used to wrap and manage the state of forms for which the form editing API is used.<p> 052 */ 053public class CmsUgcWrapper extends FormPanel { 054 055 /** The client form session. */ 056 private CmsClientUgcSession m_formSession; 057 058 /** 059 * Wraps an existing form element with this widget.<p> 060 * 061 * @param element the form element to wrap 062 * @param formSessionId the form session id 063 */ 064 public CmsUgcWrapper(Element element, String formSessionId) { 065 066 super(element, true); 067 setEncoding(FormPanel.ENCODING_MULTIPART); 068 onAttach(); 069 } 070 071 /** 072 * Checks if a form field is a file input field.<p> 073 * 074 * @param elem the form field to check 075 * @return true if the given field is a file input field 076 */ 077 public static boolean isFileField(InputElement elem) { 078 079 return "file".equalsIgnoreCase(elem.getType()); 080 } 081 082 /** 083 * Sets the form session.<p> 084 * 085 * @param session the form session 086 */ 087 public void setFormSession(CmsClientUgcSession session) { 088 089 m_formSession = session; 090 } 091 092 /** 093 * Uploads files from the given file input fields.<p< 094 * 095 * @param fields the set of names of fields containing the files to upload 096 * @param filenameCallback the callback to call with the resulting map from field names to file paths 097 * @param errorCallback the callback to call with an error message 098 */ 099 public void uploadFields( 100 final Set<String> fields, 101 final Function<Map<String, String>, Void> filenameCallback, 102 final I_CmsErrorCallback errorCallback) { 103 104 disableAllFileFieldsExcept(fields); 105 final String id = CmsJsUtils.generateRandomId(); 106 updateFormAction(id); 107 // Using an array here because we can only store the handler registration after it has been created , but 108 final HandlerRegistration[] registration = {null}; 109 registration[0] = addSubmitCompleteHandler(new SubmitCompleteHandler() { 110 111 @SuppressWarnings("synthetic-access") 112 public void onSubmitComplete(SubmitCompleteEvent event) { 113 114 enableAllFileFields(); 115 registration[0].removeHandler(); 116 CmsUUID sessionId = m_formSession.internalGetSessionId(); 117 RequestBuilder requestBuilder = CmsXmlContentUgcApi.SERVICE.uploadFiles( 118 sessionId, 119 fields, 120 id, 121 new AsyncCallback<Map<String, String>>() { 122 123 public void onFailure(Throwable caught) { 124 125 m_formSession.getContentFormApi().handleError(caught, errorCallback); 126 127 } 128 129 public void onSuccess(Map<String, String> fileNames) { 130 131 filenameCallback.apply(fileNames); 132 133 } 134 }); 135 m_formSession.getContentFormApi().getRpcHelper().executeRpc(requestBuilder); 136 m_formSession.getContentFormApi().getRequestCounter().decrement(); 137 } 138 }); 139 m_formSession.getContentFormApi().getRequestCounter().increment(); 140 submit(); 141 } 142 143 /** 144 * Disables all file input fields except the one with the given name.<p> 145 * 146 * @param fieldNames the set of names of fields that should not be disabled 147 */ 148 void disableAllFileFieldsExcept(Set<String> fieldNames) { 149 150 for (InputElement field : getAllFields()) { 151 if (isFileField(field)) { 152 boolean shouldDisable = !fieldNames.contains(field.getName()); 153 field.setDisabled(shouldDisable); 154 } 155 } 156 } 157 158 /** 159 * Enables all file input fields.<p> 160 */ 161 void enableAllFileFields() { 162 163 for (InputElement field : getAllFields()) { 164 if (isFileField(field)) { 165 field.setDisabled(false); 166 } 167 } 168 } 169 170 /** 171 * Gets all form fields.<p> 172 * 173 * @return the list of form fields 174 */ 175 List<InputElement> getAllFields() { 176 177 NodeList<Element> fields = getElement().getElementsByTagName(InputElement.TAG); 178 List<InputElement> result = Lists.newArrayList(); 179 for (int i = 0; i < fields.getLength(); i++) { 180 InputElement field = InputElement.as(fields.getItem(i)); 181 result.add(field); 182 } 183 return result; 184 } 185 186 /** 187 * Updates the form's action attribute.<p> 188 * 189 * @param id the current form data id 190 */ 191 private void updateFormAction(String id) { 192 193 setAction( 194 CmsXmlContentUgcApi.SERVICE_URL 195 + "?" 196 + CmsUgcConstants.PARAM_FORM_DATA_ID 197 + "=" 198 + id 199 + "&" 200 + CmsUgcConstants.PARAM_SESSION_ID 201 + "=" 202 + m_formSession.getSessionId()); 203 } 204}