001/*
002 *  Copyright 2019 Anyware Services
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 */
016package org.ametys.cms.data;
017
018import java.util.Collection;
019
020import javax.jcr.RepositoryException;
021import javax.jcr.Session;
022
023import org.ametys.cms.data.type.AbstractContentElementType;
024import org.ametys.cms.repository.ModifiableContent;
025import org.ametys.plugins.repository.AmetysObjectResolver;
026import org.ametys.plugins.repository.AmetysRepositoryException;
027import org.ametys.plugins.repository.UnknownAmetysObjectException;
028import org.ametys.plugins.repository.data.holder.ModifiableModelAwareDataHolder;
029import org.ametys.plugins.repository.data.holder.group.impl.ModifiableModelAwareComposite;
030import org.ametys.plugins.repository.data.holder.group.impl.ModifiableModelAwareRepeater;
031import org.ametys.runtime.model.exception.BadItemTypeException;
032import org.ametys.runtime.model.exception.UndefinedItemPathException;
033import org.ametys.runtime.model.type.ModelItemType;
034
035/**
036 * Content wrapper used by attributes of type content
037 * @see AbstractContentElementType
038 */
039public class ContentValue implements ModifiableModelAwareDataHolder
040{
041    private AmetysObjectResolver _resolver;
042    private String _contentId;
043    private ModifiableContent _content;
044    private Session _session;
045
046    /**
047     * Constructor of the content wrapper
048     * @param resolver resolver used to get the content from its identifier
049     * @param contentId content identifier
050     */
051    public ContentValue(AmetysObjectResolver resolver, String contentId)
052    {
053        this(resolver, contentId, null);
054    }
055
056    /**
057     * Constructor of the content wrapper
058     * @param resolver resolver used to get the content from its identifier
059     * @param contentId content identifier
060     * @param session the current session. If <code>null</code>, a new session will be used to retrieve the content 
061     */
062    public ContentValue(AmetysObjectResolver resolver, String contentId, Session session)
063    {
064        _resolver = resolver;
065        _contentId = contentId;
066        _session = session;
067    }
068    
069    /**
070     * Retrieves the content's identifier
071     * @return the content's identifier
072     */
073    public String getContentId()
074    {
075        return _contentId;
076    }
077    
078    /**
079     * Retrieves the content
080     * @return the content
081     * @throws AmetysRepositoryException if an error occurs.
082     * @throws UnknownAmetysObjectException if no content exists for the identifier
083     */
084    public ModifiableContent getContent() throws AmetysRepositoryException, UnknownAmetysObjectException
085    {
086        if (_content == null)
087        {
088            if (_session != null)
089            {
090                try
091                {
092                    _content = _resolver.resolveById(_contentId, _session);
093                }
094                catch (RepositoryException e)
095                {
096                    throw new AmetysRepositoryException("Unable to retrieve the content with the id '" + _contentId + "'.", e);
097                }
098            }
099            else
100            {
101                _content = _resolver.resolveById(_contentId);
102            }
103        }
104        
105        return _content; 
106    }
107
108    public boolean hasValue(String dataPath) throws IllegalArgumentException
109    {
110        return getContent().hasValue(dataPath);
111    }
112
113    public Collection<String> getDataNames()
114    {
115        return getContent().getDataNames();
116    }
117
118    public <T> T getValue(String dataPath) throws IllegalArgumentException, UndefinedItemPathException, BadItemTypeException
119    {
120        return getContent().getValue(dataPath);
121    }
122
123    public <T> T getValue(String dataPath, boolean useDefaultFromModel, T defaultValue) throws IllegalArgumentException, UndefinedItemPathException, BadItemTypeException
124    {
125        return getContent().getValue(dataPath, useDefaultFromModel, defaultValue);
126    }
127
128    public boolean isMultiple(String path) throws IllegalArgumentException, UndefinedItemPathException, BadItemTypeException
129    {
130        return getContent().isMultiple(path);
131    }
132
133    public ModelItemType getType(String path) throws IllegalArgumentException, UndefinedItemPathException
134    {
135        return getContent().getType(path);
136    }
137
138    public ModifiableModelAwareComposite getComposite(String compositePath) throws IllegalArgumentException, UndefinedItemPathException, BadItemTypeException
139    {
140        return getContent().getComposite(compositePath);
141    }
142
143    public ModifiableModelAwareRepeater getRepeater(String repeaterPath) throws IllegalArgumentException, UndefinedItemPathException, BadItemTypeException
144    {
145        return getContent().getRepeater(repeaterPath);
146    }
147
148    public ModifiableModelAwareComposite getComposite(String compositePath, boolean createNew) throws IllegalArgumentException, UndefinedItemPathException, BadItemTypeException
149    {
150        return getContent().getComposite(compositePath, createNew);
151    }
152
153    public ModifiableModelAwareRepeater getRepeater(String repeaterPath, boolean createNew) throws IllegalArgumentException, UndefinedItemPathException, BadItemTypeException
154    {
155        return getContent().getRepeater(repeaterPath, createNew);
156    }
157
158    public void setValue(String dataPath, Object value) throws IllegalArgumentException, UndefinedItemPathException, BadItemTypeException
159    {
160        getContent().setValue(dataPath, value);
161    }
162
163    public void removeValue(String dataPath) throws IllegalArgumentException, UndefinedItemPathException, BadItemTypeException
164    {
165        getContent().removeValue(dataPath);
166    }
167}