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.elements;
023
024import java.net.URL;
025import java.util.List;
026
027import org.nuxeo.theme.nodes.AbstractNode;
028import org.nuxeo.theme.nodes.Node;
029import org.nuxeo.theme.nodes.NodeTypeFamily;
030
031public abstract class AbstractElement extends AbstractNode implements Element {
032
033    private ElementType elementType;
034
035    private Integer uid;
036
037    private String name;
038
039    private String description;
040
041    private String cssClassName;
042
043    public Integer getUid() {
044        return uid;
045    }
046
047    public void setUid(final Integer uid) {
048        this.uid = uid;
049    }
050
051    public ElementType getElementType() {
052        return elementType;
053    }
054
055    public void setElementType(final ElementType elementType) {
056        this.elementType = elementType;
057    }
058
059    @Override
060    public NodeTypeFamily getNodeTypeFamily() {
061        return elementType.getNodeTypeFamily();
062    }
063
064    public String hash() {
065        if (uid == null) {
066            return null;
067        }
068        return uid.toString();
069    }
070
071    public List<Node> getChildrenInContext(final URL themeURL) {
072        return getChildren();
073    }
074
075    public String getName() {
076        return name;
077    }
078
079    public void setName(final String name) {
080        this.name = name;
081    }
082
083    public String getDescription() {
084        return description;
085    }
086
087    public void setDescription(final String description) {
088        this.description = description;
089    }
090
091    public boolean isEmpty() {
092        return !hasChildren();
093    }
094
095    @Override
096    public String getCssClassName() {
097        return cssClassName;
098    }
099
100    @Override
101    public void setCssClassName(String cssClassName) {
102        this.cssClassName = cssClassName;
103    }
104
105    public String computeXPath() {
106        final StringBuilder s = new StringBuilder();
107        String typeName = null;
108        Element e = this;
109        do {
110            Integer order = e.getOrder();
111            if (order != null) {
112                order += 1;
113                s.insert(0, "[" + order + ']');
114            }
115            typeName = e.getElementType().getTypeName();
116            if (typeName.equals("theme")) {
117                break;
118            }
119            s.insert(0, typeName);
120            if (typeName.equals("page")) {
121                break;
122            }
123            s.insert(0, '/');
124            e = (Element) e.getParent();
125        } while (e != null);
126        return s.toString();
127    }
128
129}