Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.nakedobjects.plugins.xml.objectstore;
  
  import java.util.List;
  
 
 
 public class XmlObjectStore implements ObjectStore {
 	
 	private static final Logger LOG = Logger.getLogger(XmlObjectStore.class);
     private static final String XMLOS_DIR = . + "xmlos.dir";
     private final DataManager dataManager;
     private final ServiceManager serviceManager;
     private boolean isFixturesInstalled;
 
     public XmlObjectStore(NakedObjectConfiguration configuration) {
         String directory = configuration.getString("xml/objects");
         final XmlFile xmlFile = new XmlFile(configurationdirectory);
          = new XmlDataManager(xmlFile);
          = new XmlServiceManager(xmlFile);
         .loadServices();
     }
 
 
     public XmlObjectStore(final DataManager dataManagerfinal ServiceManager serviceManager) {
         this. = dataManager;
         this. = serviceManager;
         serviceManager.loadServices();
     }
 
     
     ///////////////////////////////////////////////////////////
     // name
     ///////////////////////////////////////////////////////////
 
     public String name() {
         return "XML";
     }
 
     ///////////////////////////////////////////////////////////
     // close
     ///////////////////////////////////////////////////////////
 
     public void close() {
         .info("close " + this);
     }
 
     ///////////////////////////////////////////////////////////
     // reset
     ///////////////////////////////////////////////////////////
 
     public void reset() {}
 
     ///////////////////////////////////////////////////////////
    // init, shutdown, finalize
    ///////////////////////////////////////////////////////////
    
    public boolean hasInstances(final NakedObjectSpecification cls) {
        .debug("checking instance of " + cls);
        final ObjectData data = new ObjectData(clsnullnull);
        return .numberOfInstances(data) > 0;
    }
    public void open() throws ObjectPersistenceException {
    }
    public boolean isFixturesInstalled() {
        return ;
    }
    private void initObject(final NakedObject objectfinal ObjectData data) {
        if (object.getResolveState().canChangeTo(.)) {
            PersistorUtil.start(object.);
            final NakedObjectAssociation[] fields = object.getSpecification().getAssociations();
            for (int i = 0; i < fields.lengthi++) {
                final NakedObjectAssociation field = fields[i];
                if (!field.isNotDerived()) {
                    continue;
                }
                final NakedObjectSpecification fieldSpecification = field.getSpecification();
                if (fieldSpecification.isEncodeable()) {
                    final EncodableFacet encoder = fieldSpecification.getFacet(EncodableFacet.class);
                    NakedObject value;
                    final String valueData = data.value(field.getId());
                    if (valueData == null || valueData.equals("NULL")) {
                        value = null;
                    } else {
                        value = encoder.fromEncodedString(valueData);
                    }
                    ((OneToOneAssociationfield).initAssociation(objectvalue);
                } else if (field.isOneToManyAssociation()) {
                    initObjectSetupCollection(objectdatafield);
                } else if (field.isOneToOneAssociation()) {
                    initObjectSetupReference(objectdatafield);
                }
            }
            object.setOptimisticLock(data.getVersion());
            PersistorUtil.end(object);
        }
    }
    private void initObjectSetupReference(final NakedObject objectfinal ObjectData datafinal NakedObjectAssociation field) {
        final SerialOid referenceOid = (SerialOiddata.get(field.getId());
        .debug("setting up field " + field + " with " + referenceOid);
        if (referenceOid == null) {
            return;
        }
        final Data fieldData = .loadData(referenceOid);
        if (fieldData == null) {
            final NakedObject adapter = getPersistenceSession().recreateAdapter(referenceOidfield.getSpecification());
            if (!adapter.getResolveState().isDestroyed()) {
                adapter.changeState(.);
            }
            ((OneToOneAssociationfield).initAssociation(objectadapter);
            .warn("No data found for " + referenceOid + " so field '" + field.getName() + "' not set in object '"
                    + object.titleString() + "'");
        } else {
            final NakedObject reference = getPersistenceSession().recreateAdapter(referenceOidspecFor(fieldData));
            ((OneToOneAssociationfield).initAssociation(objectreference);
        }
        /*
         * if (loadedObjects().isLoaded(referenceOid)) { NakedObject loadedObject =
         * loadedObjects().getLoadedObject(referenceOid); LOG.debug("using loaded object " + loadedObject);
         * object.initAssociation((OneToOneAssociation) field, loadedObject); } else { NakedObject
         * fieldObject; Data fieldData = (Data) dataManager.loadData((SerialOid) referenceOid);
         * 
         * if (fieldData != null) { fieldObject = (NakedObject) specFor(fieldData).acquireInstance(); } else {
         * fieldObject = (NakedObject) field.getSpecification().acquireInstance(); }
         * 
         * fieldObject.setOid(referenceOid);
         * 
         * if (fieldObject instanceof NakedCollection) { fieldObject.setResolved(); }
         * 
         * loadedObjects().loaded(fieldObject); object.initAssociation((OneToOneAssociation) field,
         * fieldObject); }
         */
    }
    private void initObjectSetupCollection(final NakedObject objectfinal ObjectData datafinal NakedObjectAssociation field) {
        /*
         * The internal collection is already a part of the object, and therefore cannot be recreated, but its
         * oid must be set
         */
        final ReferenceVector refs = (ReferenceVectordata.get(field.getId());
        final NakedObject collection = field.get(object);
        if (collection.getResolveState().canChangeTo(.)) {
            PersistorUtil.start(collection.);
            final int size = refs == null ? 0 : refs.size();
            final NakedObject[] elements = new NakedObject[size];
            for (int j = 0; j < sizej++) {
                final SerialOid elementOid = refs.elementAt(j);
                NakedObject adapter;
                adapter = getAdapterManager().getAdapterFor(elementOid);
                if (adapter == null) {
                    adapter = getObject(elementOidnull);
                }
                elements[j] = adapter;
            }
            final CollectionFacet facet = CollectionFacetUtils.getCollectionFacetFromSpec(collection);
            facet.init(collectionelements);
            PersistorUtil.end(collection);
        }
    }
    ///////////////////////////////////////////////////////////
    // Transaction Management
    ///////////////////////////////////////////////////////////
    public void startTransaction() {
        .debug("start transaction");
    }
    public void endTransaction() {
        .debug("end transaction");
    }
    public void abortTransaction() {
        .debug("transaction aborted");
    }
    ///////////////////////////////////////////////////////////
    // createXxxCommands
    ///////////////////////////////////////////////////////////
        return new XmlCreateObjectCommand(object);
    }
    public SaveObjectCommand createSaveObjectCommand(final NakedObject object) {
        return new XmlUpdateObjectCommand(object);
    }
        return new XmlDestroyObjectCommand(object);
    }
    ///////////////////////////////////////////////////////////
    // execute, flush
    ///////////////////////////////////////////////////////////
    public void execute(final List<PersistenceCommandcommands) {
        .debug("start execution of transaction");
        for (PersistenceCommand commandcommands) {
            command.execute(null);
        }
        .debug("end execution");
    }
    ///////////////////////////////////////////////////////////
    // getObject, resolveImmediately, resolveField
    ///////////////////////////////////////////////////////////
    public NakedObject getObject(final Oid oidfinal NakedObjectSpecification hint) {
        .debug("getObject " + oid);
        final Data data = .loadData((SerialOidoid);
        .debug("  data read " + data);
        NakedObject object;
        if (data instanceof ObjectData) {
            object = recreateObject((ObjectDatadata);
        } else if (data instanceof CollectionData) {
            throw new NakedObjectException();
        } else {
            throw new ObjectNotFoundException(oid);
        }
        return object;
    }
    public void resolveField(final NakedObject objectfinal NakedObjectAssociation field) {
        final NakedObject reference = field.get(object);
        resolveImmediately(reference);
    }
    public void resolveImmediately(final NakedObject object) {
        final ObjectData data = (ObjectData.loadData((SerialOidobject.getOid());
        Assert.assertNotNull("Not able to read in data during resolve"objectdata);
        initObject(objectdata);
    }
    /*
     * The ObjectData holds all references for internal collections, so the object should haves its internal
     * collection populated by this method.
     */
    private NakedObject recreateObject(final ObjectData data) {
        final SerialOid oid = data.getOid();
        final NakedObjectSpecification spec = specFor(data);
        final NakedObject object = getPersistenceSession().recreateAdapter(oidspec);
        initObject(objectdata);
        return object;
    }
    ///////////////////////////////////////////////////////////
    // getInstances, allInstances
    ///////////////////////////////////////////////////////////
    public NakedObject[] getInstances(final PersistenceQuery persistenceQuery) {
    	
    	if (!(persistenceQuery instanceof PersistenceQueryBuiltIn)) {
    		throw new IllegalArgumentException(MessageFormat.format(
							"Provided PersistenceQuery not supported; was {0}; " +
							"the XML object store only supports {1}",
							persistenceQuery.getClass().getName(), 
    	}
		PersistenceQueryBuiltIn builtIn = (PersistenceQueryBuiltInpersistenceQuery;
    	
        .debug("getInstances of " + builtIn.getSpecification() + " where " + builtIn);
        final ObjectData patternData = new ObjectData(builtIn.getSpecification(), nullnull);
        final NakedObject[] instances = getInstances(patternDatabuiltIn);
        return instances;
    }
    private NakedObject[] getInstances(
    		final ObjectData patternData
    		final PersistenceQueryBuiltIn persistenceQuery) {
        final ObjectDataVector data = .getInstances(patternData);
        final NakedObject[] instances = new NakedObject[data.size()];
        int count = 0;
        for (int i = 0; i < data.size(); i++) {
            final ObjectData instanceData = data.element(i);
            .debug("instance data " + instanceData);
            final SerialOid oid = instanceData.getOid();
            final NakedObjectSpecification spec = specFor(instanceData);
            final NakedObject instance = getPersistenceSession().recreateAdapter(oidspec);
            initObject(instanceinstanceData);
            if (persistenceQuery == null || 
            	persistenceQuery.matches(instance)) {
                instances[count++] = instance;
            }
        }
        final NakedObject[] array = new NakedObject[count];
        System.arraycopy(instances, 0, array, 0, count);
        return array;
    }
    private NakedObjectSpecification specFor(final Data data) {
        return getSpecificationLoader().loadSpecification(data.getTypeName());
    }
    ///////////////////////////////////////////////////////////
    // services
    ///////////////////////////////////////////////////////////
    public Oid getOidForService(final String name) {
        return .getOidForService(name);
    }
    public void registerService(final String namefinal Oid oid) {
        .registerService(nameoid);
    }
    ///////////////////////////////////////////////////////////
    // debugging
    ///////////////////////////////////////////////////////////
    public void debugData(final DebugString debug) {
        debug.appendTitle("Business Objects");
        debug.appendln(.getDebugData());
    }
    public String debugTitle() {
        return "XML Object Store";
    }
    ///////////////////////////////////////////////////////////
    // Dependencies (injected)
    ///////////////////////////////////////////////////////////

    
Set the clock used to generate sequence numbers and last changed dates for version objects.
    public void setClock(final Clock clock) {
        FileVersion.setClock(clock);
    }
    ///////////////////////////////////////////////////////////
    // Dependencies (from singleton)
    ///////////////////////////////////////////////////////////
    protected static SpecificationLoader getSpecificationLoader() {
        return NakedObjectsContext.getSpecificationLoader();
    }
    private static AdapterManager getAdapterManager() {
        return getPersistenceSession().getAdapterManager();
    }
		return NakedObjectsContext.getPersistenceSession();
	}
// Copyright (c) Naked Objects Group Ltd.
New to GrepCode? Check out our FAQ X