001    /**
002     * Copyright (C) 2009 Progress Software, Inc.
003     * http://fusesource.com
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     *    http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.kuali.maven.plugins.graph;
018    
019    import java.io.File;
020    import java.util.ArrayList;
021    import java.util.List;
022    
023    import org.kuali.maven.plugins.graph.dot.GraphException;
024    import org.kuali.maven.plugins.graph.dot.edge.EdgeHandler;
025    import org.kuali.maven.plugins.graph.dot.edge.SmartEdgeHandler;
026    import org.kuali.maven.plugins.graph.pojo.Edge;
027    import org.kuali.maven.plugins.graph.pojo.GraphNode;
028    import org.kuali.maven.plugins.graph.pojo.MavenContext;
029    import org.kuali.maven.plugins.graph.pojo.State;
030    import org.kuali.maven.plugins.graph.tree.Node;
031    import org.kuali.maven.plugins.graph.tree.TreeHelper;
032    
033    /**
034     * @goal conflicts
035     * @requiresDependencyResolution compile|test|runtime
036     */
037    public class ConflictsMojo extends BaseMojo {
038    
039        /**
040         * The file the graph will be written to
041         *
042         * @parameter expression="${graph.file}" default-value="${project.build.directory}/graph/conflicts.png"
043         */
044        private File file;
045    
046        @Override
047        protected EdgeHandler getEdgeHandler() {
048            return new SmartEdgeHandler();
049        }
050    
051        @Override
052        public File getFile() {
053            return file;
054        }
055    
056        public void setFile(File file) {
057            this.file = file;
058        }
059    
060        @Override
061        public void execute() {
062            setIncludes(null);
063            setExcludes(null);
064            setHide(null);
065            setShow("::conflict");
066            super.execute();
067        }
068    
069        @Override
070        protected void postProcess(Node<MavenContext> node, List<GraphNode> nodes, List<Edge> edges) {
071            TreeHelper helper = new TreeHelper();
072            List<MavenContext> contexts = getContexts(node);
073            List<Edge> conflictEdges = getEdges(contexts, edges);
074            for (Edge conflictEdge : conflictEdges) {
075                conflictEdge.getChild().setHidden(false);
076            }
077            List<Node<MavenContext>> treeNodes = getNodes(node, conflictEdges);
078            for (Node<MavenContext> treeNode : treeNodes) {
079                helper.showPath(treeNode);
080            }
081        }
082    
083        protected List<Node<MavenContext>> getNodes(Node<MavenContext> node, List<Edge> edges) {
084            List<Node<MavenContext>> nodes = new ArrayList<Node<MavenContext>>();
085            for (Edge edge : edges) {
086                nodes.add(getNode(node, edge));
087            }
088            return nodes;
089        }
090    
091        protected Node<MavenContext> getNode(Node<MavenContext> node, Edge edge) {
092            List<Node<MavenContext>> nodes = node.getRoot().getBreadthFirstList();
093            for (Node<MavenContext> elementNode : nodes) {
094                GraphNode graphNode = elementNode.getObject().getGraphNode();
095                int id = graphNode.getId();
096                if (id == edge.getChild().getId()) {
097                    return elementNode;
098                }
099            }
100            throw new GraphException("Inconsistent tree state.  Unable to locate node " + edge.getChild().getId());
101        }
102    
103        protected List<Edge> getEdges(List<MavenContext> contexts, List<Edge> edges) {
104            List<Edge> newEdges = new ArrayList<Edge>();
105            for (MavenContext context : contexts) {
106                newEdges.add(getEdge(context, edges));
107            }
108            return newEdges;
109        }
110    
111        protected Edge getEdge(MavenContext context, List<Edge> edges) {
112            for (Edge edge : edges) {
113                GraphNode parent = edge.getParent();
114                int parentId = parent.getId();
115                if (parentId == context.getGraphNode().getId()) {
116                    return edge;
117                }
118            }
119            throw new GraphException("Inconsistent tree state.  Unable to locate an edge for "
120                    + context.getArtifactIdentifier());
121        }
122    
123        protected List<MavenContext> getContexts(Node<MavenContext> node) {
124            List<Node<MavenContext>> nodeList = node.getBreadthFirstList();
125            List<MavenContext> contexts = new ArrayList<MavenContext>();
126            for (Node<MavenContext> nodeElement : nodeList) {
127                MavenContext context = nodeElement.getObject();
128                State state = context.getState();
129                boolean hidden = context.getGraphNode().isHidden();
130                if (!hidden && state == State.CONFLICT) {
131                    contexts.add(context);
132                }
133            }
134            return contexts;
135        }
136    
137    }