001/* 002 * Copyright 2018 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.plugins.repository.model; 017 018import java.util.Map; 019 020import org.apache.avalon.framework.service.ServiceException; 021import org.apache.cocoon.ProcessingException; 022import org.xml.sax.ContentHandler; 023import org.xml.sax.SAXException; 024 025import org.ametys.core.util.XMLUtils; 026import org.ametys.plugins.repository.data.type.ModelItemTypeConstants; 027import org.ametys.runtime.i18n.I18nizableText; 028import org.ametys.runtime.model.DefinitionContext; 029import org.ametys.runtime.model.ModelItem; 030import org.ametys.runtime.model.ModelItemGroup; 031import org.ametys.runtime.model.exception.UnknownTypeException; 032import org.ametys.runtime.model.type.ModelItemType; 033import org.ametys.runtime.plugin.ExtensionPoint; 034 035/** 036 * Definition of a repeater. 037 */ 038public class RepeaterDefinition extends ModelItemGroup 039{ 040 /** The widget parameter name for the mode to use for the repeater's modification */ 041 public static final String MODIFICATION_MODE_PARAM_NAME = "mode"; 042 /** The widget parameter value for repeater's modification in a panel */ 043 public static final String MODIFICATION_MODE_PANEL = "panel"; 044 /** The widget parameter value for repeater's modification in a table */ 045 public static final String MODIFICATION_MODE_TABLE = "table"; 046 047 private int _initializeSize; 048 private int _minSize; 049 private int _maxSize; 050 private I18nizableText _addLabel; 051 private I18nizableText _delLabel; 052 private String _headerLabel; 053 054 /** 055 * Default constructor. 056 */ 057 public RepeaterDefinition() 058 { 059 super(); 060 } 061 062 /** 063 * Constructor used to create simple models and items 064 * @param name the name of the definition 065 * @param type the type of the repeater 066 * @param children the repeater's children 067 */ 068 public RepeaterDefinition(String name, ModelItemType type, ModelItem... children) 069 { 070 super(name, children); 071 setType(type); 072 } 073 074 /** 075 * Retrieves the initial size. 076 * @return the initial size. 077 */ 078 public int getInitialSize() 079 { 080 return _initializeSize; 081 } 082 083 /** 084 * Set the initial size. 085 * @param size the initial size. 086 */ 087 public void setInitialSize(int size) 088 { 089 _initializeSize = size; 090 } 091 092 /** 093 * Retrieves the minimum size. 094 * @return the minimum size. 095 */ 096 public int getMinSize() 097 { 098 return _minSize; 099 } 100 101 /** 102 * Set the minimum size. 103 * @param size the minimum size. 104 */ 105 public void setMinSize(int size) 106 { 107 _minSize = size; 108 } 109 110 /** 111 * Retrieves the maximum size. 112 * @return the maximum size or <code>-1</code> if unbounded. 113 */ 114 public int getMaxSize() 115 { 116 return _maxSize; 117 } 118 119 /** 120 * Set the maximum size. 121 * @param size the maximum size or <code>-1</code> if unbounded. 122 */ 123 public void setMaxSize(int size) 124 { 125 _maxSize = size; 126 } 127 128 /** 129 * Retrieves the add label. 130 * @return the add label or <code>null</code> if none. 131 */ 132 public I18nizableText getAddLabel() 133 { 134 return _addLabel; 135 } 136 137 /** 138 * Set the add label. 139 * @param label the add label or <code>null</code> if none. 140 */ 141 public void setAddLabel(I18nizableText label) 142 { 143 _addLabel = label; 144 } 145 146 /** 147 * Retrieves the delete label. 148 * @return the delete label or <code>null</code> if none. 149 */ 150 public I18nizableText getDeleteLabel() 151 { 152 return _delLabel; 153 } 154 155 /** 156 * Set the delete label. 157 * @param label the delete label or <code>null</code> if none. 158 */ 159 public void setDeleteLabel(I18nizableText label) 160 { 161 _delLabel = label; 162 } 163 164 /** 165 * Get the header label. 166 * @return the header label or <code>null</code> if none. 167 */ 168 public String getHeaderLabel() 169 { 170 return _headerLabel; 171 } 172 173 /** 174 * Set the header label. 175 * @param label the header label or <code>null</code> if none. 176 */ 177 public void setHeaderLabel(String label) 178 { 179 _headerLabel = label; 180 } 181 182 @Override 183 protected Map<String, Object> _toJSON(DefinitionContext context, boolean includeChildren) throws ProcessingException 184 { 185 Map<String, Object> result = super._toJSON(context, includeChildren); 186 187 result.put("type", getType().getId()); 188 result.put("add-label", getAddLabel()); 189 result.put("del-label", getDeleteLabel()); 190 result.put("header-label", getHeaderLabel()); 191 result.put("initial-size", getInitialSize()); 192 result.put("min-size", getMinSize()); 193 194 if (getMaxSize() >= 0) 195 { 196 result.put("max-size", getMaxSize()); 197 } 198 199 return result; 200 } 201 202 @SuppressWarnings("static-access") 203 @Override 204 public void toSAX(ContentHandler contentHandler, DefinitionContext context) throws SAXException 205 { 206 super.toSAX(contentHandler, context); 207 208 XMLUtils.createElement(contentHandler, "initial-size", String.valueOf(getInitialSize())); 209 XMLUtils.createElement(contentHandler, "min-size", String.valueOf(getMinSize())); 210 if (getMaxSize() >= 0) 211 { 212 XMLUtils.createElement(contentHandler, "max-size", String.valueOf(getMaxSize())); 213 } 214 215 XMLUtils.createI18nElementIfNotNull(contentHandler, "label", getLabel()); 216 XMLUtils.createI18nElementIfNotNull(contentHandler, "description", getDescription()); 217 XMLUtils.createElementIfNotNull(contentHandler, "header-label", getHeaderLabel()); 218 } 219 220 /** 221 * Creates a {@link RepeaterDefinition} 222 * @param name the repeater's name 223 * @param availableTypesExtensionPoint the role of the extension point containing all available types for this {@link ModelItem} 224 * @param children the repeater's children 225 * @return the created {@link RepeaterDefinition} 226 * @throws UnknownTypeException if the repeater type is not available in the extension point 227 * @throws ServiceException if an error occurs while getting the extension point of available types 228 */ 229 public static RepeaterDefinition of(String name, String availableTypesExtensionPoint, ModelItem... children) throws UnknownTypeException, ServiceException 230 { 231 @SuppressWarnings("unchecked") 232 ExtensionPoint<ModelItemType> availableTypes = (ExtensionPoint<ModelItemType>) __serviceManager.lookup(availableTypesExtensionPoint); 233 if (!availableTypes.hasExtension(ModelItemTypeConstants.REPEATER_TYPE_ID)) 234 { 235 throw new UnknownTypeException("The type '" + ModelItemTypeConstants.REPEATER_TYPE_ID + "' (used for data '" + name + "') is not available for the given extension point."); 236 } 237 else 238 { 239 ModelItemType type = availableTypes.getExtension(ModelItemTypeConstants.REPEATER_TYPE_ID); 240 return new RepeaterDefinition(name, type, children); 241 } 242 } 243}