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 /*
25  * Created on 31.10.2003
26  *
27  * To change the template for this generated file go to
28  * Window>Preferences>Java>Code Generation>Code and Comments
29  */
30 package com.sun.star.wizards.common;
31 
32 import java.util.List;
33 import java.util.Vector;
34 
35 import com.sun.star.beans.Property;
36 import com.sun.star.lang.XMultiServiceFactory;
37 import com.sun.star.sdbc.XResultSet;
38 import com.sun.star.sdbc.XRow;
39 import com.sun.star.ucb.*;
40 import com.sun.star.uno.UnoRuntime;
41 
42 /**
43  * @author rpiterman
44  * This class is used to copy the content of a folder to
45  * another folder.
46  * There is an incosistency with argument order.
47  * It should be always: dir,filename.
48  */
49 public class UCB
50 {
51 
52     private Object ucb;
53     private FileAccess fa;
54 
UCB(XMultiServiceFactory xmsf)55     public UCB(XMultiServiceFactory xmsf) throws Exception
56     {
57         String[] keys = new String[2];
58         keys[ 0 ] = "Local";
59         keys[ 1 ] = "Office";
60         ucb = xmsf.createInstanceWithArguments(
61             "com.sun.star.ucb.UniversalContentBroker", keys );
62         fa = new FileAccess(xmsf);
63     }
64 
deleteDirContent(String dir)65     public void deleteDirContent(String dir)
66         throws Exception
67     {
68         if (!fa.exists(dir,true))
69         {
70           return;
71         }
72         List l = listFiles(dir,null);
73         for (int i = 0; i<l.size(); i++)
74         {
75             delete(FileAccess.connectURLs(dir ,(String)l.get(i)));
76     }
77     }
78 
delete(String filename)79     public void delete(String filename) throws Exception
80     {
81         //System.out.println("UCB.delete(" + filename);
82         executeCommand( getContent(filename),"delete",Boolean.TRUE);
83     }
84 
copy(String sourceDir, String targetDir)85     public void copy(String sourceDir, String targetDir) throws Exception
86     {
87         copy(sourceDir,targetDir,(Verifier)null);
88     }
89 
copy(String sourceDir, String targetDir, Verifier verifier)90     public void copy(String sourceDir, String targetDir, Verifier verifier) throws Exception
91     {
92         List files = listFiles(sourceDir,verifier);
93         for (int i = 0; i<files.size(); i++)
94         {
95           copy(sourceDir, (String)files.get(i), targetDir);
96         }
97 
98     }
99 
copy(String sourceDir, String filename, String targetDir, String targetName)100     public void copy(String sourceDir, String filename, String targetDir, String targetName) throws Exception
101     {
102         if (!fa.exists(targetDir,true))
103         {
104           fa.fileAccess.createFolder(targetDir);
105         }
106         //System.out.println("UCB.copy(" + sourceDir + ", " + filename +  ", " + targetDir+ ", " + targetName);
107         executeCommand(ucb, "globalTransfer", copyArg(sourceDir,filename, targetDir,targetName));
108     }
109 
110     /**
111      * @deprecated
112      * @param sourceDir
113      * @param filename
114      * @param targetDir
115      * @throws Exception
116      */
copy(String sourceDir, String filename, String targetDir)117     public void copy(String sourceDir, String filename, String targetDir) throws Exception
118     {
119         copy(sourceDir,filename, targetDir, PropertyNames.EMPTY_STRING);
120     }
121 
122     /**
123      * target name can be PropertyNames.EMPTY_STRING, in which case the name stays lige the source name
124      * @param sourceDir
125      * @param sourceFilename
126      * @param targetDir
127      * @param targetFilename
128      * @return
129      */
copyArg(String sourceDir, String sourceFilename, String targetDir, String targetFilename)130     public GlobalTransferCommandArgument copyArg(String sourceDir, String sourceFilename, String targetDir, String targetFilename)
131     {
132 
133         GlobalTransferCommandArgument aArg = new GlobalTransferCommandArgument();
134         aArg.Operation = TransferCommandOperation.COPY;
135         aArg.SourceURL = fa.getURL(sourceDir,sourceFilename);
136         aArg.TargetURL = targetDir;
137         aArg.NewTitle = targetFilename;
138         // fail, if object with same name exists in target folder
139         aArg.NameClash = NameClash.OVERWRITE;
140         return aArg;
141     }
142 
executeCommand(Object xContent, String aCommandName, Object aArgument)143     public Object executeCommand(Object xContent, String aCommandName, Object aArgument)
144         throws com.sun.star.ucb.CommandAbortedException,
145             com.sun.star.uno.Exception
146     {
147         XCommandProcessor xCmdProcessor = UnoRuntime.queryInterface(
148             XCommandProcessor.class, xContent);
149         Command aCommand  = new Command();
150         aCommand.Name     = aCommandName;
151         aCommand.Handle   = -1; // not available
152         aCommand.Argument = aArgument;
153         return xCmdProcessor.execute(aCommand, 0, null);
154     }
155 
listFiles(String path, Verifier verifier)156     public List listFiles(String path, Verifier verifier) throws Exception
157     {
158         Object xContent = getContent(path);
159 
160         OpenCommandArgument2 aArg = new OpenCommandArgument2();
161         aArg.Mode = OpenMode.ALL;
162         aArg.Priority = 32768;
163 
164         // Fill info for the properties wanted.
165         aArg.Properties = new Property[] {new Property()};
166 
167         aArg.Properties[0].Name = PropertyNames.PROPERTY_TITLE;
168         aArg.Properties[0].Handle = -1;
169 
170         XDynamicResultSet xSet;
171 
172         xSet = UnoRuntime.queryInterface(
173           XDynamicResultSet.class,executeCommand(xContent, "open", aArg));
174 
175         XResultSet xResultSet = xSet.getStaticResultSet();
176 
177         List files = new Vector();
178 
179         if (xResultSet.first())
180         {
181             // obtain XContentAccess interface for child content access and XRow for properties
182             XContentAccess xContentAccess = UnoRuntime.queryInterface(
183                 XContentAccess.class, xResultSet);
184             XRow xRow = UnoRuntime.queryInterface(XRow.class, xResultSet);
185             do
186             {
187                 // Obtain URL of child.
188                 String aId = xContentAccess.queryContentIdentifierString();
189                 // First column: Title (column numbers are 1-based!)
190                 String aTitle = xRow.getString(1);
191                 if (aTitle.length() == 0 && xRow.wasNull())
192                 {
193                     //ignore
194                 }
195                 else
196                 {
197                     files.add(aTitle);
198                 }
199             }
200             while (xResultSet.next()); // next child
201         }
202 
203         if (verifier != null)
204         {
205             for (int i = 0; i<files.size(); i++)
206             {
207                 if (!verifier.verify(files.get(i)))
208                 {
209                     files.remove(i--);
210                 }
211             }
212         }
213 
214         return files;
215     }
216 
getContentProperty(Object content, String propName, Class type)217     public Object getContentProperty(Object content, String propName, Class type)
218         throws Exception
219     {
220         Property[] pv = new Property[1];
221         pv[0] = new Property();
222         pv[0].Name = propName;
223         pv[0].Handle = -1;
224 
225         Object row = executeCommand(content,"getPropertyValues",pv);
226         XRow xrow = UnoRuntime.queryInterface(XRow.class,row);
227         if (type.equals(String.class))
228         {
229            return xrow.getString(1);
230         }
231         else if (type.equals(Boolean.class))
232         {
233             return xrow.getBoolean(1) ? Boolean.TRUE : Boolean.FALSE;
234         }
235         else if (type.equals(Integer.class))
236         {
237             return new Integer(xrow.getInt(1));
238         }
239         else if (type.equals(Short.class))
240         {
241             return new Short(xrow.getShort(1));
242         }
243         else
244         {
245             return null;
246         }
247 
248     }
249 
getContent(String path)250     public Object getContent(String path) throws Exception
251     {
252         //System.out.println("Getting Content for : " + path);
253         XContentIdentifier id = UnoRuntime.queryInterface(XContentIdentifierFactory.class, ucb).createContentIdentifier(path);
254 
255         return UnoRuntime.queryInterface(
256           XContentProvider.class,ucb).queryContent(id);
257     }
258 
259     public static interface Verifier
260     {
261 
verify(Object object)262         public boolean verify(Object object);
263     }
264 }
265