001/*
002 * (C) Copyright 2012 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 *     Arnaud Kervern
018 */
019package org.nuxeo.ecm.mobile.webengine.adapter;
020
021import java.util.Collections;
022import java.util.HashMap;
023import java.util.Map;
024
025import org.nuxeo.ecm.core.api.CoreSession;
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.core.api.DocumentModelComparator;
028import org.nuxeo.ecm.core.api.DocumentModelList;
029import org.nuxeo.ecm.core.api.Filter;
030import org.nuxeo.ecm.core.api.impl.CompoundFilter;
031import org.nuxeo.ecm.core.api.impl.FacetFilter;
032import org.nuxeo.ecm.core.api.impl.LifeCycleFilter;
033import org.nuxeo.ecm.mobile.webengine.document.MobileDocument;
034import org.nuxeo.ecm.webengine.WebException;
035import org.nuxeo.ecm.webengine.model.impl.DefaultAdapter;
036
037/**
038 * Default mobile adapter that exposes some usefull methods to other adapters.
039 *
040 * @author <a href="mailto:akervern@nuxeo.com">Arnaud Kervern</a>
041 */
042public abstract class DefaultMobileAdapter extends DefaultAdapter {
043    public static final Filter ONLY_VISIBLE_CHILDREN = new CompoundFilter(new FacetFilter(null,
044            Collections.singletonList("HiddenInNavigation")), new LifeCycleFilter(null,
045            Collections.singletonList("deleted")));
046
047    /**
048     * Get the current DocumentModel
049     *
050     * @return
051     */
052    protected DocumentModel getDocumentModel() {
053        MobileDocument doc = getMobileDocument();
054        if (doc == null) {
055            doc = getMobileDocument();
056        }
057        return doc.getDocument();
058    }
059
060    /**
061     * Get the current MobileDocument
062     *
063     * @return
064     */
065    protected MobileDocument getMobileDocument() {
066        Object targetObject = ctx.getTargetObject();
067        if (!(targetObject instanceof MobileDocument)) {
068            throw new WebException("Target Object must be MobileDocument");
069        }
070
071        MobileDocument mobileDoc = (MobileDocument) targetObject;
072        return mobileDoc;
073    }
074
075    public DocumentModelList getChildren() {
076        CoreSession session = ctx.getCoreSession();
077        Map<String, String> order = new HashMap<String, String>();
078        order.put("title", "asc");
079
080        DocumentModelComparator dmc = new DocumentModelComparator("dublincore", order);
081
082        return session.getChildren(getDocumentModel().getRef(), null, ONLY_VISIBLE_CHILDREN, dmc);
083    }
084}