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 String sourceDirectoryName;
035
036    protected Directory sourceDirectory;
037
038    protected String targetDirectoryName;
039
040    protected Directory targetDirectory;
041
042    protected String fieldName;
043
044    /**
045     * @since 9.2
046     */
047    public AbstractReference(String fieldName, String targetDirectoryName) {
048        this.fieldName = fieldName;
049        this.targetDirectoryName = targetDirectoryName;
050    }
051
052    @Override
053    public String getFieldName() {
054        return fieldName;
055    }
056
057    @Override
058    public Directory getSourceDirectory() {
059        if (sourceDirectory == null) {
060            sourceDirectory = Framework.getService(DirectoryService.class).getDirectory(sourceDirectoryName);
061        }
062        return sourceDirectory;
063    }
064
065    @Override
066    public void setSourceDirectoryName(String sourceDirectoryName) {
067        sourceDirectory = null;
068        this.sourceDirectoryName = sourceDirectoryName;
069    }
070
071    @Override
072    public Directory getTargetDirectory() {
073        if (targetDirectory == null) {
074            targetDirectory = Framework.getService(DirectoryService.class).getDirectory(targetDirectoryName);
075        }
076        return targetDirectory;
077    }
078
079    @Override
080    public void setTargetDirectoryName(String targetDirectoryName) {
081        targetDirectory = null;
082        this.targetDirectoryName = targetDirectoryName;
083    }
084}