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