001/*
002 * (C) Copyright 2010 Nuxeo SAS (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 *     Nuxeo - initial API and implementation
016 */
017
018package org.nuxeo.connect.client.jsf;
019
020import static org.jboss.seam.ScopeType.SESSION;
021
022import java.io.Serializable;
023
024import javax.faces.context.FacesContext;
025
026import org.jboss.seam.annotations.Begin;
027import org.jboss.seam.annotations.In;
028import org.jboss.seam.annotations.Name;
029import org.jboss.seam.annotations.Scope;
030import org.jboss.seam.annotations.web.RequestParameter;
031import org.jboss.seam.contexts.Context;
032import org.nuxeo.connect.data.DownloadablePackage;
033import org.nuxeo.connect.packages.PackageManager;
034import org.nuxeo.ecm.admin.AdminViewManager;
035import org.nuxeo.ecm.core.api.CoreSession;
036import org.nuxeo.ecm.core.api.DocumentModelList;
037import org.nuxeo.ecm.core.api.NuxeoPrincipal;
038import org.nuxeo.ecm.core.api.repository.RepositoryManager;
039import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
040import org.nuxeo.ecm.platform.ui.web.api.WebActions;
041import org.nuxeo.ecm.platform.util.RepositoryLocation;
042import org.nuxeo.runtime.api.Framework;
043
044/**
045 * Seam Bean used to build/restore a JSF/Seam context from a REST call. This bean is called when a user clicks on an
046 * installation link from MarketPlace.
047 *
048 * @author tiry
049 */
050@Name("externalLinkManager")
051@Scope(SESSION)
052public class ExternalLinkManager implements Serializable {
053
054    private static final long serialVersionUID = 1L;
055
056    @RequestParameter
057    protected String packageId;
058
059    protected DownloadablePackage pkg;
060
061    @In(create = true, required = false)
062    protected WebActions webActions;
063
064    @In(create = true, required = false)
065    protected AdminViewManager adminViews;
066
067    @In
068    protected transient Context sessionContext;
069
070    @In(create = true)
071    protected transient NavigationContext navigationContext;
072
073    @In(create = true)
074    protected transient RepositoryManager repositoryManager;
075
076    protected static NuxeoPrincipal getUser() {
077        return (NuxeoPrincipal) FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
078    }
079
080    protected void setupCurrentUser() {
081        sessionContext.set("currentUser", getUser());
082    }
083
084    @Begin(id = "#{conversationIdGenerator.nextMainConversationId}", join = true)
085    public String startDownload() {
086        if (packageId == null) {
087            return null;
088        }
089
090        PackageManager pm = Framework.getLocalService(PackageManager.class);
091        pkg = pm.getPackage(packageId);
092
093        if (getUser().isAdministrator() && pkg != null) {
094            return "confirm_download";
095        } else {
096            return "can_not_download";
097        }
098    }
099
100    public DownloadablePackage getPkg() {
101        return pkg;
102    }
103
104    protected void initMinimalContext() {
105        setupCurrentUser();
106
107        // we try to select the server to go to the next screen
108        if (navigationContext.getCurrentServerLocation() == null) {
109            // update location
110            RepositoryLocation repLoc = new RepositoryLocation(repositoryManager.getRepositoryNames().get(0));
111            navigationContext.setCurrentServerLocation(repLoc);
112        }
113        CoreSession documentManager = navigationContext.getOrCreateDocumentManager();
114        DocumentModelList domains = documentManager.query("select * from Domain");
115        navigationContext.setCurrentDocument(domains.get(0));
116    }
117
118    public String confirm() {
119        initMinimalContext();
120        webActions.setCurrentTabId(AdminViewManager.ADMIN_ACTION_CATEGORY, "ConnectApps", "ConnectAppsRemote");
121        adminViews.addExternalPackageDownloadRequest(pkg.getId());
122        return AdminViewManager.VIEW_ADMIN;
123    }
124
125}