001/*
002 * (C) Copyright 2017-2018 Nuxeo (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 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.admin;
020
021import static org.jboss.seam.ScopeType.CONVERSATION;
022
023import java.io.Serializable;
024import java.util.ArrayList;
025import java.util.Collection;
026import java.util.Collections;
027import java.util.HashMap;
028import java.util.List;
029import java.util.Map;
030
031import org.jboss.seam.annotations.Name;
032import org.jboss.seam.annotations.Scope;
033import org.nuxeo.runtime.api.Framework;
034import org.nuxeo.runtime.migration.MigrationDescriptor;
035import org.nuxeo.runtime.migration.MigrationDescriptor.MigrationStepDescriptor;
036import org.nuxeo.runtime.migration.MigrationService;
037import org.nuxeo.runtime.migration.MigrationService.MigrationStatus;
038import org.nuxeo.runtime.migration.MigrationServiceImpl;
039
040/**
041 * Seam bean that wraps the {@link MigrationService} service to provide a JSF admin UI.
042 */
043@Name("migrationAdmin")
044@Scope(CONVERSATION)
045public class MigrationAdminBean implements Serializable {
046
047    private static final long serialVersionUID = 1L;
048
049    public List<Map<String, Object>> getMigrationInfos() {
050        MigrationService migrationService = Framework.getService(MigrationService.class);
051        List<Map<String, Object>> migrationInfos = new ArrayList<>();
052
053        Collection<MigrationDescriptor> descriptors = ((MigrationServiceImpl) migrationService).getMigrationDescriptors();
054        descriptors.forEach(descr -> {
055            MigrationStatus status = migrationService.getStatus(descr.getId());
056            Map<String, Object> migrationInfo = new HashMap<>();
057            migrationInfo.put("id", descr.getId());
058            migrationInfo.put("descriptor", descr);
059            migrationInfo.put("status", status);
060            if (!status.isRunning()) {
061                // compute available steps
062                String state = status.getState();
063                List<MigrationStepDescriptor> steps = new ArrayList<>();
064                for (MigrationStepDescriptor step : descr.steps.values()) {
065                    if (step.fromState.equals(state)) {
066                        steps.add(step);
067                    }
068                }
069                // sort steps by id
070                Collections.sort(steps, (a, b) -> a.getId().compareTo(b.getId()));
071                migrationInfo.put("steps", steps);
072            }
073            migrationInfos.add(migrationInfo);
074        });
075        // sort migrationInfos by id
076        Collections.sort(migrationInfos, (a, b) -> ((String) a.get("id")).compareTo((String) b.get("id")));
077
078        return migrationInfos;
079    }
080
081    public void probeAndSetState(String id) {
082        MigrationService migrationService = Framework.getService(MigrationService.class);
083        migrationService.probeAndSetState(id);
084    }
085
086    public void runStep(String id, String step) {
087        MigrationService migrationService = Framework.getService(MigrationService.class);
088        migrationService.runStep(id, step);
089    }
090
091}