001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.portletdisplaytemplate.webdav;
016    
017    import com.liferay.portal.kernel.webdav.BaseWebDAVStorageImpl;
018    import com.liferay.portal.kernel.webdav.Resource;
019    import com.liferay.portal.kernel.webdav.WebDAVException;
020    import com.liferay.portal.kernel.webdav.WebDAVRequest;
021    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
022    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
023    import com.liferay.portlet.dynamicdatamapping.webdav.DDMWebDavUtil;
024    
025    import java.util.ArrayList;
026    import java.util.List;
027    
028    /**
029     * @author Juan Fern??ndez
030     */
031    public class ApplicationDisplayTemplateWebDAVStorageImpl
032            extends BaseWebDAVStorageImpl {
033    
034            @Override
035            public int deleteResource(WebDAVRequest webDAVRequest)
036                    throws WebDAVException {
037    
038                    return DDMWebDavUtil.deleteResource(
039                            webDAVRequest, getRootPath(), getToken(), 0);
040            }
041    
042            @Override
043            public Resource getResource(WebDAVRequest webDAVRequest)
044                    throws WebDAVException {
045    
046                    return DDMWebDavUtil.getResource(
047                            webDAVRequest, getRootPath(), getToken(), 0);
048            }
049    
050            @Override
051            public List<Resource> getResources(WebDAVRequest webDAVRequest)
052                    throws WebDAVException {
053    
054                    try {
055                            String[] pathArray = webDAVRequest.getPathArray();
056    
057                            if (pathArray.length == 2) {
058                                    return getFolders(webDAVRequest);
059                            }
060                            else if (pathArray.length == 3) {
061                                    return getTemplates(webDAVRequest);
062                            }
063    
064                            return new ArrayList<Resource>();
065                    }
066                    catch (Exception e) {
067                            throw new WebDAVException(e);
068                    }
069            }
070    
071            @Override
072            public int putResource(WebDAVRequest webDAVRequest) throws WebDAVException {
073                    return DDMWebDavUtil.putResource(
074                            webDAVRequest, getRootPath(), getToken(), 0);
075            }
076    
077            protected List<Resource> getFolders(WebDAVRequest webDAVRequest)
078                    throws Exception {
079    
080                    List<Resource> resources = new ArrayList<Resource>();
081    
082                    resources.add(
083                            DDMWebDavUtil.toResource(
084                                    webDAVRequest, DDMWebDavUtil.TYPE_TEMPLATES, getRootPath(),
085                                    true));
086    
087                    return resources;
088            }
089    
090            protected List<Resource> getTemplates(WebDAVRequest webDAVRequest)
091                    throws Exception {
092    
093                    List<Resource> resources = new ArrayList<Resource>();
094    
095                    List<DDMTemplate> ddmTemplates =
096                            DDMTemplateLocalServiceUtil.getTemplatesByClassPK(
097                                    webDAVRequest.getGroupId(), 0);
098    
099                    for (DDMTemplate ddmTemplate : ddmTemplates) {
100                            Resource resource = DDMWebDavUtil.toResource(
101                                    webDAVRequest, ddmTemplate, getRootPath(), true);
102    
103                            resources.add(resource);
104                    }
105    
106                    return resources;
107            }
108    
109    }