001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *      http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.oozie.command.coord;
020
021import java.util.ArrayList;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025import org.apache.oozie.CoordinatorActionBean;
026import org.apache.oozie.ErrorCode;
027import org.apache.oozie.command.CommandException;
028import org.apache.oozie.command.PreconditionException;
029import org.apache.oozie.command.XCommand;
030import org.apache.oozie.coord.CoordUtils;
031import org.apache.oozie.coord.input.dependency.CoordInputDependency;
032import org.apache.oozie.dependency.ActionDependency;
033import org.apache.oozie.executor.jpa.CoordActionQueryExecutor;
034import org.apache.oozie.executor.jpa.CoordActionQueryExecutor.CoordActionQuery;
035import org.apache.oozie.executor.jpa.JPAExecutorException;
036import org.apache.oozie.util.Pair;
037
038public class CoordActionMissingDependenciesXCommand
039        extends XCommand<List<Pair<CoordinatorActionBean, Map<String, ActionDependency>>>> {
040
041    private String actions;
042    private String dates;
043    private String jobId;
044    private List<CoordinatorActionBean> coordActions = new ArrayList<CoordinatorActionBean>();
045
046    public CoordActionMissingDependenciesXCommand(String id, String actions, String dates) {
047        super("CoordActionMissingDependenciesXCommand", "CoordActionMissingDependenciesXCommand", 1);
048        this.jobId = id;
049        this.actions = actions;
050        this.dates = dates;
051
052        if (id.contains("@")) {
053            this.jobId = id.substring(0, id.indexOf("@"));
054            this.actions = id.substring(id.indexOf("@") + 1);
055        }
056    }
057
058    public CoordActionMissingDependenciesXCommand(String id) {
059        this(id, null, null);
060    }
061
062    @Override
063    protected boolean isLockRequired() {
064        return false;
065    }
066
067    @Override
068    public String getEntityKey() {
069        return null;
070    }
071
072    @Override
073    protected void eagerVerifyPrecondition() throws CommandException, PreconditionException {
074        if (actions == null && dates == null) {
075            throw new CommandException(ErrorCode.E1029, "Action(s) are missing.");
076        }
077    }
078
079    @Override
080    protected void loadState() throws CommandException {
081        String actionId = null;
082
083        try {
084            List<String> actionIds = CoordUtils.getActionListForScopeAndDate(jobId, actions, dates);
085            for (String id : actionIds) {
086                actionId = id;
087                coordActions.add(CoordActionQueryExecutor.getInstance()
088                        .get(CoordActionQuery.GET_COORD_ACTION_FOR_INPUTCHECK, actionId));
089            }
090        }
091        catch (JPAExecutorException e) {
092            if (e.getErrorCode().equals(ErrorCode.E0605)) {
093                throw new CommandException(ErrorCode.E0605, actionId);
094            }
095            else {
096                throw new CommandException(ErrorCode.E1029, e);
097            }
098        }
099
100    }
101
102    @Override
103    protected void verifyPrecondition() throws CommandException, PreconditionException {
104
105    }
106
107    @Override
108    protected List<Pair<CoordinatorActionBean, Map<String, ActionDependency>>> execute() throws CommandException {
109
110        List<Pair<CoordinatorActionBean, Map<String, ActionDependency>>> inputDependenciesListPair =
111                new ArrayList<Pair<CoordinatorActionBean, Map<String, ActionDependency>>>();
112        try {
113
114            for (CoordinatorActionBean coordAction : coordActions) {
115                CoordInputDependency coordPullInputDependency = coordAction.getPullInputDependencies();
116                CoordInputDependency coordPushInputDependency = coordAction.getPushInputDependencies();
117                Map<String, ActionDependency> dependencyMap = new HashMap<String, ActionDependency>();
118                dependencyMap.putAll(coordPullInputDependency.getMissingDependencies(coordAction));
119                dependencyMap.putAll(coordPushInputDependency.getMissingDependencies(coordAction));
120
121                inputDependenciesListPair.add(
122                        new Pair<CoordinatorActionBean, Map<String, ActionDependency>>(coordAction, dependencyMap));
123            }
124        }
125        catch (Exception e) {
126            throw new CommandException(ErrorCode.E1028, e.getMessage(), e);
127        }
128
129        return inputDependenciesListPair;
130    }
131
132}