001 /* 002 * JBoss DNA (http://www.jboss.org/dna) 003 * See the COPYRIGHT.txt file distributed with this work for information 004 * regarding copyright ownership. Some portions may be licensed 005 * to Red Hat, Inc. under one or more contributor license agreements. 006 * See the AUTHORS.txt file in the distribution for a full listing of 007 * individual contributors. 008 * 009 * Unless otherwise indicated, all code in JBoss DNA is licensed 010 * to you under the terms of the GNU Lesser General Public License as 011 * published by the Free Software Foundation; either version 2.1 of 012 * the License, or (at your option) any later version. 013 * 014 * JBoss DNA is distributed in the hope that it will be useful, 015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 017 * Lesser General Public License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this software; if not, write to the Free 021 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 022 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 023 */ 024 package org.jboss.dna.graph.io; 025 026 import java.util.List; 027 import net.jcip.annotations.NotThreadSafe; 028 import org.jboss.dna.graph.ExecutionContext; 029 import org.jboss.dna.graph.Graph; 030 import org.jboss.dna.graph.Graph.Batch; 031 import org.jboss.dna.graph.property.Path; 032 import org.jboss.dna.graph.property.Property; 033 034 /** 035 * A {@link Destination} that makes the changes to a graph via a {@link Batch}. 036 */ 037 @NotThreadSafe 038 public class GraphBatchDestination implements Destination { 039 protected final Graph.Batch batch; 040 protected final boolean ignoreSubmit; 041 042 /** 043 * Create a new instance that will use the specified batch. When {@link #submit()} is called, the batch will be 044 * {@link Batch#execute() executed}. 045 * 046 * @param batch the batch 047 * @throws IllegalArgumentException if the batch is null 048 */ 049 public GraphBatchDestination( Graph.Batch batch ) { 050 this(batch, false); 051 } 052 053 /** 054 * Create a new instance that will use the specified batch. If {@code ignoreSubmit} is true, then {@link #submit()} does 055 * nothing (and the batch must be executed}; otherwise, {@link #submit()} immediately calls the {@link Batch#execute() 056 * executed}. 057 * 058 * @param batch the batch 059 * @param ignoreSubmit true if the {@link #submit()} method should be ignored, or false otherwise 060 * @throws IllegalArgumentException if the batch is null 061 */ 062 public GraphBatchDestination( Graph.Batch batch, 063 boolean ignoreSubmit ) { 064 assert batch != null; 065 this.batch = batch; 066 this.ignoreSubmit = ignoreSubmit; 067 } 068 069 /** 070 * Return whether this instance is ignoring calls to {@link #submit()}. 071 * 072 * @return ignoreSubmit 073 */ 074 public boolean isSubmitIgnored() { 075 return ignoreSubmit; 076 } 077 078 /** 079 * {@inheritDoc} 080 * 081 * @see org.jboss.dna.graph.io.Destination#getExecutionContext() 082 */ 083 public ExecutionContext getExecutionContext() { 084 return batch.getGraph().getContext(); 085 } 086 087 /** 088 * {@inheritDoc} 089 * 090 * @see org.jboss.dna.graph.io.Destination#create(org.jboss.dna.graph.property.Path, java.util.List) 091 */ 092 public void create( Path path, 093 List<Property> properties ) { 094 assert properties != null; 095 if (properties.isEmpty()) { 096 batch.create(path).and(); 097 } else { 098 batch.create(path, properties).and(); 099 } 100 } 101 102 /** 103 * {@inheritDoc} 104 * 105 * @see org.jboss.dna.graph.io.Destination#create(org.jboss.dna.graph.property.Path, org.jboss.dna.graph.property.Property, 106 * org.jboss.dna.graph.property.Property[]) 107 */ 108 public void create( Path path, 109 Property firstProperty, 110 Property... additionalProperties ) { 111 if (firstProperty == null) { 112 batch.create(path).and(); 113 } else { 114 batch.create(path, firstProperty, additionalProperties).and(); 115 } 116 } 117 118 /** 119 * {@inheritDoc} 120 * 121 * @see org.jboss.dna.graph.io.Destination#setProperties(org.jboss.dna.graph.property.Path, org.jboss.dna.graph.property.Property[]) 122 */ 123 public void setProperties( Path path, 124 Property... properties ) { 125 if (properties == null) return; 126 127 batch.set(properties).on(path); 128 } 129 130 /** 131 * {@inheritDoc} 132 * 133 * @see org.jboss.dna.graph.io.Destination#submit() 134 */ 135 public void submit() { 136 // Execute only if we're not ignoring submits ... 137 if (!this.ignoreSubmit && !batch.hasExecuted()) batch.execute(); 138 139 } 140 }