001/*
002 * (C) Copyright 2006-2008 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.webengine.model.impl;
021
022import org.nuxeo.common.xmap.annotation.XNode;
023import org.nuxeo.common.xmap.annotation.XNodeList;
024import org.nuxeo.common.xmap.annotation.XObject;
025import org.nuxeo.ecm.webengine.loader.ClassProxy;
026import org.nuxeo.ecm.webengine.loader.StaticClassProxy;
027import org.nuxeo.ecm.webengine.model.ResourceType;
028import org.nuxeo.ecm.webengine.model.Utils;
029import org.nuxeo.ecm.webengine.model.WebAdapter;
030
031/**
032 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
033 */
034@XObject("web-adapter")
035public class AdapterDescriptor extends TypeDescriptor {
036
037    @XNode("@class")
038    void setClass(Class<?> clazz) {
039        this.clazz = new StaticClassProxy(clazz);
040    }
041
042    @XNode("@type")
043    void setType(String type) {
044        this.type = type;
045    }
046
047    @XNode("@name")
048    public String name;
049
050    @XNode("@fragment")
051    void setFragment(String fragment) {
052        this.fragment = fragment;
053    }
054
055    @XNode("@superType")
056    void setSuperType(String superType) {
057        this.superType = superType;
058    }
059
060    @XNode(value = "targetType")
061    public String targetType = ResourceType.ROOT_TYPE_NAME;
062
063    @XNodeList(value = "facet", type = String[].class, componentType = String.class, nullByDefault = true)
064    public String[] facets;
065
066    public AdapterDescriptor() {
067    }
068
069    public AdapterDescriptor(ClassProxy clazz, String name, String type, String superType) {
070        super(clazz, type, superType);
071        this.name = name;
072    }
073
074    public AdapterDescriptor(ClassProxy clazz, String name, String type, String superType, String targetType,
075            String[] facets) {
076        super(clazz, type, superType);
077        this.name = name;
078        if (facets != null && facets.length > 0) {
079            this.facets = facets;
080        }
081        if (targetType == null || "*".equals(targetType)) {
082            this.targetType = ResourceType.ROOT_TYPE_NAME;
083        } else {
084            this.targetType = targetType;
085        }
086    }
087
088    @Override
089    public boolean isAdapter() {
090        return true;
091    }
092
093    @Override
094    public boolean equals(Object obj) {
095        if (obj == this) {
096            return true;
097        }
098        if (obj == null) {
099            return false;
100        }
101        if (obj instanceof AdapterDescriptor) {
102            AdapterDescriptor td = (AdapterDescriptor) obj;
103            return type.equals(td.type) && Utils.streq(fragment, td.fragment);
104        }
105        return false;
106    }
107
108    @Override
109    public String getId() {
110        return type;
111    }
112
113    @Override
114    public String getFragment() {
115        return fragment;
116    }
117
118    @Override
119    public boolean isMainFragment() {
120        return fragment == null;
121    }
122
123    @Override
124    public AdapterDescriptor asAdapterDescriptor() {
125        return this;
126    }
127
128    public static AdapterDescriptor fromAnnotation(ClassProxy clazz, WebAdapter type) {
129        return new AdapterDescriptor(clazz, type.name(), type.type(), type.superType(), type.targetType(),
130                type.facets());
131    }
132
133}