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-2.1.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 *     Dragos Mihalache
016 */
017package org.nuxeo.ecm.core.uidgen;
018
019import java.util.Set;
020
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023import org.nuxeo.common.xmap.annotation.XNode;
024import org.nuxeo.common.xmap.annotation.XNodeList;
025import org.nuxeo.common.xmap.annotation.XObject;
026
027/**
028 * UID generator configuration holder.
029 */
030@XObject("generator")
031public class UIDGeneratorDescriptor {
032
033    private static final Log log = LogFactory.getLog(UIDGeneratorDescriptor.class);
034
035    private static final int DEFAULT_COUNTER_START = 1;
036
037    // @XNode
038    private String generationExpression;
039
040    // @XNode
041    private Set<?> generationCriteria;
042
043    // @XNode
044    private int counterStart;
045
046    @XNode("@name")
047    private String name;
048
049    @XNode("@class")
050    private String className;
051
052    // @XNode("propertyName")
053    // private String propertyName;
054    @XNodeList(value = "propertyName", type = String[].class, componentType = String.class)
055    private String[] propertyNames;
056
057    @XNodeList(value = "docType", type = String[].class, componentType = String.class)
058    private String[] docTypes;
059
060    /**
061     * Default constructor - used normally when created as an XObject.
062     */
063    public UIDGeneratorDescriptor() {
064        log.debug("<UIDGeneratorDescriptor:init>");
065    }
066
067    /**
068     * Explicit constructor.
069     */
070    public UIDGeneratorDescriptor(String generationExp, Set<?> generationCrit) {
071        this(generationExp, generationCrit, DEFAULT_COUNTER_START);
072    }
073
074    /**
075     * Explicit constructor.
076     */
077    public UIDGeneratorDescriptor(String generationExp, Set<?> generationCrit, int counterStart) {
078        generationExpression = generationExp;
079        generationCriteria = generationCrit;
080        this.counterStart = counterStart;
081    }
082
083    public String getClassName() {
084        return className;
085    }
086
087    public void setClassName(String className) {
088        this.className = className;
089    }
090
091    public String getName() {
092        return name;
093    }
094
095    public void setName(String name) {
096        this.name = name;
097    }
098
099    public String[] getDocTypes() {
100        return docTypes;
101    }
102
103    public void setDocTypes(String[] docTypes) {
104        this.docTypes = docTypes;
105    }
106
107    public int getCounterStart() {
108        return counterStart;
109    }
110
111    public Set<?> getGenerationCriteria() {
112        return generationCriteria;
113    }
114
115    public String getGenerationExpression() {
116        return generationExpression;
117    }
118
119    /**
120     * Kept for convenience. If there is only one property to be set with generated UID.
121     *
122     * @return first propertyName
123     */
124    public String getPropertyName() {
125        if (propertyNames.length == 0) {
126            log.warn("No propertyName specified");
127            return null;
128        }
129        return propertyNames[0];
130    }
131
132    /**
133     * Set the value as first property name. Kept for convenience. If there is only one property to be set with
134     * generated UID.
135     */
136    public void setPropertyName(String propertyName) {
137        if (propertyNames.length == 0) {
138            log.warn("Cannot set propertyName.");
139        }
140        propertyNames[0] = propertyName;
141    }
142
143    public String[] getPropertyNames() {
144        return propertyNames;
145    }
146
147    public void setPropertyNames(String[] propNames) {
148        propertyNames = propNames;
149    }
150
151}