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