Ticket #2383: templatespersistence.patch
File templatespersistence.patch, 5.9 KB (added by boyanl, 15 years ago) |
---|
-
modules/org.sophie2.base.model.resources.r4/src/main/java/org/sophie2/base/model/resources/r4/keys/Key.java
1 1 package org.sophie2.base.model.resources.r4.keys; 2 2 3 3 import java.io.IOException; 4 import java.util.Collections; 5 import java.util.List; 4 6 import java.util.regex.Pattern; 5 7 6 8 import org.sophie2.base.commons.util.ImmList; … … 257 259 */ 258 260 public abstract void persistR3(ValueRef<T> ref, Storage destination, PersistenceOptions options, String format) throws IOException; 259 261 262 /** 263 * Gets a list of sub-keys that should be persisted along with this one. 264 * @return 265 * A list of sub-keys. 266 */ 267 public List<Key<?>> getSubkeys () { 268 return Collections.<Key<?>>emptyList(); 269 } 270 260 271 } -
modules/org.sophie2.base.model.resources.r4/src/main/java/org/sophie2/base/model/resources/r4/keys/SimpleSubKey.java
1 package org.sophie2.base.model.resources.r4.keys; 2 3 import java.io.IOException; 4 5 import org.sophie2.base.commons.util.ImmList; 6 import org.sophie2.base.persistence.commons.PersistenceOptions; 7 import org.sophie2.base.persistence.ref.ValueRef; 8 import org.sophie2.base.persistence.storage.Storage; 9 10 /** 11 * A key type which is a sub-key of another key 12 * 13 * @author boyanl 14 * 15 * @param <T> 16 */ 17 class SimpleSubKey<T> extends SimpleKey<T> { 18 19 /** 20 * Default constructor. 21 * 22 * @param id 23 * The string representation of the key. 24 * @param valueClass 25 * The type of the value this key holds. 26 * @param defValue 27 * Default value for the new key. 28 */ 29 public SimpleSubKey(String id, Class<T> valueClass, T defValue) { 30 super(id, valueClass, defValue); 31 } 32 33 /** 34 * Constructor with given parts as {@link ImmList}. 35 * 36 * @param parts 37 * The parts to construct the Id from. 38 * @param valueClass 39 * The type of the value this key holds. 40 * @param defValue 41 * Default value for the new key. 42 */ 43 SimpleSubKey(ImmList<String> parts, Class<T> valueClass, T defValue) { 44 super(parts, valueClass, defValue); 45 } 46 47 /** 48 * Persist the key in a way so that we'll avoid collisions with same keys but in 49 * different parents. 50 */ 51 @Override 52 public void persistR3(ValueRef<T> ref, Storage destination, 53 PersistenceOptions options, String format) throws IOException { 54 ImmList<String> parts = this.getParts (); 55 56 parts = parts.remove(parts.size() - 1); 57 58 Storage finalDest = destination; 59 for (String str : parts) { 60 Storage child = (options.isLoadMode() ? finalDest.getChild(str) : finalDest.child(str)); 61 if (options.isLoadMode() && child == null) { 62 ref.set(null); 63 return; 64 } 65 finalDest = child; 66 } 67 68 super.persistR3(ref, finalDest, options, format); 69 } 70 } 71 No newline at end of file -
modules/org.sophie2.base.model.resources.r4/src/main/java/org/sophie2/base/model/resources/r4/keys/TemplatedKey.java
1 1 package org.sophie2.base.model.resources.r4.keys; 2 2 3 import java.util.LinkedList; 4 import java.util.List; 5 3 6 import org.sophie2.base.commons.util.ImmList; 4 7 import org.sophie2.base.model.resources.r4.ResourceRefR4; 5 8 import org.sophie2.base.model.resources.r4.access.ResourceAccess; … … 29 32 30 33 // This initialization is done after super initialization. 31 34 private final SimpleKey<Boolean> KEY_LOCKED = 32 new Simple Key<Boolean>(getId() + LOCKED_SUFFIX,35 new SimpleSubKey<Boolean>(getId() + LOCKED_SUFFIX, 33 36 Boolean.class, false); 37 34 38 private final SimpleKey<Boolean> KEY_APPLY = 35 new Simple Key<Boolean>(getId() + APPLY_SUFFIX,39 new SimpleSubKey<Boolean>(getId() + APPLY_SUFFIX, 36 40 Boolean.class, true); 37 41 /** 38 42 * Default constructor. … … 127 131 .getValueClass(), this.getDefault()); 128 132 } 129 133 134 @Override 135 public List<Key<?>> getSubkeys () { 136 List<Key<?>> result = new LinkedList<Key<?>>(); 137 result.add(this.KEY_APPLY); 138 result.add(this.KEY_LOCKED); 139 140 return result; 141 } 142 130 143 /** 131 144 * The possible modes for a {@link TemplatedKey}. 132 145 */ -
modules/org.sophie2.base.model.resources.r4/src/main/java/org/sophie2/base/model/resources/r4/ResourceInfoProvider.java
197 197 try { 198 198 if (field.getName().startsWith("KEY_")) { 199 199 res.put(field.getName(), new KeyEntry(field)); 200 201 Key<?> key = (Key<?>) field.get(null); 202 for (Key<?> subKey : key.getSubkeys()) { 203 res.put(subKey.getId(), new KeyEntry(subKey, field)); 204 } 200 205 } 201 206 } catch (IllegalAccessException e) { 202 207 throw new RuntimeException("Cannot access a KEY field ("