1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package ifc.util;
29 
30 import com.sun.star.beans.XPropertySet;
31 import com.sun.star.container.XNameReplace;
32 import com.sun.star.util.ElementChange;
33 import lib.MultiMethodTest;
34 
35 import com.sun.star.util.XChangesBatch;
36 import lib.Status;
37 import lib.StatusException;
38 
39 public class _XChangesBatch extends MultiMethodTest {
40 
41     public XChangesBatch oObj;
42     private Object changeElement = null;
43     private Object originalElement = null;
44     private String elementName = null;
45     private XPropertySet xProp = null;
46     private XNameReplace xNameReplace = null;
47 
48     /**
49      * add a change that can be committed
50      */
51     protected void before() {
52         changeElement = tEnv.getObjRelation("XChangesBatch.ChangeElement");
53         originalElement = tEnv.getObjRelation("XChangesBatch.OriginalElement");
54         elementName = (String)tEnv.getObjRelation("XChangesBatch.PropertyName");
55 
56         // to do a change, get an XPropertySet
57         xProp = (XPropertySet)tEnv.getObjRelation("XChangesBatch.PropertySet");
58         try {
59             if (originalElement == null && xProp != null)
60                 originalElement = xProp.getPropertyValue(elementName);
61         }
62         catch(com.sun.star.uno.Exception e) {
63             throw new StatusException("Could not get property '" + elementName + "'.", e);
64         }
65 
66         // or get an XNameReplace
67         xNameReplace = (XNameReplace)tEnv.getObjRelation("XChangesBatch.NameReplace");
68         try {
69             if (originalElement == null && xNameReplace != null)
70                 originalElement = xNameReplace.getByName(elementName);
71         }
72         catch(com.sun.star.uno.Exception e) {
73             throw new StatusException("Could not get element by name '" + elementName + "'.", e);
74         }
75 
76         if (changeElement == null || originalElement == null || elementName == null || (xProp == null && xNameReplace == null)) {
77             log.println(
78                 changeElement == null?"Missing property 'XChangesBatch.ChangeElement'\n":"" +
79                 originalElement == null?"Missing property 'XChangesBatch.OriginalElement'\n":"" +
80                 elementName == null?"Missing property 'XChangesBatch.PropertyName'\n":"" +
81                 xProp == null?"Missing property 'XChangesBatch.PropertySet'":"" +
82                 xNameReplace == null?"Missing property 'XChangesBatch.NameReplace'":""
83             );
84             throw new StatusException("Some needed object relations are missing.", new Exception());
85         }
86     }
87 
88     public void _commitChanges() {
89         requiredMethod("getPendingChanges()");
90         try {
91             log.println("Committing changes.");
92             oObj.commitChanges();
93         }
94         catch(com.sun.star.lang.WrappedTargetException e) {
95             tRes.tested("commitChanges()", Status.exception(e));
96             return;
97         }
98         try {
99             executeChange(originalElement);
100         }
101         catch(StatusException e) {
102             tRes.tested("hasPendingChanges()", Status.exception(e));
103             return;
104         }
105 
106         try {
107             log.println("Commit changes back.");
108             oObj.commitChanges();
109         }
110         catch(com.sun.star.lang.WrappedTargetException e) {
111             tRes.tested("commitChanges()", Status.exception(e));
112             return;
113         }
114         tRes.tested("commitChanges()", true);
115     }
116 
117     public void _getPendingChanges() {
118         requiredMethod("hasPendingChanges()");
119         ElementChange[]changes = oObj.getPendingChanges();
120         if (changes == null) {
121             log.println("Returned changes was 'null'");
122             log.println("It should have been 1 change.");
123             tRes.tested("getPendingChanges()", false);
124         } else if (changes.length != 1) {
125             int amount = changes.length;
126             log.println("Found not the right number of changes: " + amount);
127             log.println("It should have been 1 change.");
128             for (int i=0; i<amount; i++) {
129                 System.out.println("Detailed Change " + i + " -> new Element: '" +
130                             changes[i].Element.toString() + "'  ReplacedElement: '" +
131                             changes[i].ReplacedElement.toString() + "'");
132             }
133             tRes.tested("getPendingChanges()", false);
134         }
135         else {
136             boolean result = changes[0].ReplacedElement.equals(originalElement);
137             result &= changes[0].Element.equals(changeElement);
138             tRes.tested("getPendingChanges()", result);
139         }
140     }
141 
142     public void _hasPendingChanges() {
143         try {
144             executeChange(changeElement);
145         }
146         catch(StatusException e) {
147             tRes.tested("hasPendingChanges()", Status.exception(e));
148             return;
149         }
150         boolean hasPendingChanges = oObj.hasPendingChanges();
151         tRes.tested("hasPendingChanges()", hasPendingChanges);
152     }
153 
154     private void executeChange(Object element) throws StatusException {
155         if (xProp != null) {
156             try {
157                 xProp.setPropertyValue(elementName, element);
158             }
159             catch(com.sun.star.uno.Exception e) {
160                 throw new StatusException("Could not set property '" + elementName + "'.", e);
161             }
162         }
163         else if (xNameReplace != null) {
164             try {
165                 xNameReplace.replaceByName(elementName, element);
166             }
167             catch(com.sun.star.uno.Exception e) {
168                 throw new StatusException("Could not replace '" + elementName + "' by name.", e);
169             }
170         }
171     }
172 }
173