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.dynamicdatalists.webdav;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.webdav.BaseWebDAVStorageImpl;
019    import com.liferay.portal.kernel.webdav.Resource;
020    import com.liferay.portal.kernel.webdav.WebDAVException;
021    import com.liferay.portal.kernel.webdav.WebDAVRequest;
022    import com.liferay.portal.util.PortalUtil;
023    import com.liferay.portlet.dynamicdatalists.model.DDLRecordSet;
024    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
025    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
026    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
027    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
028    import com.liferay.portlet.dynamicdatamapping.webdav.DDMWebDavUtil;
029    
030    import java.util.ArrayList;
031    import java.util.List;
032    
033    /**
034     * @author Juan Fernández
035     */
036    public class DDLWebDAVStorageImpl extends BaseWebDAVStorageImpl {
037    
038            @Override
039            public int deleteResource(WebDAVRequest webDAVRequest)
040                    throws WebDAVException {
041    
042                    return DDMWebDavUtil.deleteResource(
043                            webDAVRequest, getRootPath(), getToken(),
044                            PortalUtil.getClassNameId(DDLRecordSet.class));
045            }
046    
047            public Resource getResource(WebDAVRequest webDAVRequest)
048                    throws WebDAVException {
049    
050                    return DDMWebDavUtil.getResource(
051                            webDAVRequest, getRootPath(), getToken(),
052                            PortalUtil.getClassNameId(DDLRecordSet.class));
053            }
054    
055            public List<Resource> getResources(WebDAVRequest webDAVRequest)
056                    throws WebDAVException {
057    
058                    try {
059                            String[] pathArray = webDAVRequest.getPathArray();
060    
061                            if (pathArray.length == 2) {
062                                    return getFolders(webDAVRequest);
063                            }
064                            else if (pathArray.length == 3) {
065                                    String type = pathArray[2];
066    
067                                    if (type.equals(DDMWebDavUtil.TYPE_STRUCTURES)) {
068                                            return getStructures(webDAVRequest);
069                                    }
070                                    else if (type.equals(DDMWebDavUtil.TYPE_TEMPLATES)) {
071                                            return getTemplates(webDAVRequest);
072                                    }
073                            }
074    
075                            return new ArrayList<Resource>();
076                    }
077                    catch (Exception e) {
078                            throw new WebDAVException(e);
079                    }
080            }
081    
082            @Override
083            public int putResource(WebDAVRequest webDAVRequest) throws WebDAVException {
084                    return DDMWebDavUtil.putResource(
085                            webDAVRequest, getRootPath(), getToken(),
086                            PortalUtil.getClassNameId(DDLRecordSet.class));
087            }
088    
089            protected List<Resource> getFolders(WebDAVRequest webDAVRequest)
090                    throws Exception {
091    
092                    List<Resource> resources = new ArrayList<Resource>();
093    
094                    resources.add(
095                            DDMWebDavUtil.toResource(
096                                    webDAVRequest, DDMWebDavUtil.TYPE_STRUCTURES, getRootPath(),
097                                    true));
098                    resources.add(
099                            DDMWebDavUtil.toResource(
100                                    webDAVRequest, DDMWebDavUtil.TYPE_TEMPLATES, getRootPath(),
101                                    true));
102    
103                    return resources;
104            }
105    
106            protected List<Resource> getStructures(WebDAVRequest webDAVRequest)
107                    throws Exception {
108    
109                    List<Resource> resources = new ArrayList<Resource>();
110    
111                    List<DDMStructure> ddmStructures =
112                            DDMStructureLocalServiceUtil.getStructures(
113                                    webDAVRequest.getGroupId(),
114                                    PortalUtil.getClassNameId(DDLRecordSet.class));
115    
116                    for (DDMStructure ddmStructure : ddmStructures) {
117                            Resource resource = DDMWebDavUtil.toResource(
118                                    webDAVRequest, ddmStructure, getRootPath(), true);
119    
120                            resources.add(resource);
121                    }
122    
123                    return resources;
124            }
125    
126            protected List<Resource> getTemplates(WebDAVRequest webDAVRequest)
127                    throws Exception {
128    
129                    List<Resource> resources = new ArrayList<Resource>();
130    
131                    List<DDMTemplate> ddmTemplates =
132                            DDMTemplateLocalServiceUtil.getTemplatesByStructureClassNameId(
133                                    webDAVRequest.getGroupId(),
134                                    PortalUtil.getClassNameId(DDLRecordSet.class),
135                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
136    
137                    for (DDMTemplate ddmTemplate : ddmTemplates) {
138                            Resource resource = DDMWebDavUtil.toResource(
139                                    webDAVRequest, ddmTemplate, getRootPath(), true);
140    
141                            resources.add(resource);
142                    }
143    
144                    return resources;
145            }
146    
147    }