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