/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.editors.models;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.java.dev.genesis.annotation.DataProvider;
import de.innovationgate.eclipse.utils.ui.AbstractGenesisBoundObject;
import de.innovationgate.eclipse.utils.ui.model.WGASchemaDefinitionModel;
import de.innovationgate.utils.WGUtils;
import de.innovationgate.webgate.api.schemadef.WGContentItemDefinition;
import de.innovationgate.webgate.api.schemadef.WGContentTypeDefinition;
import de.innovationgate.webgate.api.schemadef.WGContentItemDefinition.Type;
import de.innovationgate.wga.model.AccessLevel;
import de.innovationgate.wga.model.KeyValueBean;
public class WGContentItemDefinitionModel extends AbstractGenesisBoundObject<WGContentItemDefinition> {
protected WGASchemaDefinitionModel _model;
public WGContentItemDefinitionModel(WGASchemaDefinitionModel model) {
_model = model;
}
public String getContentTypeName() {
WGContentTypeDefinition def = _model.findContentTypeDefinition(_bean);
if (def != null) {
return def.getName();
}
return null;
}
public static class Type extends KeyValueBean<WGContentItemDefinition.Type, String> {
public Type(WGContentItemDefinition.Type key, String value) {
super(key, value);
}
}
public static final Map<WGContentItemDefinition.Type, Type> TYPES = new HashMap<WGContentItemDefinition.Type, Type>();
static {
TYPES.put(WGContentItemDefinition.Type.BOOLEAN, new Type(WGContentItemDefinition.Type.BOOLEAN, "Boolean"));
TYPES.put(WGContentItemDefinition.Type.DATE, new Type(WGContentItemDefinition.Type.DATE, "Date"));
TYPES.put(WGContentItemDefinition.Type.NUMBER, new Type(WGContentItemDefinition.Type.NUMBER, "Number"));
TYPES.put(WGContentItemDefinition.Type.TEXT, new Type(WGContentItemDefinition.Type.TEXT, "Text"));
}
@DataProvider(objectField = "type")
public List<Type> populateTypes() {
List<Type> list = new ArrayList<Type>(TYPES.values());
return WGUtils.sortByProperty(list, "value");
}
public List getInitialValues() {
if (_bean.getInitialValue() != null && _bean.getInitialValue() instanceof List) {
return (List)_bean.getInitialValue();
} else {
List<Object> values = new ArrayList<Object>();
if (_bean.getInitialValue() != null) {
values.add(_bean.getInitialValue());
}
_bean.setInitialValueList(values);
return values;
}
}
public String getName() {
return _bean.getName();
}
public Type getType() {
return TYPES.get(_bean.getType());
}
public boolean isList() {
return _bean.isList();
}
public void setInitialValues(List<Object> values) {
_bean.setInitialValueList(values);
if (values != null && values.size() > 1) {
_bean.setList(true);
}
}
public void setList(boolean list) {
_bean.setList(list);
fireBeanChanged();
}
public void setName(String name) {
_bean.setName(name);
fireBeanChanged();
}
public void setType(Type type) {
_bean.setType((WGContentItemDefinition.Type)retrieveKey(type, TYPES));
fireBeanChanged();
}
@SuppressWarnings("unchecked")
private Object retrieveKey(Object value, Map map) {
Iterator keys = map.keySet().iterator();
while (keys.hasNext()) {
Object key = keys.next();
Object mappedValue = map.get(key);
if (value.equals(mappedValue)) {
return key;
}
}
return null;
}
}