1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 import com.sun.star.ucb.NameClash;
25 import com.sun.star.ucb.TransferCommandOperation;
26 import com.sun.star.ucb.GlobalTransferCommandArgument;
27 import com.sun.star.uno.UnoRuntime;
28 import com.sun.star.uno.XInterface;
29 
30 /**
31  * Copying, Moving and Creating Links to a Resource
32  */
33 public class ResourceManager {
34 
35     /**
36      * Member properties
37      */
38     private  Helper      m_helper;
39     private  XInterface  m_ucb;
40     private  String      m_contenturl = "";
41     private  String      m_srcURL = "";
42     private  String      m_targetFolderURL = "";
43     private  String      m_newTitle = "";
44     private  String      m_transOperation = "";
45 
46     /**
47      * Constructor.
48      *
49      *@param      String[]   This construtor requires the arguments:
50      *                          -url=...             (optional)
51      *                          -targetFolderURL=... (optional)
52      *                          -newTitle=...        (optional)
53      *                          -transOper=...       (optional)
54      *                          -workdir=...         (optional)
55      *                       See Help (method printCmdLineUsage()).
56      *                       Without the arguments a new connection to a
57      *                       running office cannot created.
58      *@exception  java.lang.Exception
59      */
ResourceManager( String args[] )60     public ResourceManager( String args[] ) throws java.lang.Exception {
61 
62         // Parse arguments
63         parseArguments( args );
64 
65         // Init
66         m_helper       = new Helper( getContentURL() );
67 
68         // Get xUCB
69         m_ucb          = m_helper.getUCB();
70     }
71 
72     /**
73      *  Copy, move or create a link for a resource.
74      *  This method requires the main and the optional arguments to be set in order to work.
75      *  See Constructor.
76      *
77      *@return boolean  Returns true if resource successfully transfered, false otherwise
78      *@exception  com.sun.star.ucb.CommandAbortedException
79      *@exception  com.sun.star.uno.Exception
80      */
transferResource()81     public boolean transferResource()
82         throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception {
83         String sourceURL       = getContentURL();      // URL of the source object
84         String targetFolderURL = getTargetFolderURL(); // URL of the target folder
85         String newTitle        = getNewTitle();        // New name for the resource
86         String transOperation  = getTransOperation();
87         return transferResource( sourceURL, targetFolderURL, newTitle, transOperation );
88     }
89 
90     /**
91      *  Copy, move or create a link for a resource.
92      *
93      *@param  String   Source URL
94      *@param  String   Target folder URL
95      *@param  String   Transfering operation (copy, move, link)
96      *@return boolean  Returns true if resource successfully transfered, false otherwise
97      *@exception  com.sun.star.ucb.CommandAbortedException
98      *@exception  com.sun.star.uno.Exception
99      */
transferResource( String sourceURL, String targetFolderURL, String newTitle, String transOperation )100     public boolean transferResource(
101             String sourceURL, String targetFolderURL,
102             String newTitle, String transOperation )
103         throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception {
104 
105         boolean result = false;
106         if ( m_ucb != null && sourceURL != null && !sourceURL.equals( "" ) &&
107              targetFolderURL != null && !targetFolderURL.equals( "" ) &&
108              newTitle != null && transOperation != null && !transOperation.equals( "" ) &&
109              ( transOperation.equals( "copy" ) || transOperation.equals( "move" ) ||
110                transOperation.equals( "link" ))) {
111 
112             /////////////////////////////////////////////////////////////////////
113             // Copy, move or create a link for a resource to another location...
114             /////////////////////////////////////////////////////////////////////
115             GlobalTransferCommandArgument arg = new GlobalTransferCommandArgument();
116             if ( transOperation.equals( "copy" )) {
117                 arg.Operation = TransferCommandOperation.COPY;
118             } else if ( transOperation.equals( "move" )) {
119                 arg.Operation = TransferCommandOperation.MOVE;
120             } else if ( transOperation.equals( "link" )) {
121                 arg.Operation = TransferCommandOperation.LINK;
122             }
123             arg.SourceURL = sourceURL;
124             arg.TargetURL = targetFolderURL;
125 
126             // object get a new unique name
127             arg.NewTitle  = newTitle;
128 
129             // fail, if object with same name exists in target folder
130             arg.NameClash = NameClash.ERROR;
131 
132             // Let UCB execute the command "globalTransfer".
133             m_helper.executeCommand( m_ucb, "globalTransfer", arg );
134             result = true;
135         }
136         return result;
137     }
138 
139     /**
140      *  Get connect URL.
141      *
142      *@return   String    That contains the connect URL
143      */
getContentURL()144     public String getContentURL() {
145         return m_contenturl;
146     }
147 
148     /**
149      * Get trasfering Operation.
150      *
151      *@return String    That contains the trasfering Operation
152      */
getTransOperation()153     public String getTransOperation() {
154         return m_transOperation;
155     }
156 
157     /**
158      * Get target folder URL.
159      *
160      *@return String    That contains the target folder URL
161      */
getTargetFolderURL()162     public String getTargetFolderURL() {
163         return m_targetFolderURL;
164     }
165 
166     /**
167      * Get new title for the resource to be transfered.
168      *
169      *@return String    That contains a new title for the transfered
170      *                  resource. Can be empty. In this case resource
171      *                  will keep the title it has in the source folder.
172      */
getNewTitle()173     public String getNewTitle() {
174         return m_newTitle;
175     }
176 
177     /**
178      * Parse arguments
179      *
180      *@param      String[]   Arguments
181      *@exception  java.lang.Exception
182      */
parseArguments( String[] args )183     public void parseArguments( String[] args ) throws java.lang.Exception {
184 
185         String workdir = "";
186 
187         for ( int i = 0; i < args.length; i++ ) {
188             if ( args[i].startsWith( "-url=" )) {
189                 m_contenturl    = args[i].substring( 5 );
190             } else if ( args[i].startsWith( "-targetFolderURL=" )) {
191                 m_targetFolderURL = args[i].substring( 17 );
192             } else if ( args[i].startsWith( "-newTitle=" )) {
193                 m_newTitle = args[i].substring( 10 );
194             } else if ( args[i].startsWith( "-transOper=" )) {
195                 m_transOperation = args[i].substring( 11 );
196             } else if ( args[i].startsWith( "-workdir=" )) {
197                 workdir = args[i].substring( 9 );
198             } else if ( args[i].startsWith( "-help" ) ||
199                         args[i].startsWith( "-?" )) {
200                 printCmdLineUsage();
201                 System.exit( 0 );
202             }
203         }
204 
205 		if ( m_contenturl == null || m_contenturl.equals( "" )) {
206             m_contenturl = Helper.prependCurrentDirAsAbsoluteFileURL( "data/data.txt" );;
207         }
208 
209         if ( m_targetFolderURL == null || m_targetFolderURL.equals( "" )) {
210             m_targetFolderURL = Helper.getAbsoluteFileURLFromSystemPath( workdir );
211         }
212 
213         if ( m_newTitle == null || m_newTitle.equals( "" )) {
214             m_newTitle = "transfered-resource-" + System.currentTimeMillis();
215         }
216 
217         if ( m_transOperation == null || m_transOperation.equals( "" )) {
218             m_transOperation = "copy";
219         }
220     }
221 
222     /**
223      * Print the commands options
224      */
printCmdLineUsage()225     public void printCmdLineUsage() {
226         System.out.println(
227             "Usage: ResourceManager -url=... -targetFolderURL=... -newTitle=... -transOper=... -workdir=..." );
228         System.out.println(
229             "Defaults: -url=<currentdir>/data/data.txt> -targetFolderURL=<workdir> -newTitle=transfered-resource-<uniquepostfix> -transOper=copy -workdir=<currentdir>");
230         System.out.println(
231             "\nExample : -url=file:///temp/MyFile.txt -targetFolderURL=file:///test/ -newTitle=RenamedFile.txt -transOper=copy " );
232     }
233 
234     /**
235      *  Create a new connection with the specific args to a running office and
236      *  copy, move or create links a resource.
237      *
238      *@param  String[]   Arguments
239      */
main( String args[] )240     public static void main ( String args[] ) {
241 
242         System.out.println( "\n" );
243 		System.out.println(
244             "-----------------------------------------------------------------" );
245 		System.out.println(
246             "ResourceManager - copies/moves a resource." );
247 		System.out.println(
248             "-----------------------------------------------------------------" );
249 
250         try {
251             ResourceManager transResource = new ResourceManager( args );
252             String sourceURL       = transResource.getContentURL();
253             String targetFolderURL = transResource.getTargetFolderURL();
254             String newTitle        = transResource.getNewTitle();
255             String transOperation  = transResource.getTransOperation();
256             boolean result = transResource.transferResource(
257                                 sourceURL, targetFolderURL, newTitle, transOperation );
258             if ( result )
259                 System.out.println( "\nTransfering resource succeeded." );
260             else
261                 System.out.println( "Transfering resource failed." );
262 
263             System.out.println( "   Source URL        : " + sourceURL );
264             System.out.println( "   Target Folder URL : " + targetFolderURL );
265             System.out.println( "   New name          : " + newTitle );
266             System.out.println( "   Transfer Operation: " + transOperation );
267 
268 
269         } catch ( com.sun.star.ucb.CommandAbortedException e ) {
270             System.out.println( "Error: " + e );
271         } catch ( com.sun.star.uno.Exception e ) {
272             System.out.println( "Error: " + e );
273         } catch ( java.lang.Exception e ) {
274             System.out.println( "Error: " + e );
275         }
276         System.exit( 0 );
277     }
278 }
279