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