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