001/*
002 * (C) Copyright 2014 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-2.1.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 *     rcattiau@gmail.com
016 */
017package org.nuxeo.ecm.agenda.seam;
018
019import static org.jboss.seam.ScopeType.CONVERSATION;
020import static org.jboss.seam.annotations.Install.FRAMEWORK;
021
022import java.io.Serializable;
023import java.security.Principal;
024import java.util.Map;
025
026import org.jboss.seam.annotations.In;
027import org.jboss.seam.annotations.Install;
028import org.jboss.seam.annotations.Name;
029import org.jboss.seam.annotations.Scope;
030import org.jboss.seam.faces.FacesMessages;
031import org.jboss.seam.international.StatusMessage;
032import org.nuxeo.ecm.automation.InvalidChainException;
033import org.nuxeo.ecm.automation.OperationException;
034import org.nuxeo.ecm.core.api.CoreSession;
035import org.nuxeo.ecm.core.api.DocumentModel;
036import org.nuxeo.ecm.core.api.IdRef;
037import org.nuxeo.ecm.platform.relations.api.DocumentRelationManager;
038import org.nuxeo.ecm.platform.relations.api.exceptions.RelationAlreadyExistsException;
039import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
040
041/**
042 * @author loopingz
043 * @since 5.9.3
044 */
045@Name("agendaAddToEventAction")
046@Scope(CONVERSATION)
047@Install(precedence = FRAMEWORK)
048public class AddToEventAction implements Serializable {
049    /**
050     * 
051     */
052    private static final long serialVersionUID = 3930167063686290160L;
053
054    private static final String PREDICATE_TYPE = "http://purl.org/dc/terms/References";
055
056    /**
057     * Get the agenda selected
058     */
059    private String selectedAgendaId = null;
060
061    @In(create = true)
062    protected Map<String, String> messages;
063
064    @In(create = true, required = false)
065    protected FacesMessages facesMessages;
066
067    @In(create = true, required = false)
068    protected transient CoreSession documentManager;
069
070    @In(create = true)
071    protected DocumentRelationManager documentRelationManager;
072
073    @In(create = true)
074    protected NavigationContext navigationContext;
075
076    @In(required = false)
077    protected transient Principal currentUser;
078
079    /**
080     * Get the event selected
081     */
082    private String selectedEventId = null;
083
084    public String getSelectedAgendaId() {
085        return selectedAgendaId;
086    }
087
088    public void setSelectedAgendaId(String selectedAgendaId) {
089        this.selectedAgendaId = selectedAgendaId;
090        this.selectedEventId = null;
091    }
092
093    public String getSelectedEventId() {
094        return selectedEventId;
095    }
096
097    public void setSelectedEventId(String selectedEventId) {
098        this.selectedEventId = selectedEventId;
099    }
100
101    public void addToEvent() throws InvalidChainException, OperationException, Exception {
102        // Do the job
103        DocumentModel doc = documentManager.getDocument(new IdRef(selectedEventId));
104        // Create link
105        DocumentModel currentDoc = navigationContext.getCurrentDocument();
106        try {
107            documentRelationManager.addRelation(documentManager, currentDoc, doc, PREDICATE_TYPE, true);
108
109            facesMessages.add(StatusMessage.Severity.INFO, messages.get("label.relation.created"));
110        } catch (RelationAlreadyExistsException e) {
111            facesMessages.add(StatusMessage.Severity.WARN, messages.get("label.relation.already.exists"));
112        }
113    }
114
115    public void cancelAddToEvent() {
116        selectedAgendaId = selectedEventId = null;
117    }
118
119}