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.coord.input.dependency;
020
021import java.io.DataInput;
022import java.io.DataOutput;
023import java.io.IOException;
024import java.io.ObjectInputStream;
025import java.io.ObjectOutputStream;
026import java.util.ArrayList;
027import java.util.Arrays;
028import java.util.HashMap;
029import java.util.List;
030import java.util.Map;
031
032import org.apache.commons.lang.StringUtils;
033import org.apache.oozie.CoordinatorActionBean;
034import org.apache.oozie.command.CommandException;
035import org.apache.oozie.command.coord.CoordCommandUtils;
036import org.apache.oozie.coord.CoordELFunctions;
037import org.apache.oozie.dependency.ActionDependency;
038import org.apache.oozie.util.WritableUtils;
039import org.jdom.JDOMException;
040
041public class CoordPullInputDependency extends AbstractCoordInputDependency {
042    private Map<String, CoordUnResolvedInputDependency> unResolvedList = new HashMap<String, CoordUnResolvedInputDependency>();
043
044    public CoordPullInputDependency() {
045        super();
046
047    }
048
049    public void addResolvedList(String dataSet, String list) {
050        unResolvedList.get(dataSet).addResolvedList(Arrays.asList(list.split(",")));
051    }
052
053    public CoordUnResolvedInputDependency getUnResolvedDependency(String dataSet) {
054        return unResolvedList.get(dataSet);
055    }
056
057    public boolean isUnResolvedDependencyMet() {
058        for (CoordUnResolvedInputDependency coordUnResolvedDependency : unResolvedList.values()) {
059            if (!coordUnResolvedDependency.isResolved()) {
060                return false;
061            }
062        }
063        return true;
064    }
065
066    public void addUnResolvedList(String dataSet, String dependency) {
067        unResolvedList.put(dataSet, new CoordUnResolvedInputDependency(Arrays.asList(dependency.split("#"))));
068    }
069
070    public String getMissingDependencies() {
071        StringBuffer bf = new StringBuffer(super.getMissingDependencies());
072        String unresolvedMissingDependencies = getUnresolvedMissingDependencies();
073        if (!StringUtils.isEmpty(unresolvedMissingDependencies)) {
074            bf.append(CoordCommandUtils.RESOLVED_UNRESOLVED_SEPARATOR);
075            bf.append(unresolvedMissingDependencies);
076        }
077        return bf.toString();
078    }
079
080    public String getUnresolvedMissingDependencies() {
081        StringBuffer bf = new StringBuffer();
082        if (unResolvedList != null) {
083            for (CoordUnResolvedInputDependency coordUnResolvedDependency : unResolvedList.values()) {
084                if (!coordUnResolvedDependency.isResolved()) {
085                    String unresolvedList = coordUnResolvedDependency.getUnResolvedList();
086                    if (bf.length() > 0 && !unresolvedList.isEmpty()) {
087                        bf.append(CoordELFunctions.INSTANCE_SEPARATOR);
088                    }
089                    bf.append(unresolvedList);
090                }
091            }
092        }
093        return bf.toString();
094    }
095
096    protected void generateDependencies() {
097        super.generateDependencies();
098    }
099
100    private void writeObject(ObjectOutputStream os) throws IOException, ClassNotFoundException {
101        os.writeObject(unResolvedList);
102    }
103
104    @SuppressWarnings("unchecked")
105    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
106        unResolvedList = (Map<String, CoordUnResolvedInputDependency>) in.readObject();
107        generateDependencies();
108    }
109
110    public boolean isDependencyMet() {
111        return isResolvedDependencyMeet() && isUnResolvedDependencyMet();
112
113    }
114
115    public boolean isResolvedDependencyMeet() {
116        return super.isDependencyMet();
117
118    }
119
120    @Override
121    public void write(DataOutput out) throws IOException {
122        super.write(out);
123        WritableUtils.writeMap(out, unResolvedList);
124    }
125
126    @Override
127    public void readFields(DataInput in) throws IOException {
128        super.readFields(in);
129        unResolvedList = WritableUtils.readMap(in, CoordUnResolvedInputDependency.class);
130    }
131
132    @Override
133    public void setMissingDependencies(String join) {
134        // We don't have to set this for input logic. Dependency map will have computed missing dependencies
135    }
136
137    @Override
138    public List<String> getAvailableDependencies(String dataSet) {
139        List<String> availableList = new ArrayList<String>();
140        availableList.addAll(super.getAvailableDependencies(dataSet));
141        if (getUnResolvedDependency(dataSet) != null) {
142            availableList.addAll(getUnResolvedDependency(dataSet).getResolvedList());
143        }
144        return availableList;
145    }
146
147    public boolean isDataSetResolved(String dataSet) {
148        if(unResolvedList.containsKey(dataSet)){
149            return unResolvedList.get(dataSet).isResolved();
150        }
151        else{
152            return super.isDataSetResolved(dataSet);
153        }
154    }
155
156    @Override
157    public Map<String, ActionDependency> getMissingDependencies(CoordinatorActionBean coordAction)
158            throws CommandException, IOException, JDOMException {
159        Map<String, ActionDependency> missingDependenciesMap = new HashMap<String, ActionDependency>();
160        missingDependenciesMap.putAll(super.getMissingDependencies(coordAction));
161
162        for (String key : unResolvedList.keySet()) {
163            if (!unResolvedList.get(key).isResolved()) {
164                missingDependenciesMap.put(key,
165                        new ActionDependency(unResolvedList.get(key).getDependencies(), new ArrayList<String>()));
166            }
167        }
168        return missingDependenciesMap;
169    }
170}