/*
 * Copyright 2008-2010 Microarray Informatics Team, EMBL-European Bioinformatics Institute
 *
 * Licensed 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.
 *
 *
 * For further details of the Gene Expression Atlas project, including source code,
 * downloads and documentation, please see:
 *
 * http://gxa.github.com/gxa
 */

package uk.ac.ebi.gxa.loader.handler.sdrf;

import uk.ac.ebi.arrayexpress2.magetab.datamodel.sdrf.node.AssayNode;
import uk.ac.ebi.arrayexpress2.magetab.datamodel.sdrf.node.HybridizationNode;
import uk.ac.ebi.arrayexpress2.magetab.exception.ObjectConversionException;
import uk.ac.ebi.arrayexpress2.magetab.handler.sdrf.node.FactorValueNodeHandler;
import uk.ac.ebi.gxa.loader.cache.AtlasLoadCache;
import uk.ac.ebi.gxa.loader.cache.AtlasLoadCacheRegistry;
import uk.ac.ebi.gxa.loader.utils.SDRFWritingUtils;
import uk.ac.ebi.microarray.atlas.model.Assay;

/**
 * Javadocs go here!
 *
 * @author Tony Burdett
 * @date 19-Feb-2010
 */
public class AtlasLoadUpdatingFactorValueNodeHandler extends FactorValueNodeHandler {
    protected void writeValues() throws ObjectConversionException {
        // get the cache
        AtlasLoadCache cache = AtlasLoadCacheRegistry.getRegistry().retrieveAtlasLoadCache(investigation);

        // lookup hyb/assay nodes in the graph
        synchronized (investigation) {
            Iterable<AssayNode> assayNodes = investigation.SDRF.lookupNodes(AssayNode.class);
            Iterable<HybridizationNode> hybridizationNodes = investigation.SDRF.lookupNodes(HybridizationNode.class);

            // now, diff assay nodes with the assays in the cache
            for (AssayNode assayNode : assayNodes) {
                Assay assay = cache.fetchAssay(assayNode.getNodeName());

                if (assay != null) {
                    synchronized (assay) {
                        getLog().debug("Factor Values need adding for " + assay.getAccession());
                        SDRFWritingUtils.writeAssayProperties(investigation, assay, assayNode);
                    }
                }
            }

            // now, diff hyb nodes with the assays in the cache
            for (HybridizationNode hybridizationNode : hybridizationNodes) {
                Assay assay = cache.fetchAssay(hybridizationNode.getNodeName());

                if (assay != null) {
                    synchronized (assay) {
                        getLog().debug("Factor Values need adding for " + assay.getAccession());
                        SDRFWritingUtils.writeHybridizationProperties(investigation, assay, hybridizationNode);
                    }
                }
            }
        }
    }
}

@@ -1000,24 +1000,93 @@
 zationNode;%0A
+import uk.ac.ebi.arrayexpress2.magetab.datamodel.sdrf.node.SDRFNode;%0A
 import uk.ac
@@ -1798,24 +1798,1404 @@
 tigation);%0A%0A
+//        // fixme: this is the patch to prevent comod exceptions%0A//        // get the next node for compilation - in this case, this is the assay/hyb that this factor value is anchored to%0A//        SDRFNode hybNode;%0A//        while ((hybNode = getNextNodeForCompilation()) != null) %7B%0A//            synchronized (hybNode) %7B%0A//                Assay assay = cache.fetchAssay(hybNode.getNodeName());%0A//%0A//                if (assay != null) %7B%0A//                    synchronized (assay) %7B%0A//                        getLog().debug(%22Factor Values need adding for %22 + assay.getAccession());%0A//%0A//                        if (hybNode instanceof AssayNode) %7B%0A//                            SDRFWritingUtils.writeAssayProperties(investigation,%0A//                                                                  assay,%0A//                                                                  (AssayNode) hybNode);%0A//                        %7D%0A//                        else if (hybNode instanceof HybridizationNode) %7B%0A//                            SDRFWritingUtils.writeHybridizationProperties(investigation,%0A//                                                                          assay,%0A//                                                                          (HybridizationNode) hybNode);%0A//                        %7D%0A//                    %7D%0A//                %7D%0A//            %7D%0A//        %7D%0A%0A
         // l

fix for comod added as comments for info only. This is already fixed in sybaris branch.