Search in sources :

Example 1 with MMigration

use of org.compiere.model.MMigration in project adempiere by adempiere.

the class MigrationApply method doIt.

@Override
protected String doIt() throws Exception {
    if (Ini.isPropertyBool(Ini.P_LOGMIGRATIONSCRIPT)) {
        addLog(Msg.getMsg(getCtx(), "LogMigrationScriptFlagIsSetMessage"));
        return "@Error@";
    }
    // Use a null transaction to generate a read only query
    // Doesn't lock table 
    MMigration migration = new MMigration(getCtx(), getRecord_ID(), null);
    if (migration.is_new()) {
        addLog(Msg.getMsg(getCtx(), "NoMigrationMessage"));
        //return;
        return "@Error@";
    }
    migration.setIsForce(isForce());
    try {
        addLog(migration.apply());
    } catch (AdempiereException e) {
        addLog(e.getMessage());
        if (// abort on first error
        !isForce())
            throw new AdempiereException(e.getMessage(), e);
    }
    return "@OK@";
}
Also used : AdempiereException(org.adempiere.exceptions.AdempiereException) MMigration(org.compiere.model.MMigration)

Example 2 with MMigration

use of org.compiere.model.MMigration in project adempiere by adempiere.

the class MigrationCreate method doIt.

/**
	 * 
	 * Process to create migration from selected records
	 * 
	 * @author Paul Bowden, Adaxa Pty Ltd
	 *
	 */
@Override
protected String doIt() throws Exception {
    MMigration migration = new MMigration(getCtx(), 0, get_TrxName());
    MTable table = MTable.get(getCtx(), tableId);
    String whereClause;
    List<PO> pos;
    if (recordId > 0) {
        pos = new ArrayList<PO>(1);
        pos.add(table.getPO(recordId, get_TrxName()));
    } else {
        String where = "EntityType = ?";
        pos = table.createQuery(where, get_TrxName()).list();
    }
    for (PO po : pos) {
        POInfo info = POInfo.getPOInfo(getCtx(), tableId, get_TrxName());
        MMigrationStep step = new MMigrationStep(migration, po, info, MMigrationStep.ACTION_Insert);
    }
    return "@OK@";
}
Also used : POInfo(org.compiere.model.POInfo) MTable(org.compiere.model.MTable) MMigrationStep(org.compiere.model.MMigrationStep) MMigration(org.compiere.model.MMigration) PO(org.compiere.model.PO)

Example 3 with MMigration

use of org.compiere.model.MMigration in project adempiere by adempiere.

the class MigrationFromXML method loadFile.

private void loadFile(File file) throws SAXException, IOException {
    if (!file.exists())
        return;
    if (!file.getName().endsWith(".xml"))
        return;
    if (file.getName().equals("build.xml"))
        return;
    log.log(Level.CONFIG, "Loading file: " + file);
    Document doc = builder.parse(file);
    NodeList migrations = doc.getDocumentElement().getElementsByTagName("Migration");
    for (int i = 0; i < migrations.getLength(); i++) {
        Element element = (Element) migrations.item(i);
        // Top level - create a new transaction for every migration and commit
        Trx.run(trxName -> {
            Properties ctx = Env.getCtx();
            MMigration migration;
            try {
                migration = MMigration.fromXmlNode(ctx, element, trxName);
                if (migration == null) {
                    log.log(Level.CONFIG, "XML file not a Migration. Skipping.");
                    return;
                }
                if (isApply()) {
                    if (MMigration.STATUSCODE_Applied.equals(migration.getStatusCode())) {
                        log.log(Level.CONFIG, migration.toString() + " ---> Migration already applied - skipping.");
                        return;
                    }
                    if (MMigration.STATUSCODE_Failed.equals(migration.getStatusCode()) || MMigration.STATUSCODE_PartiallyApplied.equals(migration.getStatusCode())) {
                        log.log(Level.CONFIG, migration.toString() + " ---> Migration exists but has to be rolled back.");
                        applyMigration(migration.getCtx(), migration.getAD_Migration_ID(), trxName);
                    }
                    applyMigration(migration.getCtx(), migration.getAD_Migration_ID(), trxName);
                }
            } catch (AdempiereException | SQLException e) {
                if (!isForce()) {
                    throw new AdempiereException("Loading migration from " + file.toString() + " failed.", e);
                }
            }
        });
    }
}
Also used : SQLException(java.sql.SQLException) AdempiereException(org.adempiere.exceptions.AdempiereException) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) Properties(java.util.Properties) MMigration(org.compiere.model.MMigration)

Example 4 with MMigration

use of org.compiere.model.MMigration in project adempiere by adempiere.

the class MigrationToXML method doIt.

@Override
protected String doIt() throws Exception {
    MMigration migration = new MMigration(getCtx(), migrationId, get_TrxName());
    if (migration == null || migration.is_new())
        return "No migration to export";
    log.log(Level.FINE, "Creating xml document for migration: " + migration);
    Document document = null;
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    document = builder.newDocument();
    Element root = document.createElement("Migrations");
    document.appendChild(root);
    root.appendChild(migration.toXmlNode(document));
    //set up a transformer
    TransformerFactory transfac = TransformerFactory.newInstance();
    transfac.setAttribute("indent-number", 2);
    Transformer trans;
    trans = transfac.newTransformer();
    trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    trans.setOutputProperty(OutputKeys.INDENT, "yes");
    trans.setOutputProperty(OutputKeys.STANDALONE, "yes");
    log.log(Level.FINE, "Writing xml to file.");
    //create string from xml tree
    FileWriter fw = new FileWriter(fileName);
    StreamResult result = new StreamResult(fw);
    DOMSource source = new DOMSource(document);
    trans.transform(source, result);
    fw.close();
    return "Exported migration to: " + fileName;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) FileWriter(java.io.FileWriter) Document(org.w3c.dom.Document) MMigration(org.compiere.model.MMigration)

Example 5 with MMigration

use of org.compiere.model.MMigration in project adempiere by adempiere.

the class MigrationMerge method prepare.

@Override
protected void prepare() {
    int fromId = 0, toId = 0;
    ProcessInfoParameter[] params = getParameter();
    for (ProcessInfoParameter p : params) {
        String para = p.getParameterName();
        if (para.equals("AD_Migration_ID"))
            fromId = p.getParameterAsInt();
        else if (para.equals("AD_MigrationTo_ID"))
            toId = p.getParameterAsInt();
    }
    // launched from migration window
    if (toId == 0)
        toId = getRecord_ID();
    migrationTo = new MMigration(getCtx(), toId, get_TrxName());
    migrationFrom = new MMigration(getCtx(), fromId, get_TrxName());
}
Also used : MMigration(org.compiere.model.MMigration)

Aggregations

MMigration (org.compiere.model.MMigration)5 AdempiereException (org.adempiere.exceptions.AdempiereException)2 Document (org.w3c.dom.Document)2 Element (org.w3c.dom.Element)2 FileWriter (java.io.FileWriter)1 SQLException (java.sql.SQLException)1 Properties (java.util.Properties)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 Transformer (javax.xml.transform.Transformer)1 TransformerFactory (javax.xml.transform.TransformerFactory)1 DOMSource (javax.xml.transform.dom.DOMSource)1 StreamResult (javax.xml.transform.stream.StreamResult)1 MMigrationStep (org.compiere.model.MMigrationStep)1 MTable (org.compiere.model.MTable)1 PO (org.compiere.model.PO)1 POInfo (org.compiere.model.POInfo)1 NodeList (org.w3c.dom.NodeList)1