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            @Override
048            public Resource getResource(WebDAVRequest webDAVRequest)
049                    throws WebDAVException {
050    
051                    return DDMWebDavUtil.getResource(
052                            webDAVRequest, getRootPath(), getToken(),
053                            PortalUtil.getClassNameId(DDLRecordSet.class));
054            }
055    
056            @Override
057            public List<Resource> getResources(WebDAVRequest webDAVRequest)
058                    throws WebDAVException {
059    
060                    try {
061                            String[] pathArray = webDAVRequest.getPathArray();
062    
063                            if (pathArray.length == 2) {
064                                    return getFolders(webDAVRequest);
065                            }
066                            else if (pathArray.length == 3) {
067                                    String type = pathArray[2];
068    
069                                    if (type.equals(DDMWebDavUtil.TYPE_STRUCTURES)) {
070                                            return getStructures(webDAVRequest);
071                                    }
072                                    else if (type.equals(DDMWebDavUtil.TYPE_TEMPLATES)) {
073                                            return getTemplates(webDAVRequest);
074                                    }
075                            }
076    
077                            return new ArrayList<Resource>();
078                    }
079                    catch (Exception e) {
080                            throw new WebDAVException(e);
081                    }
082            }
083    
084            @Override
085            public int putResource(WebDAVRequest webDAVRequest) throws WebDAVException {
086                    return DDMWebDavUtil.putResource(
087                            webDAVRequest, getRootPath(), getToken(),
088                            PortalUtil.getClassNameId(DDLRecordSet.class));
089            }
090    
091            protected List<Resource> getFolders(WebDAVRequest webDAVRequest)
092                    throws Exception {
093    
094                    List<Resource> resources = new ArrayList<Resource>();
095    
096                    resources.add(
097                            DDMWebDavUtil.toResource(
098                                    webDAVRequest, DDMWebDavUtil.TYPE_STRUCTURES, getRootPath(),
099                                    true));
100                    resources.add(
101                            DDMWebDavUtil.toResource(
102                                    webDAVRequest, DDMWebDavUtil.TYPE_TEMPLATES, getRootPath(),
103                                    true));
104    
105                    return resources;
106            }
107    
108            protected List<Resource> getStructures(WebDAVRequest webDAVRequest)
109                    throws Exception {
110    
111                    List<Resource> resources = new ArrayList<Resource>();
112    
113                    List<DDMStructure> ddmStructures =
114                            DDMStructureLocalServiceUtil.getStructures(
115                                    webDAVRequest.getGroupId(),
116                                    PortalUtil.getClassNameId(DDLRecordSet.class));
117    
118                    for (DDMStructure ddmStructure : ddmStructures) {
119                            Resource resource = DDMWebDavUtil.toResource(
120                                    webDAVRequest, ddmStructure, getRootPath(), true);
121    
122                            resources.add(resource);
123                    }
124    
125                    return resources;
126            }
127    
128            protected List<Resource> getTemplates(WebDAVRequest webDAVRequest)
129                    throws Exception {
130    
131                    List<Resource> resources = new ArrayList<Resource>();
132    
133                    List<DDMTemplate> ddmTemplates =
134                            DDMTemplateLocalServiceUtil.getTemplatesByStructureClassNameId(
135                                    webDAVRequest.getGroupId(),
136                                    PortalUtil.getClassNameId(DDLRecordSet.class),
137                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
138    
139                    for (DDMTemplate ddmTemplate : ddmTemplates) {
140                            Resource resource = DDMWebDavUtil.toResource(
141                                    webDAVRequest, ddmTemplate, getRootPath(), true);
142    
143                            resources.add(resource);
144                    }
145    
146                    return resources;
147            }
148    
149    }