1 /*************************************************************************
2  *
3  *  The Contents of this file are made available subject to the terms of
4  *  the BSD license.
5  *
6  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7  *  All rights reserved.
8  *
9  *  Redistribution and use in source and binary forms, with or without
10  *  modification, are permitted provided that the following conditions
11  *  are met:
12  *  1. Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *  2. Redistributions in binary form must reproduce the above copyright
15  *     notice, this list of conditions and the following disclaimer in the
16  *     documentation and/or other materials provided with the distribution.
17  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18  *     contributors may be used to endorse or promote products derived
19  *     from this software without specific prior written permission.
20  *
21  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  *************************************************************************/
34 
35 import com.sun.star.ucb.NameClash;
36 import com.sun.star.ucb.TransferCommandOperation;
37 import com.sun.star.ucb.GlobalTransferCommandArgument;
38 import com.sun.star.uno.UnoRuntime;
39 import com.sun.star.uno.XInterface;
40 
41 /**
42  * Copying, Moving and Creating Links to a Resource
43  */
44 public class ResourceManager {
45 
46     /**
47      * Member properties
48      */
49     private  Helper      m_helper;
50     private  XInterface  m_ucb;
51     private  String      m_contenturl = "";
52     private  String      m_srcURL = "";
53     private  String      m_targetFolderURL = "";
54     private  String      m_newTitle = "";
55     private  String      m_transOperation = "";
56 
57     /**
58      * Constructor.
59      *
60      *@param      String[]   This construtor requires the arguments:
61      *                          -url=...             (optional)
62      *                          -targetFolderURL=... (optional)
63      *                          -newTitle=...        (optional)
64      *                          -transOper=...       (optional)
65      *                          -workdir=...         (optional)
66      *                       See Help (method printCmdLineUsage()).
67      *                       Without the arguments a new connection to a
68      *                       running office cannot created.
69      *@exception  java.lang.Exception
70      */
71     public ResourceManager( String args[] ) throws java.lang.Exception {
72 
73         // Parse arguments
74         parseArguments( args );
75 
76         // Init
77         m_helper       = new Helper( getContentURL() );
78 
79         // Get xUCB
80         m_ucb          = m_helper.getUCB();
81     }
82 
83     /**
84      *  Copy, move or create a link for a resource.
85      *  This method requires the main and the optional arguments to be set in order to work.
86      *  See Constructor.
87      *
88      *@return boolean  Returns true if resource successfully transfered, false otherwise
89      *@exception  com.sun.star.ucb.CommandAbortedException
90      *@exception  com.sun.star.uno.Exception
91      */
92     public boolean transferResource()
93         throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception {
94         String sourceURL       = getContentURL();      // URL of the source object
95         String targetFolderURL = getTargetFolderURL(); // URL of the target folder
96         String newTitle        = getNewTitle();        // New name for the resource
97         String transOperation  = getTransOperation();
98         return transferResource( sourceURL, targetFolderURL, newTitle, transOperation );
99     }
100 
101     /**
102      *  Copy, move or create a link for a resource.
103      *
104      *@param  String   Source URL
105      *@param  String   Target folder URL
106      *@param  String   Transfering operation (copy, move, link)
107      *@return boolean  Returns true if resource successfully transfered, false otherwise
108      *@exception  com.sun.star.ucb.CommandAbortedException
109      *@exception  com.sun.star.uno.Exception
110      */
111     public boolean transferResource(
112             String sourceURL, String targetFolderURL,
113             String newTitle, String transOperation )
114         throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception {
115 
116         boolean result = false;
117         if ( m_ucb != null && sourceURL != null && !sourceURL.equals( "" ) &&
118              targetFolderURL != null && !targetFolderURL.equals( "" ) &&
119              newTitle != null && transOperation != null && !transOperation.equals( "" ) &&
120              ( transOperation.equals( "copy" ) || transOperation.equals( "move" ) ||
121                transOperation.equals( "link" ))) {
122 
123             /////////////////////////////////////////////////////////////////////
124             // Copy, move or create a link for a resource to another location...
125             /////////////////////////////////////////////////////////////////////
126             GlobalTransferCommandArgument arg = new GlobalTransferCommandArgument();
127             if ( transOperation.equals( "copy" )) {
128                 arg.Operation = TransferCommandOperation.COPY;
129             } else if ( transOperation.equals( "move" )) {
130                 arg.Operation = TransferCommandOperation.MOVE;
131             } else if ( transOperation.equals( "link" )) {
132                 arg.Operation = TransferCommandOperation.LINK;
133             }
134             arg.SourceURL = sourceURL;
135             arg.TargetURL = targetFolderURL;
136 
137             // object get a new unique name
138             arg.NewTitle  = newTitle;
139 
140             // fail, if object with same name exists in target folder
141             arg.NameClash = NameClash.ERROR;
142 
143             // Let UCB execute the command "globalTransfer".
144             m_helper.executeCommand( m_ucb, "globalTransfer", arg );
145             result = true;
146         }
147         return result;
148     }
149 
150     /**
151      *  Get connect URL.
152      *
153      *@return   String    That contains the connect URL
154      */
155     public String getContentURL() {
156         return m_contenturl;
157     }
158 
159     /**
160      * Get trasfering Operation.
161      *
162      *@return String    That contains the trasfering Operation
163      */
164     public String getTransOperation() {
165         return m_transOperation;
166     }
167 
168     /**
169      * Get target folder URL.
170      *
171      *@return String    That contains the target folder URL
172      */
173     public String getTargetFolderURL() {
174         return m_targetFolderURL;
175     }
176 
177     /**
178      * Get new title for the resource to be transfered.
179      *
180      *@return String    That contains a new title for the transfered
181      *                  resource. Can be empty. In this case resource
182      *                  will keep the title it has in the source folder.
183      */
184     public String getNewTitle() {
185         return m_newTitle;
186     }
187 
188     /**
189      * Parse arguments
190      *
191      *@param      String[]   Arguments
192      *@exception  java.lang.Exception
193      */
194     public void parseArguments( String[] args ) throws java.lang.Exception {
195 
196         String workdir = "";
197 
198         for ( int i = 0; i < args.length; i++ ) {
199             if ( args[i].startsWith( "-url=" )) {
200                 m_contenturl    = args[i].substring( 5 );
201             } else if ( args[i].startsWith( "-targetFolderURL=" )) {
202                 m_targetFolderURL = args[i].substring( 17 );
203             } else if ( args[i].startsWith( "-newTitle=" )) {
204                 m_newTitle = args[i].substring( 10 );
205             } else if ( args[i].startsWith( "-transOper=" )) {
206                 m_transOperation = args[i].substring( 11 );
207             } else if ( args[i].startsWith( "-workdir=" )) {
208                 workdir = args[i].substring( 9 );
209             } else if ( args[i].startsWith( "-help" ) ||
210                         args[i].startsWith( "-?" )) {
211                 printCmdLineUsage();
212                 System.exit( 0 );
213             }
214         }
215 
216 		if ( m_contenturl == null || m_contenturl.equals( "" )) {
217             m_contenturl = Helper.prependCurrentDirAsAbsoluteFileURL( "data/data.txt" );;
218         }
219 
220         if ( m_targetFolderURL == null || m_targetFolderURL.equals( "" )) {
221             m_targetFolderURL = Helper.getAbsoluteFileURLFromSystemPath( workdir );
222         }
223 
224         if ( m_newTitle == null || m_newTitle.equals( "" )) {
225             m_newTitle = "transfered-resource-" + System.currentTimeMillis();
226         }
227 
228         if ( m_transOperation == null || m_transOperation.equals( "" )) {
229             m_transOperation = "copy";
230         }
231     }
232 
233     /**
234      * Print the commands options
235      */
236     public void printCmdLineUsage() {
237         System.out.println(
238             "Usage: ResourceManager -url=... -targetFolderURL=... -newTitle=... -transOper=... -workdir=..." );
239         System.out.println(
240             "Defaults: -url=<currentdir>/data/data.txt> -targetFolderURL=<workdir> -newTitle=transfered-resource-<uniquepostfix> -transOper=copy -workdir=<currentdir>");
241         System.out.println(
242             "\nExample : -url=file:///temp/MyFile.txt -targetFolderURL=file:///test/ -newTitle=RenamedFile.txt -transOper=copy " );
243     }
244 
245     /**
246      *  Create a new connection with the specific args to a running office and
247      *  copy, move or create links a resource.
248      *
249      *@param  String[]   Arguments
250      */
251     public static void main ( String args[] ) {
252 
253         System.out.println( "\n" );
254 		System.out.println(
255             "-----------------------------------------------------------------" );
256 		System.out.println(
257             "ResourceManager - copies/moves a resource." );
258 		System.out.println(
259             "-----------------------------------------------------------------" );
260 
261         try {
262             ResourceManager transResource = new ResourceManager( args );
263             String sourceURL       = transResource.getContentURL();
264             String targetFolderURL = transResource.getTargetFolderURL();
265             String newTitle        = transResource.getNewTitle();
266             String transOperation  = transResource.getTransOperation();
267             boolean result = transResource.transferResource(
268                                 sourceURL, targetFolderURL, newTitle, transOperation );
269             if ( result )
270                 System.out.println( "\nTransfering resource succeeded." );
271             else
272                 System.out.println( "Transfering resource failed." );
273 
274             System.out.println( "   Source URL        : " + sourceURL );
275             System.out.println( "   Target Folder URL : " + targetFolderURL );
276             System.out.println( "   New name          : " + newTitle );
277             System.out.println( "   Transfer Operation: " + transOperation );
278 
279 
280         } catch ( com.sun.star.ucb.CommandAbortedException e ) {
281             System.out.println( "Error: " + e );
282         } catch ( com.sun.star.uno.Exception e ) {
283             System.out.println( "Error: " + e );
284         } catch ( java.lang.Exception e ) {
285             System.out.println( "Error: " + e );
286         }
287         System.exit( 0 );
288     }
289 }
290