001/*
002 * (C) Copyright 2010-2013 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 *     Olivier Grisel
016 */
017package org.nuxeo.ecm.platform.suggestbox.service;
018
019import java.security.Principal;
020import java.util.HashMap;
021import java.util.Locale;
022import java.util.Map;
023
024import org.nuxeo.ecm.core.api.CoreSession;
025import org.nuxeo.ecm.core.api.DocumentModel;
026
027/**
028 * Base class and default implementation for passing contextual information to the suggestion service. This is primarily
029 * a hash map to store arbitrary context element that might be useful for suggester along with a few mandatory and
030 * common optional attributes for direct access.
031 *
032 * @author ogrisel
033 */
034public class SuggestionContext extends HashMap<String, Object> {
035
036    private static final long serialVersionUID = 1L;
037
038    public final String suggesterGroup;
039
040    public final Principal principal;
041
042    public final Map<String, String> messages = new HashMap<String, String>();
043
044    public transient CoreSession session;
045
046    public DocumentModel currentDocument;
047
048    public Locale locale = Locale.ENGLISH;
049
050    public SuggestionContext(String suggesterGroup, Principal principal) throws IllegalArgumentException {
051        if (suggesterGroup == null) {
052            throw new IllegalArgumentException("suggesterGroup is a mandatory field of the SuggestionContext");
053        }
054        if (principal == null) {
055            throw new IllegalArgumentException("principal is a mandatory field of the SuggestionContext");
056        }
057        this.suggesterGroup = suggesterGroup;
058        this.principal = principal;
059    }
060
061    public SuggestionContext withSession(CoreSession session) {
062        this.session = session;
063        return this;
064    }
065
066    public SuggestionContext withCurrentDocument(DocumentModel currentDocument) {
067        this.currentDocument = currentDocument;
068        return this;
069    }
070
071    public SuggestionContext withLocale(Locale locale) {
072        this.locale = locale;
073        return this;
074    }
075
076    public SuggestionContext withMessages(Map<String, String> messages) {
077        this.messages.putAll(messages);
078        return this;
079    }
080
081}