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            public Resource getResource(WebDAVRequest webDAVRequest)
043                    throws WebDAVException {
044    
045                    return DDMWebDavUtil.getResource(
046                            webDAVRequest, getRootPath(), getToken(), 0);
047            }
048    
049            public List<Resource> getResources(WebDAVRequest webDAVRequest)
050                    throws WebDAVException {
051    
052                    try {
053                            String[] pathArray = webDAVRequest.getPathArray();
054    
055                            if (pathArray.length == 2) {
056                                    return getFolders(webDAVRequest);
057                            }
058                            else if (pathArray.length == 3) {
059                                    return getTemplates(webDAVRequest);
060                            }
061    
062                            return new ArrayList<Resource>();
063                    }
064                    catch (Exception e) {
065                            throw new WebDAVException(e);
066                    }
067            }
068    
069            @Override
070            public int putResource(WebDAVRequest webDAVRequest) throws WebDAVException {
071                    return DDMWebDavUtil.putResource(
072                            webDAVRequest, getRootPath(), getToken(), 0);
073            }
074    
075            protected List<Resource> getFolders(WebDAVRequest webDAVRequest)
076                    throws Exception {
077    
078                    List<Resource> resources = new ArrayList<Resource>();
079    
080                    resources.add(
081                            DDMWebDavUtil.toResource(
082                                    webDAVRequest, DDMWebDavUtil.TYPE_TEMPLATES, getRootPath(),
083                                    true));
084    
085                    return resources;
086            }
087    
088            protected List<Resource> getTemplates(WebDAVRequest webDAVRequest)
089                    throws Exception {
090    
091                    List<Resource> resources = new ArrayList<Resource>();
092    
093                    List<DDMTemplate> ddmTemplates =
094                            DDMTemplateLocalServiceUtil.getTemplatesByClassPK(
095                                    webDAVRequest.getGroupId(), 0);
096    
097                    for (DDMTemplate ddmTemplate : ddmTemplates) {
098                            Resource resource = DDMWebDavUtil.toResource(
099                                    webDAVRequest, ddmTemplate, getRootPath(), true);
100    
101                            resources.add(resource);
102                    }
103    
104                    return resources;
105            }
106    
107    }