001    /*
002    // $Id: //open/mondrian/src/main/mondrian/rolap/Test.java#18 $
003    // This software is subject to the terms of the Common Public License
004    // Agreement, available at the following URL:
005    // http://www.opensource.org/licenses/cpl.html.
006    // Copyright (C) 2001-2002 Kana Software, Inc.
007    // Copyright (C) 2001-2008 Julian Hyde and others
008    // All Rights Reserved.
009    // You must accept the terms of that agreement to use this software.
010    //
011    // jhyde, 21 December, 2001
012    */
013    
014    package mondrian.rolap;
015    
016    import java.io.PrintWriter;
017    import java.util.ArrayList;
018    import java.util.List;
019    
020    import mondrian.olap.DriverManager;
021    import mondrian.olap.Id;
022    import mondrian.olap.Level;
023    
024    /**
025     * todo:
026     *
027     * @author jhyde
028     * @since 21 December, 2001
029     * @version $Id: //open/mondrian/src/main/mondrian/rolap/Test.java#18 $
030     */
031    public class Test {
032        PrintWriter pw;
033        RolapConnection connection;
034    
035        static public void main(String[] args) {
036            Test test = new Test(args);
037            if (true) {
038                test.run();
039            } else {
040                try {
041                    test.convertFoodMart();
042                } catch (java.sql.SQLException e) {
043                    System.out.println("Error: " + mondrian.olap.Util.getErrorMessage(e));
044                }
045            }
046        }
047    
048        Test(String[] args)
049        {
050            pw = new PrintWriter(System.out, true);
051            String connectString = "Data Source=LOCALHOST;Provider=msolap;Catalog=Foodmart";
052            connection = (RolapConnection) DriverManager.getConnection(connectString, null);
053        }
054    
055        void convertFoodMart() throws java.sql.SQLException
056        {
057            java.sql.Connection connection = null;
058            java.sql.Statement statement = null, statement2 = null;
059            try {
060                try {
061                    Class.forName("com.ms.jdbc.odbc.JdbcOdbcDriver");
062                } catch (ClassNotFoundException e) {
063                }
064                String connectString = "jdbc:odbc:DSN=FoodMart2";
065                connection = java.sql.DriverManager.getConnection(connectString);
066                statement = connection.createStatement();
067                statement2 = connection.createStatement();
068                String sql =
069                    "select * from (" +
070                    " select *, \"fname\" + ' ' + \"lname\" as \"name\" from \"customer\")" +
071                    "order by \"country\", \"state_province\", \"city\", \"name\"";
072    //          sql = "select * from \"customer\" " +
073    //              "where (\"country\", \"state_province\") = " +
074    //              " ('Canada', 'BC')";
075    //          sql = "select * from \"customer\" " +
076    //              "where \"country\" = 'Canada' " +
077    //              " and \"state_province\" = 'BC'";
078                java.sql.ResultSet resultSet = statement.executeQuery(sql);
079                if (true) {
080    //              return;
081                }
082                int i = 0;
083                while (resultSet.next()) {
084                    int customer_id = resultSet.getInt("customer_id");
085                    statement2.executeUpdate(
086                        "update \"customer\" set \"ordinal\" = " + (++i * 3) +
087                        " where \"customer_id\" = " + customer_id);
088                }
089                connection.commit();
090            } finally {
091                if (statement2 != null) {
092                    try {
093                        statement2.close();
094                    } catch (java.sql.SQLException e) {
095                    }
096                }
097                if (statement != null) {
098                    try {
099                        statement.close();
100                    } catch (java.sql.SQLException e) {
101                    }
102                }
103                if (connection != null) {
104                    try {
105                        connection.close();
106                    } catch (java.sql.SQLException e) {
107                    }
108                }
109            }
110        }
111    
112        void run()
113        {
114            RolapCube salesCube = (RolapCube) connection.getSchema().lookupCube("Sales", true);
115            RolapHierarchy measuresHierarchy =
116                    (RolapHierarchy) salesCube.getMeasuresHierarchy();
117            testMemberReader(measuresHierarchy.getMemberReader());
118    
119            RolapHierarchy genderHierarchy = (RolapHierarchy)
120                salesCube.lookupHierarchy(
121                        new Id.Segment("Gender", Id.Quoting.QUOTED), false);
122            testMemberReader(genderHierarchy.getMemberReader());
123    
124            RolapHierarchy customerHierarchy = (RolapHierarchy)
125                salesCube.lookupHierarchy(
126                        new Id.Segment("Customers", Id.Quoting.QUOTED), false);
127            testMemberReader(customerHierarchy.getMemberReader());
128        }
129        void testMemberReader(MemberReader reader)
130        {
131            pw.println();
132            pw.println("MemberReader class=" + reader.getClass());
133            pw.println("Count=" + reader.getMemberCount());
134    
135            pw.print("Root member(s)=");
136            List<RolapMember> rootMembers = reader.getRootMembers();
137            print(rootMembers);
138            pw.println();
139    
140            Level[] levels = rootMembers.get(0).getHierarchy().getLevels();
141            Level level = levels[levels.length > 1 ? 1 : 0];
142            pw.print("Members at level " + level.getUniqueName() + " are ");
143            List<RolapMember> members = reader.getMembersInLevel((RolapLevel)level, 0, Integer.MAX_VALUE);
144            print(members);
145            pw.println();
146    
147            pw.println("First children of first children: {");
148            List<RolapMember> firstChildren = new ArrayList<RolapMember>();
149            RolapMember member = rootMembers.get(0);
150            while (member != null) {
151                firstChildren.add(member);
152                pw.print("\t");
153                print(member);
154                List<RolapMember> children = new ArrayList<RolapMember>();
155                reader.getMemberChildren(member, children);
156                if (children.isEmpty()) {
157                    break;
158                }
159                pw.print(" (" + children.size() + " children)");
160                RolapMember leadMember = reader.getLeadMember(member, 5);
161                pw.print(", lead(5)=");
162                print(leadMember);
163                if (children.size() > 1) {
164                    member = children.get(1);
165                } else if (children.size() > 0) {
166                    member = children.get(0);
167                } else {
168                    member = null;
169                }
170                pw.println();
171            }
172            pw.println("}");
173        }
174        private void print(RolapMember member)
175        {
176            if (member == null) {
177                pw.print("Member(null)");
178                return;
179            }
180            pw.print("Member(" + member.getUniqueName() + ")");
181        }
182        private void print(List<RolapMember> members)
183        {
184            pw.print("{");
185            for (int i = 0; i < members.size(); i++) {
186                if (i > 0) {
187                    pw.print(", ");
188                }
189                print(members.get(i));
190            }
191            pw.print("}");
192        }
193    }
194    
195    
196    // End Test.java