001/* 002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others. 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 * 016 * Contributors: 017 * Jean-Marc Orliaguet, Chalmers 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.theme.models; 023 024import java.util.ArrayList; 025import java.util.List; 026 027import org.nuxeo.theme.Manager; 028 029public abstract class AbstractModel implements Model { 030 031 private final List<Model> items = new ArrayList<Model>(); 032 033 public String getModelTypeName() { 034 ModelType modelType = getModelType(); 035 if (modelType == null) { 036 return null; 037 } 038 return modelType.getTypeName(); 039 } 040 041 public ModelType getModelType() { 042 final String className = this.getClass().getCanonicalName(); 043 return Manager.getThemeManager().getModelByClassname(className); 044 } 045 046 public Model addItem(Model model) throws ModelException { 047 ModelType modelType = getModelType(); 048 if (modelType == null) { 049 throw new ModelException("Model type not found: " + getModelTypeName()); 050 } 051 if (!getModelType().getAllowedTypes().contains(model.getModelTypeName())) { 052 throw new ModelException("Model type: " + model.getModelTypeName() + " not allowed in: " 053 + getModelTypeName()); 054 } 055 items.add(model); 056 return model; 057 } 058 059 public Model insertItem(int index, Model model) throws ModelException { 060 if (!getModelType().getAllowedTypes().contains(model.getModelTypeName())) { 061 throw new ModelException("Model type: " + model.getModelTypeName() + " not allowed in: " 062 + getModelTypeName()); 063 } 064 items.add(index, model); 065 return model; 066 } 067 068 public List<Model> getItems() { 069 return items; 070 } 071 072 public boolean hasItems() { 073 return !items.isEmpty(); 074 } 075}