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