1*b1cdbd2cSJim Jagielski /************************************************************** 2*b1cdbd2cSJim Jagielski * 3*b1cdbd2cSJim Jagielski * Licensed to the Apache Software Foundation (ASF) under one 4*b1cdbd2cSJim Jagielski * or more contributor license agreements. See the NOTICE file 5*b1cdbd2cSJim Jagielski * distributed with this work for additional information 6*b1cdbd2cSJim Jagielski * regarding copyright ownership. The ASF licenses this file 7*b1cdbd2cSJim Jagielski * to you under the Apache License, Version 2.0 (the 8*b1cdbd2cSJim Jagielski * "License"); you may not use this file except in compliance 9*b1cdbd2cSJim Jagielski * with the License. You may obtain a copy of the License at 10*b1cdbd2cSJim Jagielski * 11*b1cdbd2cSJim Jagielski * http://www.apache.org/licenses/LICENSE-2.0 12*b1cdbd2cSJim Jagielski * 13*b1cdbd2cSJim Jagielski * Unless required by applicable law or agreed to in writing, 14*b1cdbd2cSJim Jagielski * software distributed under the License is distributed on an 15*b1cdbd2cSJim Jagielski * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*b1cdbd2cSJim Jagielski * KIND, either express or implied. See the License for the 17*b1cdbd2cSJim Jagielski * specific language governing permissions and limitations 18*b1cdbd2cSJim Jagielski * under the License. 19*b1cdbd2cSJim Jagielski * 20*b1cdbd2cSJim Jagielski *************************************************************/ 21*b1cdbd2cSJim Jagielski 22*b1cdbd2cSJim Jagielski 23*b1cdbd2cSJim Jagielski 24*b1cdbd2cSJim Jagielski package ifc.util; 25*b1cdbd2cSJim Jagielski 26*b1cdbd2cSJim Jagielski import com.sun.star.beans.XPropertySet; 27*b1cdbd2cSJim Jagielski import com.sun.star.container.XNameReplace; 28*b1cdbd2cSJim Jagielski import com.sun.star.util.ElementChange; 29*b1cdbd2cSJim Jagielski import lib.MultiMethodTest; 30*b1cdbd2cSJim Jagielski 31*b1cdbd2cSJim Jagielski import com.sun.star.util.XChangesBatch; 32*b1cdbd2cSJim Jagielski import lib.Status; 33*b1cdbd2cSJim Jagielski import lib.StatusException; 34*b1cdbd2cSJim Jagielski 35*b1cdbd2cSJim Jagielski public class _XChangesBatch extends MultiMethodTest { 36*b1cdbd2cSJim Jagielski 37*b1cdbd2cSJim Jagielski public XChangesBatch oObj; 38*b1cdbd2cSJim Jagielski private Object changeElement = null; 39*b1cdbd2cSJim Jagielski private Object originalElement = null; 40*b1cdbd2cSJim Jagielski private String elementName = null; 41*b1cdbd2cSJim Jagielski private XPropertySet xProp = null; 42*b1cdbd2cSJim Jagielski private XNameReplace xNameReplace = null; 43*b1cdbd2cSJim Jagielski 44*b1cdbd2cSJim Jagielski /** 45*b1cdbd2cSJim Jagielski * add a change that can be committed 46*b1cdbd2cSJim Jagielski */ before()47*b1cdbd2cSJim Jagielski protected void before() { 48*b1cdbd2cSJim Jagielski changeElement = tEnv.getObjRelation("XChangesBatch.ChangeElement"); 49*b1cdbd2cSJim Jagielski originalElement = tEnv.getObjRelation("XChangesBatch.OriginalElement"); 50*b1cdbd2cSJim Jagielski elementName = (String)tEnv.getObjRelation("XChangesBatch.PropertyName"); 51*b1cdbd2cSJim Jagielski 52*b1cdbd2cSJim Jagielski // to do a change, get an XPropertySet 53*b1cdbd2cSJim Jagielski xProp = (XPropertySet)tEnv.getObjRelation("XChangesBatch.PropertySet"); 54*b1cdbd2cSJim Jagielski try { 55*b1cdbd2cSJim Jagielski if (originalElement == null && xProp != null) 56*b1cdbd2cSJim Jagielski originalElement = xProp.getPropertyValue(elementName); 57*b1cdbd2cSJim Jagielski } 58*b1cdbd2cSJim Jagielski catch(com.sun.star.uno.Exception e) { 59*b1cdbd2cSJim Jagielski throw new StatusException("Could not get property '" + elementName + "'.", e); 60*b1cdbd2cSJim Jagielski } 61*b1cdbd2cSJim Jagielski 62*b1cdbd2cSJim Jagielski // or get an XNameReplace 63*b1cdbd2cSJim Jagielski xNameReplace = (XNameReplace)tEnv.getObjRelation("XChangesBatch.NameReplace"); 64*b1cdbd2cSJim Jagielski try { 65*b1cdbd2cSJim Jagielski if (originalElement == null && xNameReplace != null) 66*b1cdbd2cSJim Jagielski originalElement = xNameReplace.getByName(elementName); 67*b1cdbd2cSJim Jagielski } 68*b1cdbd2cSJim Jagielski catch(com.sun.star.uno.Exception e) { 69*b1cdbd2cSJim Jagielski throw new StatusException("Could not get element by name '" + elementName + "'.", e); 70*b1cdbd2cSJim Jagielski } 71*b1cdbd2cSJim Jagielski 72*b1cdbd2cSJim Jagielski if (changeElement == null || originalElement == null || elementName == null || (xProp == null && xNameReplace == null)) { 73*b1cdbd2cSJim Jagielski log.println( 74*b1cdbd2cSJim Jagielski changeElement == null?"Missing property 'XChangesBatch.ChangeElement'\n":"" + 75*b1cdbd2cSJim Jagielski originalElement == null?"Missing property 'XChangesBatch.OriginalElement'\n":"" + 76*b1cdbd2cSJim Jagielski elementName == null?"Missing property 'XChangesBatch.PropertyName'\n":"" + 77*b1cdbd2cSJim Jagielski xProp == null?"Missing property 'XChangesBatch.PropertySet'":"" + 78*b1cdbd2cSJim Jagielski xNameReplace == null?"Missing property 'XChangesBatch.NameReplace'":"" 79*b1cdbd2cSJim Jagielski ); 80*b1cdbd2cSJim Jagielski throw new StatusException("Some needed object relations are missing.", new Exception()); 81*b1cdbd2cSJim Jagielski } 82*b1cdbd2cSJim Jagielski } 83*b1cdbd2cSJim Jagielski _commitChanges()84*b1cdbd2cSJim Jagielski public void _commitChanges() { 85*b1cdbd2cSJim Jagielski requiredMethod("getPendingChanges()"); 86*b1cdbd2cSJim Jagielski try { 87*b1cdbd2cSJim Jagielski log.println("Committing changes."); 88*b1cdbd2cSJim Jagielski oObj.commitChanges(); 89*b1cdbd2cSJim Jagielski } 90*b1cdbd2cSJim Jagielski catch(com.sun.star.lang.WrappedTargetException e) { 91*b1cdbd2cSJim Jagielski tRes.tested("commitChanges()", Status.exception(e)); 92*b1cdbd2cSJim Jagielski return; 93*b1cdbd2cSJim Jagielski } 94*b1cdbd2cSJim Jagielski try { 95*b1cdbd2cSJim Jagielski executeChange(originalElement); 96*b1cdbd2cSJim Jagielski } 97*b1cdbd2cSJim Jagielski catch(StatusException e) { 98*b1cdbd2cSJim Jagielski tRes.tested("hasPendingChanges()", Status.exception(e)); 99*b1cdbd2cSJim Jagielski return; 100*b1cdbd2cSJim Jagielski } 101*b1cdbd2cSJim Jagielski 102*b1cdbd2cSJim Jagielski try { 103*b1cdbd2cSJim Jagielski log.println("Commit changes back."); 104*b1cdbd2cSJim Jagielski oObj.commitChanges(); 105*b1cdbd2cSJim Jagielski } 106*b1cdbd2cSJim Jagielski catch(com.sun.star.lang.WrappedTargetException e) { 107*b1cdbd2cSJim Jagielski tRes.tested("commitChanges()", Status.exception(e)); 108*b1cdbd2cSJim Jagielski return; 109*b1cdbd2cSJim Jagielski } 110*b1cdbd2cSJim Jagielski tRes.tested("commitChanges()", true); 111*b1cdbd2cSJim Jagielski } 112*b1cdbd2cSJim Jagielski _getPendingChanges()113*b1cdbd2cSJim Jagielski public void _getPendingChanges() { 114*b1cdbd2cSJim Jagielski requiredMethod("hasPendingChanges()"); 115*b1cdbd2cSJim Jagielski ElementChange[]changes = oObj.getPendingChanges(); 116*b1cdbd2cSJim Jagielski if (changes == null) { 117*b1cdbd2cSJim Jagielski log.println("Returned changes was 'null'"); 118*b1cdbd2cSJim Jagielski log.println("It should have been 1 change."); 119*b1cdbd2cSJim Jagielski tRes.tested("getPendingChanges()", false); 120*b1cdbd2cSJim Jagielski } else if (changes.length != 1) { 121*b1cdbd2cSJim Jagielski int amount = changes.length; 122*b1cdbd2cSJim Jagielski log.println("Found not the right number of changes: " + amount); 123*b1cdbd2cSJim Jagielski log.println("It should have been 1 change."); 124*b1cdbd2cSJim Jagielski for (int i=0; i<amount; i++) { 125*b1cdbd2cSJim Jagielski System.out.println("Detailed Change " + i + " -> new Element: '" + 126*b1cdbd2cSJim Jagielski changes[i].Element.toString() + "' ReplacedElement: '" + 127*b1cdbd2cSJim Jagielski changes[i].ReplacedElement.toString() + "'"); 128*b1cdbd2cSJim Jagielski } 129*b1cdbd2cSJim Jagielski tRes.tested("getPendingChanges()", false); 130*b1cdbd2cSJim Jagielski } 131*b1cdbd2cSJim Jagielski else { 132*b1cdbd2cSJim Jagielski boolean result = changes[0].ReplacedElement.equals(originalElement); 133*b1cdbd2cSJim Jagielski result &= changes[0].Element.equals(changeElement); 134*b1cdbd2cSJim Jagielski tRes.tested("getPendingChanges()", result); 135*b1cdbd2cSJim Jagielski } 136*b1cdbd2cSJim Jagielski } 137*b1cdbd2cSJim Jagielski _hasPendingChanges()138*b1cdbd2cSJim Jagielski public void _hasPendingChanges() { 139*b1cdbd2cSJim Jagielski try { 140*b1cdbd2cSJim Jagielski executeChange(changeElement); 141*b1cdbd2cSJim Jagielski } 142*b1cdbd2cSJim Jagielski catch(StatusException e) { 143*b1cdbd2cSJim Jagielski tRes.tested("hasPendingChanges()", Status.exception(e)); 144*b1cdbd2cSJim Jagielski return; 145*b1cdbd2cSJim Jagielski } 146*b1cdbd2cSJim Jagielski boolean hasPendingChanges = oObj.hasPendingChanges(); 147*b1cdbd2cSJim Jagielski tRes.tested("hasPendingChanges()", hasPendingChanges); 148*b1cdbd2cSJim Jagielski } 149*b1cdbd2cSJim Jagielski executeChange(Object element)150*b1cdbd2cSJim Jagielski private void executeChange(Object element) throws StatusException { 151*b1cdbd2cSJim Jagielski if (xProp != null) { 152*b1cdbd2cSJim Jagielski try { 153*b1cdbd2cSJim Jagielski xProp.setPropertyValue(elementName, element); 154*b1cdbd2cSJim Jagielski } 155*b1cdbd2cSJim Jagielski catch(com.sun.star.uno.Exception e) { 156*b1cdbd2cSJim Jagielski throw new StatusException("Could not set property '" + elementName + "'.", e); 157*b1cdbd2cSJim Jagielski } 158*b1cdbd2cSJim Jagielski } 159*b1cdbd2cSJim Jagielski else if (xNameReplace != null) { 160*b1cdbd2cSJim Jagielski try { 161*b1cdbd2cSJim Jagielski xNameReplace.replaceByName(elementName, element); 162*b1cdbd2cSJim Jagielski } 163*b1cdbd2cSJim Jagielski catch(com.sun.star.uno.Exception e) { 164*b1cdbd2cSJim Jagielski throw new StatusException("Could not replace '" + elementName + "' by name.", e); 165*b1cdbd2cSJim Jagielski } 166*b1cdbd2cSJim Jagielski } 167*b1cdbd2cSJim Jagielski } 168*b1cdbd2cSJim Jagielski } 169