001    /******************************************************************************
002     * Copyright (C) MActor Developers. All rights reserved.                        *
003     * ---------------------------------------------------------------------------*
004     * This file is part of MActor.                                               *
005     *                                                                            *
006     * MActor is free software; you can redistribute it and/or modify             *
007     * it under the terms of the GNU General Public License as published by       *
008     * the Free Software Foundation; either version 2 of the License, or          *
009     * (at your option) any later version.                                        *
010     *                                                                            *
011     * MActor is distributed in the hope that it will be useful,                  *
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
014     * GNU General Public License for more details.                               *
015     *                                                                            *
016     * You should have received a copy of the GNU General Public License          *
017     * along with MActor; if not, write to the Free Software                      *
018     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA *
019     ******************************************************************************/
020    package org.mactor.ui.gui.project;
021    
022    import java.io.File;
023    import java.io.StringReader;
024    import java.util.HashMap;
025    import java.util.Map;
026    import java.util.Map.Entry;
027    
028    import org.dom4j.Document;
029    import org.dom4j.Element;
030    import org.dom4j.io.SAXReader;
031    import org.mactor.framework.MactorException;
032    import org.mactor.framework.spec.ProjectContext;
033    
034    public class ProjectTreeNodeBuilder {
035            public static ProjectTreeNode buildProjectTree() throws MactorException {
036                    File projectDir = ProjectContext.getGlobalInstance().getProjectDir();
037                    OrganizationProjectTreeNode root = new OrganizationProjectTreeNode(ProjectNodeType.PROJECT_ROOT, projectDir.getName());
038                    OrganizationProjectTreeNode gcOrg = new OrganizationProjectTreeNode(ProjectNodeType.PROJECT_GLOBAL_CONFIG, "Global Config");
039                    OrganizationProjectTreeNode mbOrg = new OrganizationProjectTreeNode(ProjectNodeType.PROJECT_MESSAGE_BROKER_CONFIG, "Message Broker Config");
040                    OrganizationProjectTreeNode testOrg = new OrganizationProjectTreeNode(ProjectNodeType.PROJECT_TEST, "Tests");
041                    OrganizationProjectTreeNode mockBatteryOrg = new OrganizationProjectTreeNode(ProjectNodeType.PROJECT_MOCK_BATTERY, "Mock Batteries");
042                    OrganizationProjectTreeNode testRunOrg = new OrganizationProjectTreeNode(ProjectNodeType.PROJECT_TEST_RUN, "Test Runs");
043                    root.addChild(mockBatteryOrg);
044                    root.addChild(testRunOrg);
045                    root.addChild(testOrg);
046                    root.addChild(mbOrg);
047                    root.addChild(gcOrg);
048                    File[] files = projectDir.listFiles();
049                    boolean seperateConfigDir = !projectDir.equals(ProjectContext.getGlobalInstance().getProjectConfigDir());
050                    if (files != null) {
051                            for (int i = 0; i < files.length; i++) {
052                                    if (files[i].isDirectory())
053                                            continue;
054                                    try {
055                                            if (files[i].getName().toLowerCase().endsWith(".xml")) {
056                                                    Document doc = ProjectContext.getGlobalInstance().readFromFile(files[i]);
057                                                    String rootEl = doc.getRootElement().getName();
058                                                    if ("test".equalsIgnoreCase(rootEl))
059                                                            testOrg.addChild(XmlFileProjectTreeNode.buildFromDocument(files[i], doc, testNodeDictionary, false));
060                                                    else if ("mock_battery".equalsIgnoreCase(rootEl))
061                                                            mockBatteryOrg.addChild(XmlFileProjectTreeNode.buildFromDocument(files[i], doc, mockbatteryNodeDictionary, false));
062                                                    else if (!seperateConfigDir && "message_broker_config".equalsIgnoreCase(rootEl))
063                                                            mbOrg.addChild(XmlFileProjectTreeNode.buildFromDocument(files[i], doc, messageBrokerNodeDictionary, true));
064                                                    else if (!seperateConfigDir && "global_config".equalsIgnoreCase(rootEl))
065                                                            gcOrg.addChild(XmlFileProjectTreeNode.buildFromDocument(files[i], doc, globalConfigNodeDictionary, true));
066                                                    else if ("test_run".equalsIgnoreCase(rootEl))
067                                                            testRunOrg.addChild(XmlFileProjectTreeNode.buildFromDocument(files[i], doc, testRunNodeDictionary, true));
068                                            }
069                                    } catch (MactorException me) {
070                                            me.printStackTrace();
071                                    }
072                            }
073                    }
074                    if (seperateConfigDir) {
075                            files = ProjectContext.getGlobalInstance().getProjectConfigDir().listFiles();
076                            if (files != null) {
077                                    for (int i = 0; i < files.length; i++) {
078                                            if (files[i].isDirectory())
079                                                    continue;
080                                            try {
081                                                    if (files[i].getName().toLowerCase().endsWith(".xml")) {
082                                                            Document doc = ProjectContext.getGlobalInstance().readFromFile(files[i]);
083                                                            String rootEl = doc.getRootElement().getName();
084                                                            if ("message_broker_config".equalsIgnoreCase(rootEl))
085                                                                    mbOrg.addChild(XmlFileProjectTreeNode.buildFromDocument(files[i], doc, messageBrokerNodeDictionary, true));
086                                                            else if ("global_config".equalsIgnoreCase(rootEl))
087                                                                    gcOrg.addChild(XmlFileProjectTreeNode.buildFromDocument(files[i], doc, globalConfigNodeDictionary, true));
088                                                    }
089                                            } catch (MactorException me) {
090                                                    me.printStackTrace();
091                                            }
092                                    }
093                            }
094                    }
095                    return root;
096            }
097            public static ProjectTreeNode createNewNode(ProjectNodeType nodeType, String templateContent) throws MactorException {
098                    if (nodeType.equals(ProjectNodeType.G_GLOBAL_CONFIG)) {
099                            File f = createFile("new-global_config.xml", templateContent, true);
100                            return XmlFileProjectTreeNode.buildFromFile(f, globalConfigNodeDictionary, true);
101                    } else if (nodeType.equals(ProjectNodeType.MBC_MESSAGE_BROKERS)) {
102                            File f = createFile("new-message_broker_config.xml", templateContent, true);
103                            return XmlFileProjectTreeNode.buildFromFile(f, messageBrokerNodeDictionary, true);
104                    } else if (nodeType.equals(ProjectNodeType.T_TEST)) {
105                            File f = createFile("new-test.xml", templateContent, false);
106                            return XmlFileProjectTreeNode.buildFromFile(f, testNodeDictionary, false);
107                    } else if (nodeType.equals(ProjectNodeType.TM_MOCK_BATTERY)) {
108                            File f = createFile("new-mock_battery.xml", templateContent, false);
109                            return XmlFileProjectTreeNode.buildFromFile(f, mockbatteryNodeDictionary, false);
110                    } else if (nodeType.equals(ProjectNodeType.TR_TEST_RUN)) {
111                            File f = createFile("new-test_run.xml", templateContent, false);
112                            return XmlFileProjectTreeNode.buildFromFile(f, testRunNodeDictionary, false);
113                    } else {
114                            Element data = createElementFromString(templateContent);
115                            if (reversedMessageBrokerNodeDictionary.containsKey(nodeType))
116                                    return new XmlProjectTreeNode(nodeType, data, messageBrokerNodeDictionary);
117                            else if (reversedGlobalConfigNodeDictionary.containsKey(nodeType))
118                                    return new XmlProjectTreeNode(nodeType, data, globalConfigNodeDictionary);
119                            else if (reversedMockBatteryNodeDictionary.containsKey(nodeType))
120                                    return new XmlProjectTreeNode(nodeType, data, mockbatteryNodeDictionary);
121                            else if (reversedTestNodeDictionary.containsKey(nodeType))
122                                    return new XmlProjectTreeNode(nodeType, data, testNodeDictionary);
123                            else if (reversedTestRunNodeDictionary.containsKey(nodeType))
124                                    return new XmlProjectTreeNode(nodeType, data, testRunNodeDictionary);
125                    }
126                    throw new MactorException("Unsupported node type '" + nodeType.name() + "'");
127            }
128            private static File createFile(String nameSeed, String content, boolean isConfigFile) throws MactorException {
129                    String name = ProjectContext.getGlobalInstance().getNextFilename(nameSeed, isConfigFile);
130                    return ProjectContext.getGlobalInstance().writeStringToFile(name, content, isConfigFile);
131            }
132            private static Element createElementFromString(String elementStr) {
133                    if (elementStr == null)
134                            return null;
135                    try {
136                            Element e = (Element) new SAXReader().read(new StringReader(elementStr)).getRootElement().detach();
137                            return e;
138                    } catch (Exception e) {
139                            e.printStackTrace();
140                    }
141                    return null;
142            }
143            private static Map<String, ProjectNodeType> messageBrokerNodeDictionary = buildMessageBrokerNodeDictionary();
144            private static Map<String, ProjectNodeType> globalConfigNodeDictionary = buildGlobalConfigNodeDictionary();
145            private static Map<String, ProjectNodeType> mockbatteryNodeDictionary = buildMockBatteryNodeDictionary();
146            private static Map<String, ProjectNodeType> testRunNodeDictionary = buildTestRunNodeDictionary();
147            private static Map<String, ProjectNodeType> testNodeDictionary = buildTestNodeDictionary();
148            private static Map<ProjectNodeType, String> reversedMessageBrokerNodeDictionary = reverse(messageBrokerNodeDictionary);
149            private static Map<ProjectNodeType, String> reversedGlobalConfigNodeDictionary = reverse(globalConfigNodeDictionary);
150            private static Map<ProjectNodeType, String> reversedMockBatteryNodeDictionary = reverse(mockbatteryNodeDictionary);
151            private static Map<ProjectNodeType, String> reversedTestRunNodeDictionary = reverse(testRunNodeDictionary);
152            private static Map<ProjectNodeType, String> reversedTestNodeDictionary = reverse(testNodeDictionary);
153            private static Map<ProjectNodeType, String> reverse(Map<String, ProjectNodeType> map) {
154                    Map<ProjectNodeType, String> rm = new HashMap<ProjectNodeType, String>();
155                    for (Entry<String, ProjectNodeType> e : map.entrySet())
156                            rm.put(e.getValue(), e.getKey());
157                    return rm;
158            }
159            private static Map<String, ProjectNodeType> buildMessageBrokerNodeDictionary() {
160                    Map<String, ProjectNodeType> dic = new HashMap<String, ProjectNodeType>();
161                    dic.put("message_broker_config", ProjectNodeType.MBC_MESSAGE_BROKERS);
162                    dic.put("message_broker", ProjectNodeType.MBC_MESSAGE_BROKER);
163                    dic.put("channel", ProjectNodeType.MBC_CHANNEL);
164                    dic.put("value", ProjectNodeType.VALUE);
165                    return dic;
166            }
167            private static Map<String, ProjectNodeType> buildGlobalConfigNodeDictionary() {
168                    Map<String, ProjectNodeType> dic = new HashMap<String, ProjectNodeType>();
169                    dic.put("global_config", ProjectNodeType.G_GLOBAL_CONFIG);
170                    dic.put("group", ProjectNodeType.G_GROUP);
171                    dic.put("value", ProjectNodeType.VALUE);
172                    return dic;
173            }
174            private static Map<String, ProjectNodeType> buildMockBatteryNodeDictionary() {
175                    Map<String, ProjectNodeType> dic = new HashMap<String, ProjectNodeType>();
176                    dic.put("mock_battery", ProjectNodeType.TM_MOCK_BATTERY);
177                    dic.put("test", ProjectNodeType.TM_TEST);
178                    return dic;
179            }
180            private static Map<String, ProjectNodeType> buildTestRunNodeDictionary() {
181                    Map<String, ProjectNodeType> dic = new HashMap<String, ProjectNodeType>();
182                    dic.put("test_run", ProjectNodeType.TR_TEST_RUN);
183                    return dic;
184            }
185            private static Map<String, ProjectNodeType> buildTestNodeDictionary() {
186                    Map<String, ProjectNodeType> dic = new HashMap<String, ProjectNodeType>();
187                    dic.put("test", ProjectNodeType.T_TEST);
188                    dic.put("action", ProjectNodeType.T_ACTION);
189                    dic.put("condition", ProjectNodeType.T_CONDITION);
190                    dic.put("event_choice", ProjectNodeType.T_EVENT_CHOICE);
191                    dic.put("event_choice_branch", ProjectNodeType.T_EVENT_CHOICE_BRANCH);
192                    dic.put("loop", ProjectNodeType.T_LOOP);
193                    dic.put("message_publish", ProjectNodeType.T_MESSAGE_PUBLISH);
194                    dic.put("message_receive", ProjectNodeType.T_MESSAGE_RECEIVE);
195                    dic.put("message_respond", ProjectNodeType.T_MESSAGE_RESPOND);
196                    dic.put("message_subscribe", ProjectNodeType.T_MESSAGE_SUBSCRIBE);
197                    dic.put("value", ProjectNodeType.T_VALUE);
198                    return dic;
199            }
200    }