Changeset 8792

Show
Ignore:
Timestamp:
03/10/10 20:51:09 (6 months ago)
Author:
meddle
Message:
#2352 : Working persistence on embedded books
Location:
branches/private/meddle/linkable/modules
Files:
8 modified

Legend:

Unmodified
Added
Removed
  • branches/private/meddle/linkable/modules/org.sophie2.base.model.book/src/main/java/org/sophie2/base/model/book/interfaces/ResourceFrame.java

    r8355 r8792  
    11package org.sophie2.base.model.book.interfaces; 
    22 
     3import java.io.File; 
    34import java.io.IOException; 
    45 
     6import org.sophie2.base.model.book.resource.ResourceDataPersistType; 
     7import org.sophie2.base.model.book.resource.ResourceFilesUtil; 
     8import org.sophie2.base.model.resources.r4.LocationPrefix; 
    59import org.sophie2.base.model.resources.r4.ResourceRefR4; 
    610import org.sophie2.base.model.resources.r4.keys.DeeplyCopied; 
    711import org.sophie2.base.model.resources.r4.keys.TemplatedResourceRefKey; 
    812import org.sophie2.base.persistence.commons.PersistenceOptions; 
     13import org.sophie2.base.persistence.commons.PersistenceUtil; 
    914import org.sophie2.base.persistence.ref.ValueRef; 
    1015import org.sophie2.base.persistence.storage.Storage; 
     
    3338                                res = destination.getChild("content"); 
    3439                        } 
     40                         
     41                        String parentLocation = options.get(PersistenceUtil.LOCATION); 
     42                         
     43                        if (options.isSaveMode()) { 
     44                                ResourceRefR4 original = ref.get(); 
     45                                 
     46                                ResourceDataPersistType type =  
     47                                        ResourceDataPersistType.getPersistType(original, parentLocation); 
     48                                if (type == ResourceDataPersistType.LINKED_RELATIVE) { 
     49                                        ref.set(ResourceFilesUtil.getSaveableRef(parentLocation, ref.get())); 
     50                                        ResourceFilesUtil.saveLoadableResource(parentLocation, 
     51                                                        ref.get().getLocation(),  
     52                                                        new File(original.getLocation().substring(LocationPrefix.FILE.length()))); 
     53                                }  
     54                                 
     55                        } 
     56                         
    3557                        super.persistR3(ref, res, options, format); 
     58                         
     59                        if (options.isLoadMode()) { 
     60                                ResourceDataPersistType type =  
     61                                        ResourceDataPersistType.getPersistType(ref.get(), parentLocation); 
     62                                if (type == ResourceDataPersistType.LINKED_RELATIVE) { 
     63                                        ref.set(ResourceFilesUtil.getLoadableRef(parentLocation, ref.get())); 
     64                                } 
     65                        } 
    3666                } 
    3767        }; 
  • branches/private/meddle/linkable/modules/org.sophie2.base.model.book/src/main/java/org/sophie2/base/model/book/resource/ResourceDataPersistType.java

    r8787 r8792  
    8787                 
    8888                if (ref.isRelative()) { 
     89                        if (ref.getLocation().contains(ResourceFilesUtil.SAVEABLE_TEMPLATE)) { 
     90                                return LINKED_RELATIVE; 
     91                        } 
     92                         
    8993                        return EMBEDDED; 
    9094                } 
     
    9296                if (ref.isAbsolute()) { 
    9397                        if (parentLocation != null) { 
    94                                 if (ResourceFilesUtil.isRelativeToParent(ref, parentLocation)) { 
     98                                if (ResourceFilesUtil.isRelativeToParent(ref, parentLocation) 
     99                                                || ref.getLocation().contains(ResourceFilesUtil.FILES_DIRECTORY)) { 
    95100                                        return LINKED_RELATIVE; 
    96101                                } 
  • branches/private/meddle/linkable/modules/org.sophie2.base.model.book/src/main/java/org/sophie2/base/model/book/resource/ResourceFilesUtil.java

    r8787 r8792  
    3131        public static final String FILES_DIRECTORY = "_files"; 
    3232 
    33         private static final String SAVEABLE_TEMPLATE = "${files_dir}"; 
     33        public static final String SAVEABLE_TEMPLATE = "${files_dir}"; 
    3434 
    3535        /** 
     
    113113        } 
    114114 
     115        /** 
     116         * Checks if the passed {@link ResourceRefR4} points to a resource in the directory, 
     117         * relative to the passed <var>parentLocation</var> 
     118         *  
     119         * @param ref 
     120         *                      The ref to check. 
     121         * @param parentLocation 
     122         *                      The parent location to check. 
     123         * @return 
     124         *                      True if the ref is relative. 
     125         */ 
    115126        public static boolean isRelativeToParent(ResourceRefR4 ref, String parentLocation) { 
    116127                File parentDir = ResourceFilesUtil.getDir(parentLocation); 
     
    214225                return relative.getLocation().replace(dirName, SAVEABLE_TEMPLATE); 
    215226        } 
     227         
     228        public static ResourceRefR4 getSaveableRef(String parentPath, ResourceRefR4 ref) { 
     229                if (parentPath == null) { 
     230                        return ref; 
     231                } 
     232 
     233                int lastSeparator = parentPath.lastIndexOf(ResourceRefR4.SEPARATOR); 
     234                String dirName = getDirName(parentPath, lastSeparator);  
     235 
     236                ResourceRefR4 targetRef = ref; 
     237                if (!targetRef.getLocation().contains(dirName)) { 
     238                        targetRef = ResourceRefR4.make(new File(getDir(parentPath), ref.getName()));  
     239                } 
     240 
     241                ResourceRefR4 relative = ResourceRefR4.getRelativeRef( 
     242                                ResourceRefR4.make(parentPath), targetRef); 
     243 
     244 
     245                return ResourceRefR4.make( 
     246                                relative.getLocation().replace(dirName, SAVEABLE_TEMPLATE)); 
     247        } 
    216248 
    217249        /** 
     
    240272        } 
    241273 
     274        public static ResourceRefR4 getLoadableRef(String parentPath, ResourceRefR4 loadedRef) { 
     275                if (parentPath == null || !loadedRef.getLocation().contains(SAVEABLE_TEMPLATE)) { 
     276                        return loadedRef; 
     277                } 
     278 
     279                ResourceRefR4 absolute = ResourceRefR4.make( 
     280                                parentPath).append(loadedRef); 
     281 
     282                int lastSeparator = parentPath.lastIndexOf(ResourceRefR4.SEPARATOR); 
     283 
     284                return ResourceRefR4.make(absolute.getLocation().replace( 
     285                                SAVEABLE_TEMPLATE, getDirName(parentPath, lastSeparator))); 
     286        } 
     287         
    242288        /** 
    243289         * Saves a resource file in such a way it can be loaded in the even 
     
    268314                        } 
    269315 
    270                         copy(original, target); 
     316                        copy(original, target, false); 
    271317                } 
    272318        } 
     
    290336                assert original != null && target != null; 
    291337 
    292                 copy(original, target); 
    293         } 
    294  
    295  
    296         private static void copy(File original, File target) { 
     338                String tmpDir = System.getProperty("java.io.tmpdir"); 
     339                boolean delOnExit = target.getAbsolutePath().startsWith(tmpDir); 
     340                copy(original, target, delOnExit); 
     341        } 
     342 
     343 
     344        private static void copy(File original, File target, boolean deleteOnExit) { 
    297345                if (!original.exists()) { 
    298346                        return; 
     
    305353 
    306354                        for (File file : original.listFiles()) { 
    307                                 copy(file, new File(target, file.getName())); 
     355                                copy(file, new File(target, file.getName()), deleteOnExit); 
    308356                        } 
    309357                } else { 
     
    317365                        try { 
    318366                                BinData.transport(new FileInputStream(original), new FileOutputStream(target)); 
     367                                if (deleteOnExit) { 
     368                                        target.deleteOnExit(); 
     369                                } 
    319370                        } catch (FileNotFoundException e) { 
    320371                                throw new RuntimeException(e); 
     
    412463        } 
    413464 
    414         public static void persistRefData(final ValueRef<ResourceRefR4> ref, Storage storage, 
    415                         final PersistenceOptions options, String format) throws IOException { 
    416                 if (options.isSaveMode() && ref.get() == null) { 
    417                         return; 
    418                 } 
    419  
    420                 String parentLocation = options.get(PersistenceUtil.LOCATION); 
    421  
    422                 ValueRef<ResourceDataPersistType> enumRef = new ValueRef<ResourceDataPersistType>(); 
    423                 if (options.isSaveMode()) { 
    424                         enumRef.set(ResourceDataPersistType.getPersistType(ref.get(), parentLocation)); 
    425                 } 
    426  
    427                 if (options.isLoadMode() && storage.getChild("dataType") == null) { 
    428                         storage.child("dataType").setTextContent( 
    429                                         ResourceDataPersistType.getPersistType(ref.get(), parentLocation).name()); 
    430                 } 
    431  
    432                 MasterPersister.persist(enumRef, storage.child("dataType"), 
    433                                 options, PersistenceUtil.getStorageR3Schema(ResourceDataPersistType.class)); 
    434  
     465        public static boolean isInSaveableFormat(String location) { 
     466                return location.startsWith(SAVEABLE_TEMPLATE); 
    435467        } 
    436468 
  • branches/private/meddle/linkable/modules/org.sophie2.base.model.book/src/test/java/org/sophie2/base/model/book/resource/r4/ResourceFilesUtilTest.java

    r8787 r8792  
    7979         * @throws Exception 
    8080         */ 
     81        @Test 
    8182        public void testIsRelativeToParent() throws Exception { 
    8283                String parentLocation = "file:///home/meddle/stuff.book.s2"; 
     
    9091        } 
    9192         
     93        /** 
     94         * Tests the {@link ResourceFilesUtil#getSaveableRef(String, ResourceRefR4)} 
     95         * method. 
     96         *  
     97         * @throws Exception 
     98         */ 
     99        @Test 
     100        public void testGetSaveableRef() throws Exception { 
     101                String parentLocation = "file:///home/meddle/stuff.book.s2"; 
     102                ResourceRefR4 ref = ResourceRefR4.make(new File("/home/meddle/stuff_book_s2_files/stuff.dat")); 
     103                 
     104                ResourceRefR4 target =  
     105                        ResourceFilesUtil.getSaveableRef(parentLocation, ref); 
     106                 
     107                assertEquals(ResourceRefR4.make( 
     108                                ResourceRefR4.PARENT + 
     109                                ResourceRefR4.SEPARATOR + 
     110                                ResourceFilesUtil.SAVEABLE_TEMPLATE + 
     111                                ResourceRefR4.SEPARATOR + 
     112                                "stuff.dat"), target); 
     113        } 
     114         
     115        /** 
     116         * Tests the {@link ResourceFilesUtil#getLoadableRef(String, ResourceRefR4)} 
     117         * method. 
     118         *  
     119         * @throws Exception 
     120         */ 
     121        @Test 
     122        public void testGetLoadableRef() throws Exception { 
     123                String parentLocation = "file:///home/meddle/stuff.book.s2"; 
     124                ResourceRefR4 ref = ResourceRefR4.make(ResourceRefR4.PARENT + 
     125                                ResourceRefR4.SEPARATOR + 
     126                                ResourceFilesUtil.SAVEABLE_TEMPLATE + 
     127                                ResourceRefR4.SEPARATOR + "stuff.dat"); 
     128                 
     129                ResourceRefR4 target =  
     130                        ResourceFilesUtil.getLoadableRef(parentLocation, ref); 
     131                 
     132                assertEquals(ResourceRefR4.make( 
     133                                new File("/home/meddle/stuff_book_s2_files/stuff.dat")), target); 
     134        } 
     135         
    92136} 
  • branches/private/meddle/linkable/modules/org.sophie2.base.model.resources.r4/src/main/java/org/sophie2/base/model/resources/r4/file/FileAccessUtil.java

    r8777 r8792  
    33import java.io.ByteArrayInputStream; 
    44import java.io.File; 
    5 import java.io.FileNotFoundException; 
    65import java.io.FileOutputStream; 
    76import java.io.IOException; 
  • branches/private/meddle/linkable/modules/org.sophie2.base.persistence/src/main/java/org/sophie2/base/persistence/commons/PersistenceUtil.java

    r8078 r8792  
    4141                        String.class, null); 
    4242 
     43        /** 
     44         * Key that provides the previous location of the persistence. 
     45         */ 
     46        public static final PersistKey<String> OLD_LOCATION = new PersistKey<String>("old-location", 
     47                        String.class, null); 
     48         
    4349        /** 
    4450         * Gets the string identifier of the kind for persistence of the given 
  • branches/private/meddle/linkable/modules/org.sophie2.extra.func.annotations/src/main/java/org/sophie2/extra/func/annotations/logic/AnnotationsLogic.java

    r8144 r8792  
    151151                        } catch (IOException e) { 
    152152                                DialogUtils.showExceptionDialog(source.swingComponent().get(), 
    153                                                                                         e, "Couldn`t Export Anotation Set"); 
     153                                                                                        e, "Couldn't Export Anotation Set"); 
    154154                                fileAccess.close(); 
    155155                                return false; 
  • branches/private/meddle/linkable/modules/org.sophie2.main.func.resources/src/main/java/org/sophie2/main/func/resources/imports/ResourceImportInfo.java

    r8780 r8792  
    8787        } 
    8888         
     89        /** 
     90         * Getter of the model {@link ResourceImportType}. 
     91         *  
     92         * @return 
     93         *                      The <var>modelImportType</var>. 
     94         */ 
    8995        public ResourceImportType getModelImportType() { 
    9096                return this.modelImportType; 
    9197        } 
    9298         
     99        /** 
     100         * Returns true if the model is {@link ResourceImportType#LINK}ed 
     101         * or {@link ResourceImportType#COPY_AND_LINK}ed. 
     102         *  
     103         * @see ResourceImportInfo#getModelImportType() 
     104         *  
     105         * @return 
     106         *                      True if the model is linked. 
     107         */ 
    93108        public boolean isModelLinked() { 
    94109                return this.modelImportType == ResourceImportType.LINK