001/*
002 * (C) Copyright 2006-2010 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Thierry Delprat
016 */
017package org.nuxeo.apidoc.doc;
018
019import java.util.ArrayList;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023
024import javax.ws.rs.GET;
025import javax.ws.rs.Path;
026import javax.ws.rs.PathParam;
027import javax.ws.rs.Produces;
028
029import org.nuxeo.apidoc.api.DocumentationItem;
030import org.nuxeo.apidoc.documentation.DocumentationService;
031import org.nuxeo.apidoc.search.ArtifactSearcher;
032import org.nuxeo.ecm.core.api.DocumentModel;
033import org.nuxeo.ecm.core.api.DocumentRef;
034import org.nuxeo.ecm.core.api.IdRef;
035import org.nuxeo.ecm.webengine.model.WebObject;
036import org.nuxeo.ecm.webengine.model.impl.DefaultObject;
037import org.nuxeo.runtime.api.Framework;
038
039@WebObject(type = "documentation")
040public class DocumentationWO extends DefaultObject {
041
042    @GET
043    @Produces("text/html")
044    public Object viewAll() {
045        DocumentationService ds = Framework.getLocalService(DocumentationService.class);
046        Map<String, List<DocumentationItem>> docs = ds.listDocumentationItems(getContext().getCoreSession(), null, null);
047        return getView("index").arg("distId", ctx.getProperty("distId")).arg("docsByCat", docs);
048    }
049
050    @GET
051    @Produces("text/html")
052    @Path("filter")
053    public Object filterAll() {
054        String fulltext = getContext().getForm().getFormProperty("fulltext");
055        DocumentationService ds = Framework.getLocalService(DocumentationService.class);
056
057        ArtifactSearcher searcher = Framework.getLocalService(ArtifactSearcher.class);
058        List<DocumentationItem> items = searcher.searchDocumentation(getContext().getCoreSession(),
059                (String) ctx.getProperty("distId"), fulltext, null);
060        Map<String, String> categories = ds.getCategories();
061
062        Map<String, List<DocumentationItem>> docs = new HashMap<String, List<DocumentationItem>>();
063
064        for (DocumentationItem item : items) {
065
066            String catKey = item.getType();
067            String catLabel = categories.get(catKey);
068            if (docs.containsKey(catLabel)) {
069                docs.get(catLabel).add(item);
070            } else {
071                List<DocumentationItem> itemList = new ArrayList<DocumentationItem>();
072                itemList.add(item);
073                docs.put(catLabel, itemList);
074            }
075        }
076        return getView("index").arg("distId", ctx.getProperty("distId")).arg("docsByCat", docs).arg("searchFilter",
077                fulltext);
078    }
079
080    @GET
081    @Produces("text/html")
082    @Path("view/{docUUID}")
083    public Object viewDoc(@PathParam("docUUID") String docUUID) {
084        DocumentRef docRef = new IdRef(docUUID);
085        DocumentModel docModel = getContext().getCoreSession().getDocument(docRef);
086        DocumentationItem doc = docModel.getAdapter(DocumentationItem.class);
087
088        return getView("viewSingleDoc").arg("distId", ctx.getProperty("distId")).arg("doc", doc);
089    }
090
091}