001/*
002 * (C) Copyright 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.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 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.directory;
021
022import org.nuxeo.ecm.directory.api.DirectoryService;
023import org.nuxeo.runtime.api.Framework;
024
025/**
026 * Implementation of common Reference logic.
027 *
028 * @author ogrisel
029 */
030public abstract class AbstractReference implements Reference {
031
032    protected DirectoryServiceImpl directoryService;
033
034    protected String sourceDirectoryName;
035
036    protected Directory sourceDirectory;
037
038    protected String targetDirectoryName;
039
040    protected Directory targetDirectory;
041
042    protected String fieldName;
043
044    public String getFieldName() {
045        return fieldName;
046    }
047
048    public Directory getSourceDirectory() throws DirectoryException {
049        if (sourceDirectory == null) {
050            sourceDirectory = getDirectoryService().getDirectory(sourceDirectoryName);
051        }
052        return sourceDirectory;
053    }
054
055    public void setSourceDirectoryName(String sourceDirectoryName) {
056        sourceDirectory = null;
057        this.sourceDirectoryName = sourceDirectoryName;
058    }
059
060    public Directory getTargetDirectory() throws DirectoryException {
061        if (targetDirectory == null) {
062            targetDirectory = getDirectoryService().getDirectory(targetDirectoryName);
063        }
064        return targetDirectory;
065    }
066
067    public void setTargetDirectoryName(String targetDirectoryName) {
068        targetDirectory = null;
069        this.targetDirectoryName = targetDirectoryName;
070    }
071
072    protected DirectoryServiceImpl getDirectoryService() {
073        if (directoryService == null) {
074            directoryService = (DirectoryServiceImpl) Framework.getRuntime().getComponent(DirectoryService.NAME);
075        }
076        return directoryService;
077    }
078
079    /**
080     * @since 5.6
081     */
082    public AbstractReference clone() {
083        AbstractReference clone = newInstance();
084        clone.sourceDirectoryName = sourceDirectoryName;
085        clone.targetDirectoryName = targetDirectoryName;
086        clone.fieldName = fieldName;
087        return clone;
088    }
089
090    /**
091     * Override to instantiate sub class, used in {@link #clone()} method
092     *
093     * @since 5.6
094     */
095    protected abstract AbstractReference newInstance();
096
097}