
package org.fcrepo.spring;

import java.io.IOException;
import java.util.Collections;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;

import org.modeshape.jcr.JcrRepository;
import org.modeshape.jcr.JcrRepositoryFactory;
import org.modeshape.jcr.api.JcrTools;
import org.modeshape.jcr.api.Repository;
import org.modeshape.jcr.api.RepositoryFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.core.io.Resource;

public class ModeShapeRepositoryFactoryBean implements
        FactoryBean<JcrRepository> {

    @Inject
    private JcrRepositoryFactory jcrRepositoryFactory;

    private Resource repositoryConfiguration;

    private JcrRepository repository;

    @PostConstruct
    public void buildRepository() throws RepositoryException, IOException {
        repository =
                (JcrRepository) jcrRepositoryFactory.getRepository(Collections
                        .singletonMap(RepositoryFactory.URL,
                                repositoryConfiguration.getURL()));

        setupInitialNodes();
    }

    private void setupInitialNodes() throws RepositoryException {
        final Session s = repository.login();
        final Node objectStore =
                new JcrTools(true).findOrCreateNode(s, "/objects");
        final Node federatedStore = 
        		new JcrTools(true).findOrCreateNode(s, "/federated");

        if (objectStore.canAddMixin("fedora:objectStore")) {
            objectStore.addMixin("fedora:objectStore");

            if (!objectStore.hasProperty("fedora:size")) {
                objectStore.setProperty("fedora:size", 0L);
            }
        }
        
        if(federatedStore.canAddMixin("fedora:objectStore")) {
        	federatedStore.addMixin("fedora:objectStore");

            if(!federatedStore.hasProperty("fedora:size")) {
            	federatedStore.setProperty("fedora:size", 0L);
            }
        }

        s.save();
        s.logout();
    }

    @Override
    public JcrRepository getObject() throws RepositoryException, IOException {
        return repository;
    }

    @Override
    public Class<?> getObjectType() {
        return Repository.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    public void setRepositoryConfiguration(
            final Resource repositoryConfiguration) {
        this.repositoryConfiguration = repositoryConfiguration;
    }
}

@@ -236,16 +236,58 @@
 ssion;%0A%0A
+import org.fcrepo.services.ObjectService;%0A
 import o
@@ -1259,32 +1259,33 @@
 toryException %7B%0A
+%0A
         final Se
@@ -1318,337 +1318,73 @@
 ();%0A
-        final Node objectStore =%0A                new JcrTools(true).findOrCreateNode(s, %22/objects%22);%0A        final Node federatedStore = %0A        %09%09new JcrTools(true).findOrCreateNode(s, %22/federated%22);%0A%0A        if (objectStore.canAddMixin(%22fedora:objectStore%22)) %7B%0A            objectStore.addMixin(%22fedora:
+%0A        getObjectService().createObject(s, %22/
 object
-Store
+s
 %22);%0A
-%0A
         
     
@@ -1383,416 +1383,57 @@
     
-    if (!objectStore.hasProperty(%22fedora:size%22)) %7B%0A                objectStore.setProperty(%22fedora:size%22, 0L);%0A            %7D%0A        %7D%0A        %0A        if(federatedStore.canAddMixin(%22fedora:objectStore%22)) %7B%0A        %09federatedStore.addMixin(%22fedora:objectStore%22);%0A%0A            if(!federatedStore.hasProperty(%22fedora:size%22)) %7B%0A            %09federatedStore.setProperty(%22fedora:size%22, 0L);%0A            %7D%0A        %7D
+getObjectService().createObject(s, %22/federated%22);
 %0A%0A  
@@ -1938,11 +1938,204 @@
 n;%0A    %7D
+%0A%0A%09private ObjectService getObjectService() %7B%0A%0A        final ObjectService objectService = new ObjectService();%0A        objectService.setRepository(repository);%0A        return objectService;%0A%09%7D
 %0A%7D%0A

use the ObjectService to create /objects and /federated