001/*
002 * (C) Copyright 2006-2010 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Thierry Delprat
018 */
019package org.nuxeo.apidoc.doc;
020
021import java.util.ArrayList;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025
026import javax.ws.rs.GET;
027import javax.ws.rs.Path;
028import javax.ws.rs.PathParam;
029import javax.ws.rs.Produces;
030
031import org.nuxeo.apidoc.api.DocumentationItem;
032import org.nuxeo.apidoc.documentation.DocumentationService;
033import org.nuxeo.apidoc.search.ArtifactSearcher;
034import org.nuxeo.ecm.core.api.DocumentModel;
035import org.nuxeo.ecm.core.api.DocumentRef;
036import org.nuxeo.ecm.core.api.IdRef;
037import org.nuxeo.ecm.webengine.model.WebObject;
038import org.nuxeo.ecm.webengine.model.impl.DefaultObject;
039import org.nuxeo.runtime.api.Framework;
040
041@WebObject(type = "documentation")
042public class DocumentationWO extends DefaultObject {
043
044    @GET
045    @Produces("text/html")
046    public Object viewAll() {
047        DocumentationService ds = Framework.getLocalService(DocumentationService.class);
048        Map<String, List<DocumentationItem>> docs = ds.listDocumentationItems(getContext().getCoreSession(), null, null);
049        return getView("index").arg("distId", ctx.getProperty("distId")).arg("docsByCat", docs);
050    }
051
052    @GET
053    @Produces("text/html")
054    @Path("filter")
055    public Object filterAll() {
056        String fulltext = getContext().getForm().getFormProperty("fulltext");
057        DocumentationService ds = Framework.getLocalService(DocumentationService.class);
058
059        ArtifactSearcher searcher = Framework.getLocalService(ArtifactSearcher.class);
060        List<DocumentationItem> items = searcher.searchDocumentation(getContext().getCoreSession(),
061                (String) ctx.getProperty("distId"), fulltext, null);
062        Map<String, String> categories = ds.getCategories();
063
064        Map<String, List<DocumentationItem>> docs = new HashMap<String, List<DocumentationItem>>();
065
066        for (DocumentationItem item : items) {
067
068            String catKey = item.getType();
069            String catLabel = categories.get(catKey);
070            if (docs.containsKey(catLabel)) {
071                docs.get(catLabel).add(item);
072            } else {
073                List<DocumentationItem> itemList = new ArrayList<DocumentationItem>();
074                itemList.add(item);
075                docs.put(catLabel, itemList);
076            }
077        }
078        return getView("index").arg("distId", ctx.getProperty("distId")).arg("docsByCat", docs).arg("searchFilter",
079                fulltext);
080    }
081
082    @GET
083    @Produces("text/html")
084    @Path("view/{docUUID}")
085    public Object viewDoc(@PathParam("docUUID") String docUUID) {
086        DocumentRef docRef = new IdRef(docUUID);
087        DocumentModel docModel = getContext().getCoreSession().getDocument(docRef);
088        DocumentationItem doc = docModel.getAdapter(DocumentationItem.class);
089
090        return getView("viewSingleDoc").arg("distId", ctx.getProperty("distId")).arg("doc", doc);
091    }
092
093}