1*c07c6c98SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*c07c6c98SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*c07c6c98SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*c07c6c98SAndrew Rist  * distributed with this work for additional information
6*c07c6c98SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*c07c6c98SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*c07c6c98SAndrew Rist  * "License"); you may not use this file except in compliance
9*c07c6c98SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*c07c6c98SAndrew Rist  *
11*c07c6c98SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*c07c6c98SAndrew Rist  *
13*c07c6c98SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*c07c6c98SAndrew Rist  * software distributed under the License is distributed on an
15*c07c6c98SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*c07c6c98SAndrew Rist  * KIND, either express or implied.  See the License for the
17*c07c6c98SAndrew Rist  * specific language governing permissions and limitations
18*c07c6c98SAndrew Rist  * under the License.
19*c07c6c98SAndrew Rist  *
20*c07c6c98SAndrew Rist  *************************************************************/
21*c07c6c98SAndrew Rist 
22*c07c6c98SAndrew Rist 
23cdf0e10cSrcweir package complex.tdoc;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import com.sun.star.beans.Property;
26cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
27cdf0e10cSrcweir import com.sun.star.document.XDocumentSubStorageSupplier;
28cdf0e10cSrcweir import com.sun.star.embed.ElementModes;
29cdf0e10cSrcweir import com.sun.star.embed.XStorage;
30cdf0e10cSrcweir import com.sun.star.frame.XModel;
31cdf0e10cSrcweir import com.sun.star.frame.XTransientDocumentsDocumentContentFactory;
32cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
33cdf0e10cSrcweir import com.sun.star.sdbc.XResultSet;
34cdf0e10cSrcweir import com.sun.star.sdbc.XRow;
35cdf0e10cSrcweir import com.sun.star.text.XTextDocument;
36cdf0e10cSrcweir import com.sun.star.ucb.Command;
37cdf0e10cSrcweir import com.sun.star.ucb.ContentInfo;
38cdf0e10cSrcweir import com.sun.star.ucb.OpenCommandArgument2;
39cdf0e10cSrcweir import com.sun.star.ucb.OpenMode;
40cdf0e10cSrcweir import com.sun.star.ucb.XCommandProcessor;
41cdf0e10cSrcweir import com.sun.star.ucb.XContent;
42cdf0e10cSrcweir import com.sun.star.ucb.XDynamicResultSet;
43cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
44cdf0e10cSrcweir import util.WriterTools;
45cdf0e10cSrcweir import util.utils;
46cdf0e10cSrcweir 
47cdf0e10cSrcweir import org.junit.After;
48cdf0e10cSrcweir import org.junit.AfterClass;
49cdf0e10cSrcweir import org.junit.Before;
50cdf0e10cSrcweir import org.junit.BeforeClass;
51cdf0e10cSrcweir import org.junit.Test;
52cdf0e10cSrcweir import org.openoffice.test.OfficeConnection;
53cdf0e10cSrcweir import static org.junit.Assert.*;
54cdf0e10cSrcweir /**
55cdf0e10cSrcweir  *
56cdf0e10cSrcweir  */
57cdf0e10cSrcweir public class CheckTransientDocumentsDocumentContent {
58cdf0e10cSrcweir     // TODO: document doesn't exists
59cdf0e10cSrcweir     private final String testDocuments = "sForm.sxw";
60cdf0e10cSrcweir     private final String folderName = "TestFolder";
61cdf0e10cSrcweir     private XMultiServiceFactory xMSF = null;
62cdf0e10cSrcweir     private XTextDocument xTextDoc = null;
63cdf0e10cSrcweir 
64cdf0e10cSrcweir //    public String[] getTestMethodNames() {
65cdf0e10cSrcweir //        return new String[]{"checkTransientDocumentsDocumentContent"};
66cdf0e10cSrcweir //    }
67cdf0e10cSrcweir 
before()68cdf0e10cSrcweir     @Before public void before() {
69cdf0e10cSrcweir         xMSF = getMSF();
70cdf0e10cSrcweir         System.out.println("Open a document.");
71cdf0e10cSrcweir         String fileName = TestDocument.getUrl(testDocuments);
72cdf0e10cSrcweir         xTextDoc = WriterTools.loadTextDoc(xMSF, fileName);
73cdf0e10cSrcweir         assertNotNull(xTextDoc);
74cdf0e10cSrcweir     }
after()75cdf0e10cSrcweir     @After public void after() {
76cdf0e10cSrcweir         System.out.println("Close all documents.");
77cdf0e10cSrcweir         xTextDoc.dispose();
78cdf0e10cSrcweir     }
79cdf0e10cSrcweir 
80cdf0e10cSrcweir     /**
81cdf0e10cSrcweir      * Check the provider of document content: open some documents
82cdf0e10cSrcweir      * and look if they are accessible.
83cdf0e10cSrcweir      */
checkTransientDocumentsDocumentContent()84cdf0e10cSrcweir     @Test public void checkTransientDocumentsDocumentContent() {
85cdf0e10cSrcweir         try {
86cdf0e10cSrcweir             // create a content provider
87cdf0e10cSrcweir             Object o = xMSF.createInstance("com.sun.star.comp.ucb.TransientDocumentsDocumentContentFactory");
88cdf0e10cSrcweir 
89cdf0e10cSrcweir             XTransientDocumentsDocumentContentFactory xTransientDocumentsDocumentContentFactory =
90cdf0e10cSrcweir                             UnoRuntime.queryInterface(XTransientDocumentsDocumentContentFactory.class, o);
91cdf0e10cSrcweir             // get the model from the opened document
92cdf0e10cSrcweir             XModel xModel = xTextDoc.getCurrentController().getModel();
93cdf0e10cSrcweir 
94cdf0e10cSrcweir             // a little additional check for 114733
95cdf0e10cSrcweir             XDocumentSubStorageSupplier xDocumentSubStorageSupplier = UnoRuntime.queryInterface(XDocumentSubStorageSupplier.class, xModel);
96cdf0e10cSrcweir             String[]names = xDocumentSubStorageSupplier.getDocumentSubStoragesNames();
97cdf0e10cSrcweir             for (int i=0; i<names.length; i++) {
98cdf0e10cSrcweir                 System.out.println("SubStorage names " + i + ": " +names[i]);
99cdf0e10cSrcweir             }
100cdf0e10cSrcweir             XStorage xStorage = xDocumentSubStorageSupplier.getDocumentSubStorage(names[0], ElementModes.READWRITE);
101cdf0e10cSrcweir             assertTrue("Could not get a storage from the XDocumentStorageSupplier.", xStorage != null);
102cdf0e10cSrcweir             // get content
103cdf0e10cSrcweir             XContent xContent = xTransientDocumentsDocumentContentFactory.createDocumentContent(xModel);
104cdf0e10cSrcweir             // actual test: execute some commands
105cdf0e10cSrcweir             XCommandProcessor xCommandProcessor = UnoRuntime.queryInterface(XCommandProcessor.class, xContent);
106cdf0e10cSrcweir 
107cdf0e10cSrcweir             // create the command and arguments
108cdf0e10cSrcweir             Command command = new Command();
109cdf0e10cSrcweir             OpenCommandArgument2 cmargs2 = new OpenCommandArgument2();
110cdf0e10cSrcweir             Property[]props = new Property[1];
111cdf0e10cSrcweir             props[0] = new Property();
112cdf0e10cSrcweir             props[0].Name = "Title";
113cdf0e10cSrcweir             props[0].Handle = -1;
114cdf0e10cSrcweir             cmargs2.Mode = OpenMode.ALL;
115cdf0e10cSrcweir             cmargs2.Properties = props;
116cdf0e10cSrcweir 
117cdf0e10cSrcweir             command.Name = "open";
118cdf0e10cSrcweir             command.Argument = cmargs2;
119cdf0e10cSrcweir 
120cdf0e10cSrcweir             Object result = xCommandProcessor.execute(command, 0, null);
121cdf0e10cSrcweir             XDynamicResultSet xDynamicResultSet = UnoRuntime.queryInterface(XDynamicResultSet.class, result);
122cdf0e10cSrcweir             XResultSet xResultSet = xDynamicResultSet.getStaticResultSet();
123cdf0e10cSrcweir             XRow xRow = UnoRuntime.queryInterface(XRow.class, xResultSet);
124cdf0e10cSrcweir             // create the new folder 'folderName': first, check if it's already there
125cdf0e10cSrcweir             while(xResultSet.next()) {
126cdf0e10cSrcweir                 String existingFolderName = xRow.getString(1);
127cdf0e10cSrcweir                 System.out.println("Found existing folder: '" + existingFolderName + "'");
128cdf0e10cSrcweir                 if (folderName.equals(existingFolderName)) {
129cdf0e10cSrcweir                     fail("Cannot create a new folder: folder already exists: adapt test or choose a different document.");
130cdf0e10cSrcweir                 }
131cdf0e10cSrcweir             }
132cdf0e10cSrcweir             // create a folder
133cdf0e10cSrcweir             System.out.println("Create new folder "+ folderName);
134cdf0e10cSrcweir             ContentInfo contentInfo = new ContentInfo();
135cdf0e10cSrcweir             contentInfo.Type = "application/vnd.sun.star.tdoc-folder";
136cdf0e10cSrcweir 
137cdf0e10cSrcweir             command.Name = "createNewContent";
138cdf0e10cSrcweir             command.Argument = contentInfo;
139cdf0e10cSrcweir 
140cdf0e10cSrcweir             result = xCommandProcessor.execute(command, 0, null);
141cdf0e10cSrcweir             XContent xNewFolder = UnoRuntime.queryInterface(XContent.class, result);
142cdf0e10cSrcweir 
143cdf0e10cSrcweir             XCommandProcessor xFolderCommandProcessor = UnoRuntime.queryInterface(XCommandProcessor.class, xNewFolder);
144cdf0e10cSrcweir             System.out.println("Got the new folder: " + utils.getImplName(xNewFolder));
145cdf0e10cSrcweir 
146cdf0e10cSrcweir             // name the new folder
147cdf0e10cSrcweir             PropertyValue[] titleProp = new PropertyValue[1];
148cdf0e10cSrcweir             titleProp[0] = new PropertyValue();
149cdf0e10cSrcweir             titleProp[0].Name = "Title";
150cdf0e10cSrcweir             titleProp[0].Handle = -1;
151cdf0e10cSrcweir             titleProp[0].Value = folderName;
152cdf0e10cSrcweir             Command titleSetCommand = new Command();
153cdf0e10cSrcweir             titleSetCommand.Name = "setPropertyValues";
154cdf0e10cSrcweir             titleSetCommand.Argument = titleProp;
155cdf0e10cSrcweir             xFolderCommandProcessor.execute(titleSetCommand, 0, null);
156cdf0e10cSrcweir 
157cdf0e10cSrcweir             // 2do: check all this stuff!
158cdf0e10cSrcweir             // commit changes
159cdf0e10cSrcweir /*            InsertCommandArgument insertArgs = new InsertCommandArgument();
160cdf0e10cSrcweir             insertArgs.Data = null;
161cdf0e10cSrcweir             insertArgs.ReplaceExisting = true;
162cdf0e10cSrcweir             Command commitCommand = new Command();
163cdf0e10cSrcweir             commitCommand.Name = "insert";
164cdf0e10cSrcweir             commitCommand.Argument = insertArgs;
165cdf0e10cSrcweir             xFolderCommandProcessor.execute(commitCommand, 0, null); */
166cdf0e10cSrcweir         }
167cdf0e10cSrcweir         catch (com.sun.star.uno.Exception e) {
168cdf0e10cSrcweir             e.printStackTrace();
169cdf0e10cSrcweir             fail("Could not create test objects.");
170cdf0e10cSrcweir         }
171cdf0e10cSrcweir 
172cdf0e10cSrcweir     }
173cdf0e10cSrcweir 
174cdf0e10cSrcweir 
getMSF()175cdf0e10cSrcweir     private XMultiServiceFactory getMSF()
176cdf0e10cSrcweir     {
177cdf0e10cSrcweir         final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
178cdf0e10cSrcweir         return xMSF1;
179cdf0e10cSrcweir     }
180cdf0e10cSrcweir 
181cdf0e10cSrcweir     // setup and close connections
setUpConnection()182cdf0e10cSrcweir     @BeforeClass public static void setUpConnection() throws Exception {
183cdf0e10cSrcweir         System.out.println("setUpConnection()");
184cdf0e10cSrcweir         connection.setUp();
185cdf0e10cSrcweir     }
186cdf0e10cSrcweir 
tearDownConnection()187cdf0e10cSrcweir     @AfterClass public static void tearDownConnection()
188cdf0e10cSrcweir         throws InterruptedException, com.sun.star.uno.Exception
189cdf0e10cSrcweir     {
190cdf0e10cSrcweir         System.out.println("tearDownConnection()");
191cdf0e10cSrcweir         connection.tearDown();
192cdf0e10cSrcweir     }
193cdf0e10cSrcweir 
194cdf0e10cSrcweir     private static final OfficeConnection connection = new OfficeConnection();
195cdf0e10cSrcweir 
196cdf0e10cSrcweir 
197cdf0e10cSrcweir }
198