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, Cloneable {
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    @Override
045    public String getFieldName() {
046        return fieldName;
047    }
048
049    @Override
050    public Directory getSourceDirectory() throws DirectoryException {
051        if (sourceDirectory == null) {
052            sourceDirectory = Framework.getService(DirectoryService.class).getDirectory(sourceDirectoryName);
053        }
054        return sourceDirectory;
055    }
056
057    @Override
058    public void setSourceDirectoryName(String sourceDirectoryName) {
059        sourceDirectory = null;
060        this.sourceDirectoryName = sourceDirectoryName;
061    }
062
063    @Override
064    public Directory getTargetDirectory() throws DirectoryException {
065        if (targetDirectory == null) {
066            targetDirectory = Framework.getService(DirectoryService.class).getDirectory(targetDirectoryName);
067        }
068        return targetDirectory;
069    }
070
071    @Override
072    public void setTargetDirectoryName(String targetDirectoryName) {
073        targetDirectory = null;
074        this.targetDirectoryName = targetDirectoryName;
075    }
076
077    /**
078     * @since 5.6
079     */
080    @Override
081    public AbstractReference clone() {
082        AbstractReference clone;
083        try {
084            clone = (AbstractReference) super.clone();
085        } catch (CloneNotSupportedException e) {
086            throw new AssertionError(e);
087        }
088        // basic fields are already copied by super.clone()
089        return clone;
090    }
091
092}