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.workplace.tools.modules; 029 030import org.opencms.file.types.I_CmsResourceType; 031import org.opencms.jsp.CmsJspActionElement; 032import org.opencms.main.CmsException; 033import org.opencms.main.CmsLog; 034import org.opencms.main.OpenCms; 035import org.opencms.widgets.CmsInputWidget; 036import org.opencms.workplace.CmsDialog; 037import org.opencms.workplace.CmsWidgetDialog; 038import org.opencms.workplace.CmsWidgetDialogParameter; 039import org.opencms.workplace.CmsWorkplaceSettings; 040import org.opencms.workplace.tools.CmsToolDialog; 041 042import java.util.Collections; 043import java.util.HashMap; 044import java.util.Map; 045 046import javax.servlet.http.HttpServletRequest; 047import javax.servlet.http.HttpServletResponse; 048import javax.servlet.jsp.PageContext; 049 050import org.apache.commons.logging.Log; 051 052/** 053 * A work place administration dialog to add a xml content based resource type to a module.<p> 054 */ 055public class CmsModuleAddResourceTypes extends CmsWidgetDialog { 056 057 /** The request attribute name used to store the new resource type information. */ 058 public static final String ATTR_RESOURCE_TYPE_INFO = "resourceTypeInfo"; 059 060 /** The message key prefix. */ 061 public static final String KEY_PREFIX = "addtype"; 062 063 /** The add type report JSP. */ 064 private static final String ADD_TYPE_REPORT = "/system/workplace/admin/modules/add_type_report.jsp"; 065 066 /** The log object for this class. */ 067 private static final Log LOG = CmsLog.getLog(CmsModuleAddResourceTypes.class); 068 069 /** The dialog pages. */ 070 private static final String[] PAGES = {"page0"}; 071 072 /** Module name. */ 073 protected String m_paramModule; 074 075 /** The collected resource type information. */ 076 private CmsResourceTypeInfoBean m_resInfo; 077 078 /** 079 * Constructor.<p> 080 * 081 * @param jsp an initialized JSP action element 082 */ 083 public CmsModuleAddResourceTypes(CmsJspActionElement jsp) { 084 085 super(jsp); 086 087 } 088 089 /** 090 * Constructor.<p> 091 * 092 * @param context the JSP page context 093 * @param req the JSP request 094 * @param res the JSP response 095 */ 096 public CmsModuleAddResourceTypes(PageContext context, HttpServletRequest req, HttpServletResponse res) { 097 098 this(new CmsJspActionElement(context, req, res)); 099 } 100 101 /** 102 * @see org.opencms.workplace.CmsWidgetDialog#actionCommit() 103 */ 104 @Override 105 public void actionCommit() { 106 107 try { 108 // validate the resource type info 109 if (OpenCms.getResourceManager().hasResourceType(m_resInfo.getName())) { 110 throw new CmsException( 111 Messages.get().container(Messages.ERR_RESOURCE_TYPE_ALREADY_EXISTS_1, m_resInfo.getName())); 112 } 113 if (OpenCms.getResourceManager().hasResourceType(m_resInfo.getId())) { 114 throw new CmsException( 115 Messages.get().container( 116 Messages.ERR_RESOURCE_TYPE_ID_ALREADY_IN_USE_1, 117 String.valueOf(m_resInfo.getId()))); 118 } 119 120 getJsp().getRequest().setAttribute(ATTR_RESOURCE_TYPE_INFO, m_resInfo); 121 Map<String, String[]> params = new HashMap<String, String[]>(); 122 params.put(PARAM_CLOSELINK, new String[] {getParamCloseLink()}); 123 params.put(PARAM_STYLE, new String[] {CmsToolDialog.STYLE_NEW}); 124 getToolManager().jspForwardPage(this, ADD_TYPE_REPORT, params); 125 } catch (Exception e) { 126 LOG.error(e.getLocalizedMessage(), e); 127 setCommitErrors(Collections.<Throwable> singletonList(e)); 128 } 129 130 } 131 132 /** 133 * Gets the module parameter.<p> 134 * 135 * @return the module parameter 136 */ 137 public String getParamModule() { 138 139 return m_paramModule; 140 } 141 142 /** 143 * Sets the module parameter.<p> 144 * @param paramModule the module parameter 145 */ 146 public void setParamModule(String paramModule) { 147 148 m_paramModule = paramModule; 149 } 150 151 /** 152 * @see org.opencms.workplace.CmsWidgetDialog#createDialogHtml(java.lang.String) 153 */ 154 @Override 155 protected String createDialogHtml(String page) { 156 157 StringBuffer result = new StringBuffer(1024); 158 if (PAGES[0].equals(page)) { 159 160 // create widget table 161 result.append(createWidgetTableStart()); 162 163 // show error header once if there were validation errors 164 result.append(createWidgetErrorHeader()); 165 166 result.append(createWidgetBlockStart(getMessages().key(Messages.GUI_ADD_TYPES_LABEL_0))); 167 result.append(createDialogRowsHtml(0, 5)); 168 result.append(createWidgetBlockEnd()); 169 170 // close widget table 171 result.append(createWidgetTableEnd()); 172 } else { 173 result.append(page); 174 175 } 176 177 return result.toString(); 178 } 179 180 /** 181 * @see org.opencms.workplace.CmsWidgetDialog#defineWidgets() 182 */ 183 @Override 184 protected void defineWidgets() { 185 186 initResInfo(); 187 setKeyPrefix(KEY_PREFIX); 188 addWidget(new CmsWidgetDialogParameter(m_resInfo, "name", new CmsInputWidget())); 189 addWidget(new CmsWidgetDialogParameter(m_resInfo, "id", new CmsInputWidget())); 190 addWidget(new CmsWidgetDialogParameter(m_resInfo, "niceName", new CmsInputWidget())); 191 addWidget(new CmsWidgetDialogParameter(m_resInfo, "title", new CmsInputWidget())); 192 addWidget(new CmsWidgetDialogParameter(m_resInfo, "description", new CmsInputWidget())); 193 addWidget(new CmsWidgetDialogParameter(m_resInfo, "schemaTypeName", new CmsInputWidget())); 194 } 195 196 /** 197 * @see org.opencms.workplace.CmsWidgetDialog#getPageArray() 198 */ 199 @Override 200 protected String[] getPageArray() { 201 202 return PAGES; 203 } 204 205 /** 206 * @see org.opencms.workplace.CmsWorkplace#initMessages() 207 */ 208 @Override 209 protected void initMessages() { 210 211 // add specific dialog resource bundle 212 addMessages(Messages.get().getBundleName()); 213 // add default resource bundles 214 super.initMessages(); 215 } 216 217 /** 218 * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest) 219 */ 220 @Override 221 protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) { 222 223 // initialize parameters and dialog actions in super implementation 224 super.initWorkplaceRequestValues(settings, request); 225 226 // save the current state of the job (may be changed because of the widget values) 227 setDialogObject(m_resInfo); 228 } 229 230 /** 231 * @see org.opencms.workplace.CmsWidgetDialog#validateParamaters() 232 */ 233 @Override 234 protected void validateParamaters() throws Exception { 235 236 // nothing to do 237 } 238 239 /** 240 * Returns the next available resource type id.<p> 241 * 242 * @return the resource type id 243 */ 244 private int findNextTypeId() { 245 246 int result = (int)(20000 + Math.round(Math.random() * 1000)); 247 for (I_CmsResourceType type : OpenCms.getResourceManager().getResourceTypes()) { 248 if (type.getTypeId() >= result) { 249 result = type.getTypeId() + 1; 250 } 251 } 252 return result; 253 } 254 255 /** 256 * Initializes the resource type info bean.<p> 257 */ 258 private void initResInfo() { 259 260 Object dialogObject = getDialogObject(); 261 if ((dialogObject == null) || CmsDialog.DIALOG_INITIAL.equals(getParamAction())) { 262 m_resInfo = new CmsResourceTypeInfoBean(); 263 int id = findNextTypeId(); 264 m_resInfo.setId(id); 265 m_resInfo.setModuleName(getParamModule()); 266 } else { 267 m_resInfo = (CmsResourceTypeInfoBean)dialogObject; 268 } 269 } 270 271}