xref: /trunk/main/odk/examples/DevelopersGuide/UCB/ChildrenRetriever.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  *  The Contents of this file are made available subject to the terms of
4*cdf0e10cSrcweir  *  the BSD license.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7*cdf0e10cSrcweir  *  All rights reserved.
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  *  Redistribution and use in source and binary forms, with or without
10*cdf0e10cSrcweir  *  modification, are permitted provided that the following conditions
11*cdf0e10cSrcweir  *  are met:
12*cdf0e10cSrcweir  *  1. Redistributions of source code must retain the above copyright
13*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer.
14*cdf0e10cSrcweir  *  2. Redistributions in binary form must reproduce the above copyright
15*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer in the
16*cdf0e10cSrcweir  *     documentation and/or other materials provided with the distribution.
17*cdf0e10cSrcweir  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18*cdf0e10cSrcweir  *     contributors may be used to endorse or promote products derived
19*cdf0e10cSrcweir  *     from this software without specific prior written permission.
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*cdf0e10cSrcweir  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*cdf0e10cSrcweir  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*cdf0e10cSrcweir  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25*cdf0e10cSrcweir  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*cdf0e10cSrcweir  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*cdf0e10cSrcweir  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28*cdf0e10cSrcweir  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29*cdf0e10cSrcweir  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30*cdf0e10cSrcweir  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31*cdf0e10cSrcweir  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*cdf0e10cSrcweir  *
33*cdf0e10cSrcweir  *************************************************************************/
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir import com.sun.star.ucb.OpenCommandArgument2;
36*cdf0e10cSrcweir import com.sun.star.ucb.OpenMode;
37*cdf0e10cSrcweir import com.sun.star.ucb.XContent;
38*cdf0e10cSrcweir import com.sun.star.ucb.XContentAccess;
39*cdf0e10cSrcweir import com.sun.star.ucb.XDynamicResultSet;
40*cdf0e10cSrcweir import com.sun.star.beans.Property;
41*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
42*cdf0e10cSrcweir import com.sun.star.sdbc.XRow;
43*cdf0e10cSrcweir import com.sun.star.sdbc.XResultSet;
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir import java.util.Vector;
46*cdf0e10cSrcweir import java.util.Enumeration;
47*cdf0e10cSrcweir import java.util.StringTokenizer;
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir /**
50*cdf0e10cSrcweir  * Retrieve the Children of a UCB Folder Content
51*cdf0e10cSrcweir  */
52*cdf0e10cSrcweir public class ChildrenRetriever {
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir     /**
55*cdf0e10cSrcweir      * Member properties
56*cdf0e10cSrcweir      */
57*cdf0e10cSrcweir     private  Helper   m_helper;
58*cdf0e10cSrcweir     private  XContent m_content;
59*cdf0e10cSrcweir     private  String   m_contenturl    = "";
60*cdf0e10cSrcweir     private  Vector   m_propnames      = new Vector();
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir     /**
63*cdf0e10cSrcweir      * Constructor. Create a new connection with the specific args to a running office
64*cdf0e10cSrcweir      *
65*cdf0e10cSrcweir      *@param      String[]   This construtor requires the arguments:
66*cdf0e10cSrcweir      *                          -url=...       (optional)
67*cdf0e10cSrcweir      *                          -propNames=... (optional)
68*cdf0e10cSrcweir      *                       See Help (method printCmdLineUsage()).
69*cdf0e10cSrcweir      *                       Without the arguments a new connection to a
70*cdf0e10cSrcweir      *                       running office cannot created.
71*cdf0e10cSrcweir      *@exception  java.lang.Exception
72*cdf0e10cSrcweir      */
73*cdf0e10cSrcweir     public ChildrenRetriever( String args[] ) throws java.lang.Exception {
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir         // Parse arguments
76*cdf0e10cSrcweir         parseArguments( args );
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir         // Init
79*cdf0e10cSrcweir         m_helper       = new Helper( getContentURL() );
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir         // Create UCB content
82*cdf0e10cSrcweir         m_content      = m_helper.createUCBContent();
83*cdf0e10cSrcweir     }
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir     /**
86*cdf0e10cSrcweir      * Open a folder content, get properties values.
87*cdf0e10cSrcweir      * This method requires the main and the optional arguments to be set in order to work.
88*cdf0e10cSrcweir      * See Constructor.
89*cdf0e10cSrcweir      *
90*cdf0e10cSrcweir      *@return     Vector   Returns children properties values if values successfully retrieved,
91*cdf0e10cSrcweir      *                     null otherwise
92*cdf0e10cSrcweir      *@exception  com.sun.star.ucb.CommandAbortedException
93*cdf0e10cSrcweir      *@exception  com.sun.star.uno.Exception
94*cdf0e10cSrcweir      */
95*cdf0e10cSrcweir     public Vector getChildren()
96*cdf0e10cSrcweir         throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception {
97*cdf0e10cSrcweir         Vector properties = getProperties();
98*cdf0e10cSrcweir         return getChildren ( properties );
99*cdf0e10cSrcweir     }
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir     /**
102*cdf0e10cSrcweir      * Open a folder content, get properties values for the properties.
103*cdf0e10cSrcweir      *
104*cdf0e10cSrcweir      *@param  Vector   Properties
105*cdf0e10cSrcweir      *@return Vector   Returns children properties values if values successfully retrieved,
106*cdf0e10cSrcweir      *                 null otherwise
107*cdf0e10cSrcweir      *@exception  com.sun.star.ucb.CommandAbortedException
108*cdf0e10cSrcweir      *@exception  com.sun.star.uno.Exception
109*cdf0e10cSrcweir      */
110*cdf0e10cSrcweir     public Vector getChildren( Vector properties )
111*cdf0e10cSrcweir         throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception {
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir         Vector result = null;
114*cdf0e10cSrcweir         if ( m_content != null ) {
115*cdf0e10cSrcweir             int size = 0;
116*cdf0e10cSrcweir             if ( properties != null && !properties.isEmpty()) {
117*cdf0e10cSrcweir                 size = properties.size();
118*cdf0e10cSrcweir             }
119*cdf0e10cSrcweir             // Fill info for the properties wanted.
120*cdf0e10cSrcweir             Property[] props = new Property[ size ];
121*cdf0e10cSrcweir             for ( int index = 0 ; index < size; index++ ) {
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir                 // Define property sequence.
124*cdf0e10cSrcweir                 Property prop = new Property();
125*cdf0e10cSrcweir                 prop.Name = ( String )properties.get( index );
126*cdf0e10cSrcweir                 prop.Handle = -1; // n/a
127*cdf0e10cSrcweir                 props[ index ] = prop;
128*cdf0e10cSrcweir             }
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir             // Fill argument structure...
131*cdf0e10cSrcweir             OpenCommandArgument2 arg = new OpenCommandArgument2();
132*cdf0e10cSrcweir             arg.Mode = OpenMode.ALL; // FOLDER, DOCUMENTS -> simple filter
133*cdf0e10cSrcweir             arg.Priority = 32768;    // Final static for 32768
134*cdf0e10cSrcweir             arg.Properties = props;
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir             XDynamicResultSet set;
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir             // Execute command "open".
139*cdf0e10cSrcweir             set = ( XDynamicResultSet )UnoRuntime.queryInterface(
140*cdf0e10cSrcweir                 XDynamicResultSet.class, m_helper.executeCommand( m_content, "open", arg ));
141*cdf0e10cSrcweir             XResultSet resultSet = ( XResultSet )set.getStaticResultSet();
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir             result = new Vector();
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir             /////////////////////////////////////////////////////////////////////
146*cdf0e10cSrcweir             // Iterate over children, access children and property values...
147*cdf0e10cSrcweir             /////////////////////////////////////////////////////////////////////
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir                 // Move to begin.
150*cdf0e10cSrcweir             if ( resultSet.first() ) {
151*cdf0e10cSrcweir                 XContentAccess contentAccess = ( XContentAccess )UnoRuntime.queryInterface(
152*cdf0e10cSrcweir                     XContentAccess.class, resultSet );
153*cdf0e10cSrcweir                 XRow row = ( XRow )UnoRuntime.queryInterface( XRow.class, resultSet );
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir                 do {
156*cdf0e10cSrcweir                     Vector propsValues = new Vector();
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir                     // Obtain URL of child.
159*cdf0e10cSrcweir                     String id = contentAccess.queryContentIdentifierString();
160*cdf0e10cSrcweir                     propsValues.add( id );
161*cdf0e10cSrcweir                     for ( int i = 1; i <= size ; i++)  {
162*cdf0e10cSrcweir                         Object propValue = row.getObject( i, null );
163*cdf0e10cSrcweir                         if ( !row.wasNull() && !(propValue instanceof com.sun.star.uno.Any )) {
164*cdf0e10cSrcweir                             propsValues.add( propValue );
165*cdf0e10cSrcweir                         } else {
166*cdf0e10cSrcweir                             propsValues.add( "[ Property not found ]" );
167*cdf0e10cSrcweir                         }
168*cdf0e10cSrcweir                     }
169*cdf0e10cSrcweir                     result.add( propsValues );
170*cdf0e10cSrcweir                 } while ( resultSet.next() ); // next child
171*cdf0e10cSrcweir             }
172*cdf0e10cSrcweir         }
173*cdf0e10cSrcweir         return result;
174*cdf0e10cSrcweir     }
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir     /**
177*cdf0e10cSrcweir      *  Get connect URL.
178*cdf0e10cSrcweir      *
179*cdf0e10cSrcweir      *@return   String  That contains the connect URL
180*cdf0e10cSrcweir      */
181*cdf0e10cSrcweir     public String getContentURL() {
182*cdf0e10cSrcweir         return m_contenturl;
183*cdf0e10cSrcweir     }
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir     /**
186*cdf0e10cSrcweir      * Get the properties.
187*cdf0e10cSrcweir      *
188*cdf0e10cSrcweir      *@return String    That contains the properties
189*cdf0e10cSrcweir      */
190*cdf0e10cSrcweir     public Vector getProperties() {
191*cdf0e10cSrcweir         return m_propnames;
192*cdf0e10cSrcweir     }
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir     /**
195*cdf0e10cSrcweir      * Parse arguments
196*cdf0e10cSrcweir      *
197*cdf0e10cSrcweir      *@param      String[]   Arguments
198*cdf0e10cSrcweir      *@exception  java.lang.Exception
199*cdf0e10cSrcweir      */
200*cdf0e10cSrcweir     public void parseArguments( String[] args ) throws java.lang.Exception {
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir         for ( int i = 0; i < args.length; i++ ) {
203*cdf0e10cSrcweir             if ( args[i].startsWith( "-url=" )) {
204*cdf0e10cSrcweir                 m_contenturl    = args[i].substring( 5 );
205*cdf0e10cSrcweir             } else if ( args[i].startsWith( "-propNames=" )) {
206*cdf0e10cSrcweir                 StringTokenizer tok
207*cdf0e10cSrcweir                     = new StringTokenizer( args[i].substring( 11 ), ";" );
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir                 while ( tok.hasMoreTokens() )
210*cdf0e10cSrcweir                     m_propnames.add( tok.nextToken() );
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir             } else if ( args[i].startsWith( "-help" ) ||
213*cdf0e10cSrcweir                         args[i].startsWith( "-?" )) {
214*cdf0e10cSrcweir                 printCmdLineUsage();
215*cdf0e10cSrcweir                 System.exit( 0 );
216*cdf0e10cSrcweir             }
217*cdf0e10cSrcweir         }
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir         if ( m_contenturl == null || m_contenturl.equals( "" )) {
220*cdf0e10cSrcweir             m_contenturl    = "file:///";
221*cdf0e10cSrcweir         }
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir         if ( m_propnames.size() == 0 ) {
224*cdf0e10cSrcweir             m_propnames.add( "Title" );
225*cdf0e10cSrcweir             m_propnames.add( "IsDocument" );
226*cdf0e10cSrcweir         }
227*cdf0e10cSrcweir     }
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir     /**
230*cdf0e10cSrcweir      * Print the commands options
231*cdf0e10cSrcweir      */
232*cdf0e10cSrcweir     public void printCmdLineUsage() {
233*cdf0e10cSrcweir         System.out.println(
234*cdf0e10cSrcweir             "Usage   : ChildrenRetriever -url=... -propNames=..." );
235*cdf0e10cSrcweir         System.out.println(
236*cdf0e10cSrcweir             "Defaults: -url=file:/// -propNames=Title,IsDocument" );
237*cdf0e10cSrcweir         System.out.println(
238*cdf0e10cSrcweir             "\nExample : -url=file:///temp/ -propNames=Title;IsFolder;IsDocument" );
239*cdf0e10cSrcweir     }
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir     /**
242*cdf0e10cSrcweir      *  Print all properties out contained in vector .
243*cdf0e10cSrcweir      *
244*cdf0e10cSrcweir      *@param   Vector
245*cdf0e10cSrcweir      */
246*cdf0e10cSrcweir     public void printLine( Vector props ) {
247*cdf0e10cSrcweir         int limit;
248*cdf0e10cSrcweir         while ( !props.isEmpty() )   {
249*cdf0e10cSrcweir             String print = "";
250*cdf0e10cSrcweir             int size  = props.size();
251*cdf0e10cSrcweir             for ( int i = 0; i < size; i++ ) {
252*cdf0e10cSrcweir                 limit = 15;
253*cdf0e10cSrcweir                 Object obj = props.get( i );
254*cdf0e10cSrcweir                 if ( obj != null)  {
255*cdf0e10cSrcweir                     String prop = obj.toString();
256*cdf0e10cSrcweir                     int leng = prop.length();
257*cdf0e10cSrcweir                     if ( leng < limit ) {
258*cdf0e10cSrcweir                         for ( int l = leng; l < limit; l++) {
259*cdf0e10cSrcweir                             prop += " ";
260*cdf0e10cSrcweir                         }
261*cdf0e10cSrcweir                         print+= prop + "  ";
262*cdf0e10cSrcweir                         props.set( i, null );
263*cdf0e10cSrcweir                     } else {
264*cdf0e10cSrcweir                         String temp1 = prop.substring( 0, limit );
265*cdf0e10cSrcweir                         String temp2 = prop.substring( limit );
266*cdf0e10cSrcweir                         print+= temp1 + "  ";
267*cdf0e10cSrcweir                         props.set( i, temp2 );
268*cdf0e10cSrcweir                     }
269*cdf0e10cSrcweir                 } else  {
270*cdf0e10cSrcweir                     for ( int l = 0; l < limit; l++) {
271*cdf0e10cSrcweir                         print += " ";
272*cdf0e10cSrcweir                     }
273*cdf0e10cSrcweir                     print+= "  ";
274*cdf0e10cSrcweir                 }
275*cdf0e10cSrcweir             }
276*cdf0e10cSrcweir             System.out.println( print );
277*cdf0e10cSrcweir             boolean isEmpty = true;
278*cdf0e10cSrcweir             for ( int i = 0; i < size; i++ ) {
279*cdf0e10cSrcweir             Object obj = props.get( i );
280*cdf0e10cSrcweir             if( obj != null )
281*cdf0e10cSrcweir                 isEmpty = false;
282*cdf0e10cSrcweir             }
283*cdf0e10cSrcweir             if( isEmpty )
284*cdf0e10cSrcweir                 props.clear();
285*cdf0e10cSrcweir         }
286*cdf0e10cSrcweir     }
287*cdf0e10cSrcweir 
288*cdf0e10cSrcweir     /**
289*cdf0e10cSrcweir      *  Create a new connection with the specific args to a running office and
290*cdf0e10cSrcweir      *  access the children from a folder.
291*cdf0e10cSrcweir      *
292*cdf0e10cSrcweir      *@param  String[]   Arguments
293*cdf0e10cSrcweir      */
294*cdf0e10cSrcweir     public static void main ( String args[] ) {
295*cdf0e10cSrcweir 
296*cdf0e10cSrcweir         System.out.println( "\n" );
297*cdf0e10cSrcweir         System.out.println(
298*cdf0e10cSrcweir             "-----------------------------------------------------------------" );
299*cdf0e10cSrcweir         System.out.println(
300*cdf0e10cSrcweir             "ChildrenRetriever - obtains the children of a folder resource." );
301*cdf0e10cSrcweir         System.out.println(
302*cdf0e10cSrcweir             "-----------------------------------------------------------------" );
303*cdf0e10cSrcweir 
304*cdf0e10cSrcweir         try {
305*cdf0e10cSrcweir             ChildrenRetriever access = new ChildrenRetriever( args );
306*cdf0e10cSrcweir 
307*cdf0e10cSrcweir             // Get the properties Title and IsFolder for the children.
308*cdf0e10cSrcweir             Vector result = access.getChildren();
309*cdf0e10cSrcweir 
310*cdf0e10cSrcweir             String tempPrint = "\nChildren of resource " + access.getContentURL();
311*cdf0e10cSrcweir             int size = tempPrint.length();
312*cdf0e10cSrcweir             System.out.println( tempPrint );
313*cdf0e10cSrcweir             tempPrint = "";
314*cdf0e10cSrcweir             for( int i = 0; i < size; i++ ) {
315*cdf0e10cSrcweir                 tempPrint += "-";
316*cdf0e10cSrcweir             }
317*cdf0e10cSrcweir             System.out.println( tempPrint );
318*cdf0e10cSrcweir 
319*cdf0e10cSrcweir             if ( result != null && !result.isEmpty() ) {
320*cdf0e10cSrcweir 
321*cdf0e10cSrcweir                 Vector cont = new Vector();
322*cdf0e10cSrcweir                 cont.add("URL:");
323*cdf0e10cSrcweir                 Vector props = access.getProperties();
324*cdf0e10cSrcweir                 size = props.size();
325*cdf0e10cSrcweir                 for ( int i = 0; i < size; i++ ) {
326*cdf0e10cSrcweir                     Object obj = props.get( i );
327*cdf0e10cSrcweir                     String prop = obj.toString();
328*cdf0e10cSrcweir                     cont.add( prop + ":" );
329*cdf0e10cSrcweir                 }
330*cdf0e10cSrcweir                 access.printLine(cont);
331*cdf0e10cSrcweir                 System.out.println( "\n" );
332*cdf0e10cSrcweir                 for ( Enumeration e = result.elements(); e.hasMoreElements(); ) {
333*cdf0e10cSrcweir                     Vector propsV   = ( Vector )e.nextElement();
334*cdf0e10cSrcweir                     access.printLine( propsV );
335*cdf0e10cSrcweir                 }
336*cdf0e10cSrcweir             }
337*cdf0e10cSrcweir         } catch ( com.sun.star.ucb.ResultSetException e ) {
338*cdf0e10cSrcweir             System.out.println( "Error: " + e );
339*cdf0e10cSrcweir         } catch ( com.sun.star.ucb.CommandAbortedException e ) {
340*cdf0e10cSrcweir             System.out.println( "Error: " + e );
341*cdf0e10cSrcweir         } catch ( com.sun.star.uno.Exception e ) {
342*cdf0e10cSrcweir             System.out.println( "Error: " + e );
343*cdf0e10cSrcweir         } catch ( java.lang.Exception e ) {
344*cdf0e10cSrcweir             System.out.println( "Error: " + e );
345*cdf0e10cSrcweir         }
346*cdf0e10cSrcweir         System.exit( 0 );
347*cdf0e10cSrcweir     }
348*cdf0e10cSrcweir }
349