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

     
    11package org.sophie2.base.model.resources.r4.keys; 
    22 
    33import java.io.IOException; 
     4import java.util.Collections; 
     5import java.util.List; 
    46import java.util.regex.Pattern; 
    57 
    68import org.sophie2.base.commons.util.ImmList; 
     
    257259         */ 
    258260        public abstract void persistR3(ValueRef<T> ref, Storage destination, PersistenceOptions options, String format) throws IOException; 
    259261         
     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         
    260271} 
  • modules/org.sophie2.base.model.resources.r4/src/main/java/org/sophie2/base/model/resources/r4/keys/SimpleSubKey.java

     
     1package org.sophie2.base.model.resources.r4.keys; 
     2 
     3import java.io.IOException; 
     4 
     5import org.sophie2.base.commons.util.ImmList; 
     6import org.sophie2.base.persistence.commons.PersistenceOptions; 
     7import org.sophie2.base.persistence.ref.ValueRef; 
     8import 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 */ 
     17class 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

     
    11package org.sophie2.base.model.resources.r4.keys; 
    22 
     3import java.util.LinkedList; 
     4import java.util.List; 
     5 
    36import org.sophie2.base.commons.util.ImmList; 
    47import org.sophie2.base.model.resources.r4.ResourceRefR4; 
    58import org.sophie2.base.model.resources.r4.access.ResourceAccess; 
     
    2932         
    3033        // This initialization is done after super initialization. 
    3134        private final SimpleKey<Boolean> KEY_LOCKED =  
    32                 new SimpleKey<Boolean>(getId() + LOCKED_SUFFIX, 
     35                new SimpleSubKey<Boolean>(getId() + LOCKED_SUFFIX, 
    3336                        Boolean.class, false); 
     37         
    3438        private final SimpleKey<Boolean> KEY_APPLY =  
    35                 new SimpleKey<Boolean>(getId() + APPLY_SUFFIX, 
     39                new SimpleSubKey<Boolean>(getId() + APPLY_SUFFIX, 
    3640                        Boolean.class, true); 
    3741        /** 
    3842         * Default constructor. 
     
    127131                                .getValueClass(), this.getDefault()); 
    128132        } 
    129133         
     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                 
    130143        /** 
    131144         * The possible modes for a {@link TemplatedKey}. 
    132145         */ 
  • modules/org.sophie2.base.model.resources.r4/src/main/java/org/sophie2/base/model/resources/r4/ResourceInfoProvider.java

     
    197197                        try { 
    198198                                if (field.getName().startsWith("KEY_")) { 
    199199                                        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                                        } 
    200205                                } 
    201206                        } catch (IllegalAccessException e) { 
    202207                                throw new RuntimeException("Cannot access a KEY field ("