001/*
002 *  Copyright 2013 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.plugins.cart;
017
018import java.time.ZonedDateTime;
019import java.util.ArrayList;
020import java.util.List;
021
022import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
023import org.ametys.cms.contenttype.ContentTypesHelper;
024import org.ametys.cms.repository.Content;
025import org.ametys.core.user.UserIdentity;
026import org.ametys.runtime.i18n.I18nizableText;
027
028/**
029 * Implementation of a {@link CartElement} wrapping a {@link Content}
030 *
031 */
032public class ContentElement implements CartElement
033{
034    private Content _content;
035    private ContentTypesHelper _cTypesHelper;
036    private ContentTypeExtensionPoint _cTypesEP;
037    
038    /**
039     * Constructor
040     * @param content The content
041     * @param cTypesHelper The content types helper
042     * @param cTypesEP The extension point for content types
043     */
044    public ContentElement (Content content, ContentTypesHelper cTypesHelper, ContentTypeExtensionPoint cTypesEP)
045    {
046        _content = content; 
047        _cTypesHelper = cTypesHelper;
048        _cTypesEP = cTypesEP;
049    }
050    
051    @Override
052    public String getId()
053    {
054        return _content.getId();
055    }
056
057    @Override
058    public I18nizableText getTitle()
059    {
060        return new I18nizableText(_content.getTitle());
061    }
062    
063    @Override
064    public I18nizableText getDescription()
065    {
066        return new I18nizableText(_content.getTitle());
067    }
068
069    public List<I18nizableText> getGroups ()
070    {
071        List<I18nizableText> cTypesLabel = new ArrayList<>();
072        
073        for (String cTypeId : _content.getTypes())
074        {
075            cTypesLabel.add(_cTypesEP.getExtension(cTypeId).getLabel());
076        }
077        return cTypesLabel;
078    }
079    
080    @Override
081    public ZonedDateTime getLastModified()
082    {
083        return _content.getLastModified();
084    }
085
086    @Override
087    public UserIdentity getLastContributor()
088    {
089        return _content.getLastContributor();
090    }
091
092    @Override
093    public String getType()
094    {
095        return "content";
096    }
097    
098    @Override
099    public String getGlyphIcon()
100    {
101        return _cTypesHelper.getIconGlyph(_content);
102    }
103    
104    @Override
105    public String getSmallIcon()
106    {
107        return _cTypesHelper.getSmallIcon(_content);
108    }
109
110    @Override
111    public String getMediumIcon()
112    {
113        return _cTypesHelper.getMediumIcon(_content);
114    }
115
116    /**
117     * Get the content
118     * @return The content
119     */
120    public Content getContent()
121    {
122        return _content;
123    }
124}