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