Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Austrian Association for Software Tool Integration (AASTI) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The AASTI licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 
 package org.openengsb.core.persistence.internal;
 
 import java.io.File;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.UUID;
 
 
 
 public class DefaultPersistenceService implements PersistenceService {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(DefaultPersistenceService.class);
 
     private final File storageLocation;
     private final PersistenceIndex index;
     private final ObjectPersistenceBackend persitenceBackend;
 
     public DefaultPersistenceService(File storageLocationObjectPersistenceBackend persitenceBackend,
             PersistenceIndex index) {
         this. = storageLocation;
         this. = persitenceBackend;
         this. = index;
     }
 
     @Override
     public void create(Object beanthrows PersistenceException {
         synchronized () {
             indexBean(bean);
             .updateIndex();
         }
     }
 
     @Override
     public void create(List<? extends Objectbeansthrows PersistenceException {
         synchronized () {
             for (Object bean : beans) {
                 indexBean(bean);
             }
             .updateIndex();
         }
     }
 
     private void indexBean(Object bean) {
         File objectFile = getNewObjectFile();
         .writeDatabaseObject(beanobjectFile);
         .indexObject(bean.getClass(), objectFile);
     }
 
     @SuppressWarnings("unchecked")
     @Override
     public <TYPE> List<TYPE> query(TYPE example) {
         List<ObjectInfoobjects = .findIndexObject(example.getClass());
         List<TYPE> finalResult = Lists.newArrayList();
         for (ObjectInfo info : objects) {
             Object databaseObject = .readDatabaseObject(new File(info.getLocation()));
             if (databaseObject == null) {
                 continue;
             }
             if (evaluateObject(exampledatabaseObject)) {
                 finalResult.add((TYPE) databaseObject);
             }
         }
         .debug("Reduced {} objects to final result of size {}"objects.size(), finalResult.size());
         return finalResult;
     }
 
     private boolean evaluateObject(Object exampleObject databaseObject) {
         return example.equals(databaseObject);
     }
 
     @Override
     public <TYPE> List<TYPE> query(List<TYPE> examples) {
         List<TYPE> allResults = Lists.newArrayList();
         for (TYPE example : examples) {
             allResults.addAll(query(example));
         }
        return allResults;
    }
    @Override
    public <TYPE> void update(TYPE oldBean, TYPE newBeanthrows PersistenceException {
        updateObject(oldBeannewBean);
        .updateIndex();
    }
    private <TYPE> void updateObject(TYPE oldBean, TYPE newBeanthrows PersistenceException {
        List<ObjectInfoobjects = .findIndexObject(oldBean.getClass());
        List<ObjectInfotoRemove = Lists.newArrayList();
        for (ObjectInfo info : objects) {
            Object databaseObject = .readDatabaseObject(new File(info.getLocation()));
            if (evaluateObject(oldBeandatabaseObject)) {
                .info("Found object matching all equals of old bean {}"oldBean.getClass());
                toRemove.add(info);
            }
        }
        if (toRemove.size() != 1) {
            throw new PersistenceException("No unique object to remove available.");
        }
        for (ObjectInfo info : toRemove) {
            .removeIndexObject(info);
            new File(info.getLocation()).delete();
            indexBean(newBean);
        }
    }
    @Override
    public <TYPE> void update(Map<TYPE, TYPE> beansthrows PersistenceException {
        Set<TYPE> keySet = beans.keySet();
        for (TYPE key : keySet) {
            TYPE value = beans.get(key);
            updateObject(keyvalue);
        }
        .updateIndex();
    }
    @Override
    public <TYPE> void delete(TYPE examplethrows PersistenceException {
        deleteObject(example);
        .updateIndex();
    }
    @Override
    public <TYPE> void delete(List<? extends TYPE> examplesthrows PersistenceException {
        for (TYPE example : examples) {
            deleteObject(example);
        }
        .updateIndex();
    }
    private <TYPE> void deleteObject(TYPE example) {
        List<ObjectInfoobjects = .findIndexObject(example.getClass());
        boolean foundObjectToDelete = false;
        for (ObjectInfo info : objects) {
            Object databaseObject = .readDatabaseObject(new File(info.getLocation()));
            if (evaluateObject(exampledatabaseObject)) {
                .info("Found object matching all equals of bean to remvoe{}"example.getClass());
                .removeIndexObject(info);
                new File(info.getLocation()).delete();
                foundObjectToDelete = true;
            }
        }
        if (!foundObjectToDelete) {
            throw new PersistenceException("No element could be found to be deleted");
        }
    }
    private File getNewObjectFile() {
        return new File( + "/" + UUID.randomUUID() + ".ser");
    }
New to GrepCode? Check out our FAQ X