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 *     bstefanescu
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.platform.rendering.template;
023
024import java.util.Collection;
025import java.util.HashMap;
026import java.util.Map;
027
028import org.nuxeo.ecm.core.api.DocumentModel;
029
030/**
031 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
032 */
033public abstract class DocumentFieldAccessor {
034
035    protected final String name;
036
037    public static final Map<String, DocumentFieldAccessor> accessors = new HashMap<String, DocumentFieldAccessor>();
038
039    protected DocumentFieldAccessor(String name) {
040        this.name = name;
041        accessors.put(name, this);
042    }
043
044    public static DocumentFieldAccessor get(String name) {
045        return accessors.get(name);
046    }
047
048    public static void put(DocumentFieldAccessor accessor) {
049        accessors.put(accessor.name, accessor);
050    }
051
052    public static Collection<String> getFieldNames() {
053        return accessors.keySet();
054    }
055
056    public static Collection<DocumentFieldAccessor> getAcessors() {
057        return accessors.values();
058    }
059
060    public static int getAcessorsCount() {
061        return accessors.size();
062    }
063
064    public String getName() {
065        return name;
066    }
067
068    public abstract Object getValue(DocumentModel doc);
069
070    public static DocumentFieldAccessor ID = new DocumentFieldAccessor("id") {
071        @Override
072        public Object getValue(DocumentModel doc) {
073            return doc.getId();
074        }
075    };
076
077    public static DocumentFieldAccessor NAME = new DocumentFieldAccessor("name") {
078        @Override
079        public Object getValue(DocumentModel doc) {
080            return doc.getName();
081        }
082    };
083
084    public static DocumentFieldAccessor TYPE = new DocumentFieldAccessor("type") {
085        @Override
086        public Object getValue(DocumentModel doc) {
087            return doc.getType();
088        }
089    };
090
091    public static DocumentFieldAccessor PATH = new DocumentFieldAccessor("path") {
092        @Override
093        public Object getValue(DocumentModel doc) {
094            return doc.getPathAsString();
095        }
096    };
097
098    public static DocumentFieldAccessor FACETS = new DocumentFieldAccessor("facets") {
099        @Override
100        public Object getValue(DocumentModel doc) {
101            return doc.getFacets();
102        }
103    };
104
105    public static DocumentFieldAccessor SCHEMAS = new DocumentFieldAccessor("schemas") {
106        @Override
107        public Object getValue(DocumentModel doc) {
108            return doc.getSchemas();
109        }
110    };
111
112    public static DocumentFieldAccessor SYSTEM = new DocumentFieldAccessor("system") {
113        @Override
114        public Object getValue(DocumentModel doc) {
115            return null; // TODO
116        }
117    };
118
119}