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.Collections;
020import java.util.List;
021
022import org.ametys.core.user.UserIdentity;
023import org.ametys.core.util.DateUtils;
024import org.ametys.plugins.explorer.resources.Resource;
025import org.ametys.runtime.i18n.I18nizableText;
026
027/**
028 * Implementation of a {@link CartElement} wrapping a {@link Resource}
029 *
030 */
031public class ResourceElement implements CartElement
032{
033    private Resource _resource;
034    
035    /**
036     * Constructor
037     * @param resource The resource
038     */
039    public ResourceElement (Resource resource)
040    {
041        _resource = resource;
042    }
043    
044    @Override
045    public String getId()
046    {
047        return _resource.getId();
048    }
049    
050    @Override
051    public I18nizableText getTitle()
052    {
053        return new I18nizableText(_resource.getName());
054    }
055
056    @Override
057    public I18nizableText getDescription()
058    {
059        return new I18nizableText(_resource.getName());
060    }
061    
062    @Override
063    public List<I18nizableText> getGroups()
064    {
065        return Collections.singletonList(new I18nizableText("plugin.cart", "PLUGINS_CART_UITOOL_CART_RESOURCES_GROUP"));
066    }
067    
068    @Override
069    public ZonedDateTime getLastModified()
070    {
071        return DateUtils.asZonedDateTime(_resource.getLastModified());
072    }
073
074    @Override
075    public UserIdentity getLastContributor()
076    {
077        return _resource.getLastContributor();
078    }
079    
080    @Override
081    public String getType()
082    {
083        return "resource";
084    }
085    
086    @Override
087    public String getGlyphIcon()
088    {
089        int index = _resource.getName().lastIndexOf(".");
090        String extension = _resource.getName().substring(index + 1);
091        
092        switch (extension) 
093        {
094            case "css":
095                return "ametysicon-css5";
096            case "csv":
097                return "ametysicon-file-extension-csv";
098            case "doc":
099                return "ametysicon-file-extension-doc";
100            case "docx":
101                return "ametysicon-file-extension-docx";
102            case "html":
103            case "htm":
104            case "xhtml":
105                return "ametysicon-file-extension-html";
106            case "png":
107            case "jpg":
108            case "jpeg":
109            case "bmp":
110            case "gif":
111                return "ametysicon-image2";
112            case "pdf":
113                return "ametysicon-pdf17";
114            case "ppt":
115                return "ametysicon-ppt";
116            case "pptx":
117                return "ametysicon-pptx";
118            case "txt":
119                return "ametysicon-txt";
120            case "avi":
121            case "mov":
122            case "swf":
123            case "wmv":
124            case "flv":
125            case "mpeg":
126            case "mpg":
127                return "ametysicon-movie16";
128            case "xml":
129                return "ametysicon-xml6";
130            case "xls":
131                return "ametysicon-xls1";
132            case "xlsx":
133                return "ametysicon-xlsx";
134            case "zip":
135                return "ametysicon-zip6";
136            case "rar":
137            case "odp":
138                return "ametysicon-odp2";
139            case "odt":
140                return "ametysicon-odt5";
141            case "jar":
142                return "ametysicon-jar3";
143            case "pps":
144                return "ametysicon-pps2";
145            case "psd":
146                return "ametysicon-psd3";
147            case "tar":
148            case "tgz":
149                return "ametysicon-tar1";
150            case "mp3":
151            case "mod":
152            case "mid":
153                return "ametysicon-music168";
154            default:
155                return "ametysicon-question-mark1";
156        }
157    }
158    
159    @Override
160    public String getSmallIcon()
161    {
162        return null;
163    }
164
165    @Override
166    public String getMediumIcon()
167    {
168        return null;
169    }
170
171    /**
172     * Get the resource
173     * @return The resource
174     */
175    public Resource getResource()
176    {
177        return _resource;
178    }
179}