001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     bstefanescu
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.rendering.template;
021
022import java.util.Collection;
023import java.util.HashMap;
024import java.util.Map;
025
026import org.nuxeo.ecm.core.api.DocumentModel;
027
028/**
029 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
030 */
031public abstract class DocumentFieldAccessor {
032
033    protected final String name;
034
035    public static final Map<String, DocumentFieldAccessor> accessors = new HashMap<String, DocumentFieldAccessor>();
036
037    protected DocumentFieldAccessor(String name) {
038        this.name = name;
039        accessors.put(name, this);
040    }
041
042    public static DocumentFieldAccessor get(String name) {
043        return accessors.get(name);
044    }
045
046    public static void put(DocumentFieldAccessor accessor) {
047        accessors.put(accessor.name, accessor);
048    }
049
050    public static Collection<String> getFieldNames() {
051        return accessors.keySet();
052    }
053
054    public static Collection<DocumentFieldAccessor> getAcessors() {
055        return accessors.values();
056    }
057
058    public static int getAcessorsCount() {
059        return accessors.size();
060    }
061
062    public String getName() {
063        return name;
064    }
065
066    public abstract Object getValue(DocumentModel doc);
067
068    public static DocumentFieldAccessor ID = new DocumentFieldAccessor("id") {
069        @Override
070        public Object getValue(DocumentModel doc) {
071            return doc.getId();
072        }
073    };
074
075    public static DocumentFieldAccessor NAME = new DocumentFieldAccessor("name") {
076        @Override
077        public Object getValue(DocumentModel doc) {
078            return doc.getName();
079        }
080    };
081
082    public static DocumentFieldAccessor TYPE = new DocumentFieldAccessor("type") {
083        @Override
084        public Object getValue(DocumentModel doc) {
085            return doc.getType();
086        }
087    };
088
089    public static DocumentFieldAccessor PATH = new DocumentFieldAccessor("path") {
090        @Override
091        public Object getValue(DocumentModel doc) {
092            return doc.getPathAsString();
093        }
094    };
095
096    public static DocumentFieldAccessor FACETS = new DocumentFieldAccessor("facets") {
097        @Override
098        public Object getValue(DocumentModel doc) {
099            return doc.getFacets();
100        }
101    };
102
103    public static DocumentFieldAccessor SCHEMAS = new DocumentFieldAccessor("schemas") {
104        @Override
105        public Object getValue(DocumentModel doc) {
106            return doc.getSchemas();
107        }
108    };
109
110    public static DocumentFieldAccessor SYSTEM = new DocumentFieldAccessor("system") {
111        @Override
112        public Object getValue(DocumentModel doc) {
113            return null; // TODO
114        }
115    };
116
117}