Package org.salamanca

Source Code of org.salamanca.Migrate

package org.salamanca;

import org.salamanca.broker.BrokerServer;
import java.util.Properties;
import java.io.InputStream;
import javax.jdo.*;
import javax.naming.Context;
import javax.naming.NamingException;
import java.io.IOException;
import javax.naming.InitialContext;
import org.salamanca.domain.*;
import java.util.*;
import javax.jdo.spi.PersistenceCapable;

/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class Migrate {
    static PersistenceManagerFactory pmfJDOInstruments = null;
    static PersistenceManagerFactory pmfKodo = null;

    public Migrate() {
    }

    /**
     * main
     *
     * @param args String[]
     */
    public static void main(String[] args) {
        initJDOInstruments();
        initKodo();
        migrar();
    }


    /**
     * initJDOInstruments
     */
    private static void initJDOInstruments() {

        Properties prop = new Properties();
        InputStream in;
        try {
            in = BrokerServer.class.getClassLoader().getResourceAsStream
                 ("jdoinstruments.properties");

            System.out.println(
                    " ---> Cargando archivo de configuraci�n jdoinstruments");
            prop.load(in);
            pmfJDOInstruments = org.jdoinstruments.runtime.
                                proxymodel.common.
                                PersistenceManagerFactoryProxy.
                                getPersistenceManagerFactory(prop);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    /**
     * initKodo
     */
    private static void initKodo() {

        Properties props = new java.util.Properties();

        try {
            InputStream iS = ClassLoader.getSystemResourceAsStream(
                    "kodo.properties");
            System.out.println(" ---> Cargando archivo de configuraci�n Kodo");
            props.load(iS);
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        pmfKodo = javax.jdo.JDOHelper.getPersistenceManagerFactory(props);
    }


    /**
     * initKodo
     */
    private static void migrar() {
        /*    User
            Curso-setAlumnos

            Alumno-persona
                  -curso
                  -vectorPeriodos

            Persona-setAlumno

         Periodo-alumno*/

        PersistenceManager pmJDOI = pmfJDOInstruments.getPersistenceManager();
        PersistenceManager pmKodo = pmfKodo.getPersistenceManager();

        Extent iterat = pmKodo.getExtent(Curso.class, true);
        if (iterat.iterator().hasNext()) {
            System.out.println("Imposible migrar sobre una base de datos llena");
            return;
        }

        pmKodo.currentTransaction().begin();
        Extent ext;


        Iterator iter;


        //---- Curso, Alumno,Persona,Periodo

        ext = pmJDOI.getExtent(Curso.class, true);
        //cargar
        int Curso_setAlumnoConNulo = 0;
        int Alumno_VectorPeriodoConNulo = 0;

        iter = ext.iterator();
        while (iter.hasNext()) {
            Curso itemCurso = (Curso) iter.next();

            Iterator iter2 = itemCurso.getSetAlumnos().iterator();
            while (iter2.hasNext()) {
                Alumno itemAlumno = (Alumno) iter2.next();

                if (itemAlumno != null) {
                    itemAlumno.getPersona().getSetAlumno();
                    itemAlumno.getCurso();
                    Enumeration enumer = itemAlumno.getVectorPeriodos().
                                         elements();
                    while (enumer.hasMoreElements()) {
                        Periodo itemPeriodo = (Periodo) enumer.nextElement();
                        if (itemPeriodo != null) {
                            itemPeriodo.getAlumno();
                        } else {
                            Alumno_VectorPeriodoConNulo++;
                        }
                    }
                } else {
                    Curso_setAlumnoConNulo++;
                }

            }

        }
        System.out.println("Curso Set Alumno con nulo = " +
                           Curso_setAlumnoConNulo);
        System.out.println("Alumno Vector Periodo con Nulo = " +
                           Alumno_VectorPeriodoConNulo);
        // persistencia

        //cargar
        HashSet setPersistentes = new HashSet();

        //
        ext = pmJDOI.getExtent(User.class, true);
        iter = ext.iterator();
        while (iter.hasNext()) {
            PersistenceCapable itemCurso = (PersistenceCapable) iter.next();
            if (itemCurso != null) {
                setPersistentes.add(itemCurso);
            }

        }

        //
        ext = pmJDOI.getExtent(Curso.class, true);
        iter = ext.iterator();
        while (iter.hasNext()) {
            PersistenceCapable itemCurso = (PersistenceCapable) iter.next();
            if (itemCurso != null) {
                setPersistentes.add(itemCurso);
            }

        }
        //
        ext = pmJDOI.getExtent(Alumno.class, true);
        iter = ext.iterator();
        while (iter.hasNext()) {
            PersistenceCapable itemCurso = (PersistenceCapable) iter.next();
            if (itemCurso != null) {
                setPersistentes.add(itemCurso);
            }

        }
        //
        ext = pmJDOI.getExtent(Persona.class, true);
        iter = ext.iterator();
        while (iter.hasNext()) {
            PersistenceCapable itemCurso = (PersistenceCapable) iter.next();
            if (itemCurso != null) {
                setPersistentes.add(itemCurso);
            }

        }

        //
        ext = pmJDOI.getExtent(Periodo.class, true);
        iter = ext.iterator();
        while (iter.hasNext()) {
            PersistenceCapable itemCurso = (PersistenceCapable) iter.next();
            if (itemCurso != null) {
                setPersistentes.add(itemCurso);
            }

        }

        //
        ext = pmJDOI.getExtent(Pago.class, true);
        iter = ext.iterator();
        while (iter.hasNext()) {
            PersistenceCapable itemCurso = (PersistenceCapable) iter.next();
            if (itemCurso != null) {
                setPersistentes.add(itemCurso);
            }

        }
        //
        ext = pmJDOI.getExtent(CuotaBasica.class, true);
        iter = ext.iterator();
        while (iter.hasNext()) {
            PersistenceCapable itemCurso = (PersistenceCapable) iter.next();
            if (itemCurso != null) {
                setPersistentes.add(itemCurso);
            }

        }

        pmJDOI.retrieveAll(setPersistentes);
        pmJDOI.makeTransientAll(setPersistentes);

        pmKodo.makePersistentAll(setPersistentes);
        pmKodo.currentTransaction().commit();

    }


}
TOP

Related Classes of org.salamanca.Migrate

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.