Source Tree resolve conflicts with an external tool

2018-05-21 12_55_29-Sourcetree-blue - Internet Explorer

Source Tree supports multiple external tools to compare differences and resolve conflicts between files. By default you can choose any of this:

Source tree tools

But In this entry I’m going to show how to use it with Meld. 

Meld is a visual diff and merge tool targeted at developers. Meld helps you compare files, directories, and version controlled projects.

In linux systems it’s very useful and it’s my preffered tool (only when I can’t use diff and merge tools from my IDE).

How to configure

Got to main menú an select Tools > Options > Tab Diff  and into selection option External Diff  pick Custom and into field Difff Command set the path to your meld binary (I’m on windows and my case is Meld.exe), next into field Arguments set this:

\"$LOCAL\" \"$REMOTE\"

Now for Merge Tool, select the same binary (Meld.exe) and set this into filed Arguments:

 
--auto-merge \"$LOCAL\" \"$BASE\" \"$REMOTE\" --output=\"$MERGED\" 

Now if you choose anyfile in your project you can check the diff selecting External Diff or CTRL+D over your file, and meld it’s executed:

extdifmeld

And it’s the same for files in conflict, select your file with the context menu Resolve conflicts > Launch External Merge Tool, now you can view side to side the differences between versions.

extmergemeld

And thats all, cheers

 

Anuncios

Spring and JPA with two data sources (with annotations)

A few days I recevied a comment from my friend @sock_osg (you can follow on twitter) he recomends me to rewrite my previous post with annotations.

And well here is it. But the are more few things to comment about it, for example in the previous post there are not a Transactional capabilities between DB’s because I’m not using JTA transaction type.

Now for this example I write the code to support transactions between multple data bases with different data sources, like I read on stack overflow:

if you find yourself with multiple entity managers, with corresponding tx managers, then you should consider using a single JtaTransactionManager instead. The entity managers should be able to participate in JTA transactions, and this will give you full transactionality across both entity managers, without having to worry about which entity manager you’re in at any one time.

You can download all code here, it’s hosted on my Github account, pull the code from the branch named “annotations”.

Let’s review the most important files, frst the DAO classes: Seguir leyendo

Spring and JPA with two data sources

JPA it’s the most used standar in java to manage the persistence and I ever want to write about this topic.

In this entry I want to share how configure a java project to use JPA with spring with two data sources, you can view all source of this project on my github repository.

Most of the configuration is in spring, in the application context you need declare:

  • 2 Data Sources
  • 2 Entity Manager Factories
  • 2 Transaction Manager

This is explained by self:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/jdbc
        http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">

    <context:property-placeholder location="classpath:app-props.properties" />

    <context:component-scan base-package="org.oz" />
    <context:annotation-config/>

    <bean id="pum" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
        <property name="persistenceXmlLocations">
            <list>
                <value>classpath:META-INF/persistence.xml</value>
            </list>
        </property>
        <property name="dataSources">
            <map>
                <entry key="localDataSource" value-ref="dataSource1"/>
                <entry key="remoteDataSource" value-ref="dataSource2"/>
            </map>
        </property>

        <!-- if no datasource is specified, use this one -->
        <property name="defaultDataSource" ref="dataSource1"/>
        <property name="defaultPersistenceUnitName" value="unit1"/>

    </bean>

    <bean id="dataSource1" class="org.springframework.jdbc.datasource.DriverManagerDataSource" lazy-init="true" primary="true"
          p:driverClassName="${jdbc1.driver}"
          p:url="${jdbc1.url}"
          p:username="${jdbc1.user}"
          p:password="${jdbc1.pass}"
    />

    <bean id="entityManagerFactory1" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
          p:persistenceXmlLocation="classpath:META-INF/persistence.xml"
          p:persistenceUnitName="unit1"
          p:dataSource-ref="dataSource1"
          p:packagesToScan="org.oz.persistence.dao.db1"
          lazy-init="true"/>

    <bean id="transactionManager1" class="org.springframework.orm.jpa.JpaTransactionManager"
          p:entityManagerFactory-ref="entityManagerFactory1"
          lazy-init="true"/>

    <bean id="dataSource2" class="org.springframework.jdbc.datasource.DriverManagerDataSource" lazy-init="true"
          p:driverClassName="${jdbc2.driver}"
          p:url="${jdbc2.url}"
          p:username="${jdbc2.user}"
          p:password="${jdbc2.pass}"
    />

    <bean id="entityManagerFactory2" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
          p:persistenceXmlLocation="classpath:META-INF/persistence.xml"
          p:persistenceUnitName="unit2"
          p:dataSource-ref="dataSource2"
          p:packagesToScan="org.oz.persistence.dao.db2"
          lazy-init="true"/>

    <bean id="transactionManager2" class="org.springframework.orm.jpa.JpaTransactionManager"
          p:entityManagerFactory-ref="entityManagerFactory2"
          lazy-init="true"/>

    <tx:annotation-driven transaction-manager="transactionManager1"  />
    <tx:annotation-driven transaction-manager="transactionManager2" />

    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

    <bean id="customerDao" class="org.oz.persistence.dao.db1.CustomerDao"/>
    <bean id="productDao" class="org.oz.persistence.dao.db2.ProductDao"/>

</beans>

 

Now in peresistence.xml we going to declare two persistence units:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
             http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

    <persistence-unit name="unit1" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

        <mapping-file>META-INF/native-querys.xml</mapping-file>
        <class>org.oz.persistence.dao.db1.model.Customer</class>

        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
            <property name="hibernate.connection.driver_class" value="org.h2.Driver"/>
            <property name="hibernate.archive.autodetection" value="class,hbm"/>
            <property name="useUnicode" value="true"/>
            <property name="characterSetResults" value="UTF8"/>
            <property name="characterEncoding" value="UTF8"/>
            <property name="hibernate.format_sql" value="false"/>
            <property name="hibernate.use_sql_comments" value="false"/>
            <property name="hibernate.hbm2ddl.keywords" value="auto-quote"/>
            <property name="hibernate.bytecode.use_reflection_optimizer" value="true"/>
            <property name="hibernate.connection.useUnicode" value="true"/>
            <property name="hibernate.connection.characterEncoding" value="UTF8"/>
            <property name="hibernate.connection.charSet" value="UTF8"/>
            <property name="hibernate.connection.characterSetResults" value="UTF8"/>

            <property name="hibernate.default_schema" value="BASEA"/>
        </properties>

    </persistence-unit>

    <persistence-unit name="unit2" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

        <mapping-file>META-INF/native-querys.xml</mapping-file>
        <class>org.oz.persistence.dao.db2.model.Product</class>

        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
            <property name="hibernate.connection.driver_class" value="org.h2.Driver"/>
            <property name="hibernate.archive.autodetection" value="class,hbm"/>
            <property name="useUnicode" value="true"/>
            <property name="characterSetResults" value="UTF8"/>
            <property name="characterEncoding" value="UTF8"/>
            <property name="hibernate.format_sql" value="false"/>
            <property name="hibernate.use_sql_comments" value="false"/>
            <property name="hibernate.hbm2ddl.keywords" value="auto-quote"/>
            <property name="hibernate.bytecode.use_reflection_optimizer" value="true"/>
            <property name="hibernate.connection.useUnicode" value="true"/>
            <property name="hibernate.connection.characterEncoding" value="UTF8"/>
            <property name="hibernate.connection.charSet" value="UTF8"/>
            <property name="hibernate.connection.characterSetResults" value="UTF8"/>

            <property name="hibernate.default_schema" value="BASEB"/>
        </properties>

    </persistence-unit>
</persistence>

Then only we’re need to do is set the reference of the persistence unit in every DAO to inject the EntityManager:

package org.oz.persistence.dao.db1;

import javax.persistence.*;
import java.util.Collection;

/**
 * Created by <a href="https://twitter.com/jaehoox">jaehoo</a> on 16/03/2018
 */
public class CustomerDao {

    public static final String SEL_TABLES="select.tablesh2";

    @PersistenceContext(unitName = "unit1" , type = PersistenceContextType.TRANSACTION)
    private EntityManager em;

    public Collection loadCustomers() {
        Query query = em.createQuery("FROM Customer");
        return query.getResultList();

    }

    public Collection getTables(){
        return em.createNamedQuery(SEL_TABLES).getResultList();
    }

}

package org.oz.persistence.dao.db2;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;
import java.util.Collection;

/**
 * Created by <a href="https://twitter.com/jaehoox">jaehoo</a> on 16/03/2018
 */
public class ProductDao {

    public static final String SEL_TABLES="select.tablesh2";

    @PersistenceContext(unitName = "unit2", type = PersistenceContextType.TRANSACTION, name = "unit2")
    private EntityManager em;

    public Collection loadProducts() {
        Query query = em.createQuery("FROM Product");
        return query.getResultList();

    }

    public Collection getTables(){
        return em.createNamedQuery(SEL_TABLES).getResultList();
    }

}

The interesting point of this is in the use of the transaction manager, lets take a look on test class:

package org.oz.persistence.dao.db1;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.oz.persistence.dao.db1.model.Customer;
import org.oz.persistence.dao.db2.ProductDao;
import org.oz.persistence.dao.db2.model.Product;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:app-ctx-test.xml"})
@TransactionConfiguration
@Slf4j
public class CustomerDaoTest {

    @Resource(name = "customerDao")
    private CustomerDao customerDao;

    @Resource(name = "productDao")
    private ProductDao productDao;

    @Test
    @Transactional("transactionManager1")
    public void loadCustomers() throws Exception {

        List customers = (List) customerDao.loadCustomers();

        log.info("customers:{}",customers.size());

        for(Customer c : customers){
            log.info("{}",c);
        }

        List tables = (List) customerDao.getTables();

        log.info("tables:{}",tables.size());
        for(Object c : tables){
            log.info("{}",c);
        }

    }

    @Test
    @Transactional("transactionManager2")
    public void loadProducts() throws Exception {

        List products = (List) productDao.loadProducts();

        log.info("products:{}",products.size());
        log.info("{}",products.get(0));

        List tables = (List) productDao.getTables();

        log.info("tables:{}",tables.size());
        for(Object c : tables){
            log.info("{}",c);
        }

    }

    @Test
    public void queryngTwoSoruces() throws Exception {

        log.info("getting data from two DS in one method");
        List tables = new ArrayList();

        tables.addAll(productDao.getTables());
        tables.addAll(customerDao.getTables());

        log.info("tables:{}",tables.size());
        for(Object c : tables){
            log.info("{}",c);
        }

    }

}

The first lines load the application context and inject DAO classes with the Entity Manager.

The first and the second method get all records of the customer and Product table and get all data base table names, each of one is using their corresponding transaction manager (as you can see on @Transactional annotation), until here everything is clear you can querying data from each data base.

But what happens in the third method??

Interesting rigth? and the anwser is… there is not a Transaction implicit, yeah so simple! it works but I don’t have transactional capabilities, that means that I need to manage each transaction in separated ways.

But I read this in stackoverflow:

The javadoc for JpaTransactionManager has some advice on this:

“This transaction manager is appropriate for applications that use a single JPA EntityManagerFactory for transactional data access. JTA (usually through JtaTransactionManager) is necessary for accessing multiple transactional resources within the same transaction. Note that you need to configure your JPA provider accordingly in order to make it participate in JTA transactions.”

In other words, if you find yourself with multiple entity managers, with corresponding tx managers, then you should consider using a single JtaTransactionManager instead. The entity managers should be able to participate in JTA transactions, and this will give you full transactionality across both entity managers, without having to worry about which entity manager you’re in at any one time.

Of course, JtaTransactionManager does require a full JTA-supporting application server, rather than a vanilla servlet engine like Tomcat.

Maybe one day I’ll write about how to do this with the server container but now this is enough.

Cheers

SAP PO Soap to Soap scenario with multiple operations (sync and async)

I don’t know but I was thinking, why in SAP make a simple web service it’s awful task?

In other technologies (Java for example) you can publish one service with multiple operations with synchronous and asynchronous communication, but do this in SAP PO it’s very tricky.

I don’t write how to develop the scenario, instead, I prefer to talk about “that dirty tricks” to achieve this.

For example, you develop a service interface with multiple operations to expose some functionality via Advanced Adapter Engine, I use soap sender adapter for the outbound system (legacy system) and soap receiver for the inbound system (SAP), I’m working on SAP PO 7.5 (in older versions too).

2018-04-05 10_21_52-Enterprise Services Builder (vhephxidci_XID_00)2018-04-05 10_22_38-Enterprise Services Builder (vhephxidci_XID_00) Seguir leyendo

SAP PI Naming Convention

When I start on PI development I feeling very confused by the naming convention of their components, a couple years ago I was thinking if it is a best naming convention but I don’t found anything about it.

Talking to other consultants of PI/PO they are agree this naming is a responsibility of the organization standards, but in some times it’s not the best.

For example, when you implement a Java proxy if you naming convention it’s not clear the java code is very awful and I don’t like that. In the organization when I work this is very common mistake, because every release of new service interface has problems with the other develoment teams to integrate your systems, only by the naming convetion is too large or dificult to read.

I’ve been developing services interfaces around of five years and recently I found (in my opinion) the best naming convention for SAP PI/PO in this website.

The document is like a cheetshet and it’s very clear to read, definitely I recommend this.

If you has problems to access I leave a copy of the document here

Cheers

SAP PO get SOAP xml payload

Frecuently I need to check xml payload on SOAP service interfaces but the PO it’s configured to not persist this messages.

In old versions of PI (like 7.10) you can use the TraceHTTP parameter in  sender or receiver adapter, in the module sap.com/com.sap.aii.af.soapadapter/XISOAPAdapterBean, this paramenter write the xml payload in the message log, according to next values:

  • headers: Only HTTP headers are traced for request and response
  • plain: the whole HTTP request and response are traced (including headers and body)
  • hex: A hex dump is traced for whole HTTP request and response

Seguir leyendo

SAP PO mapping Sync SOAP to JDBC

Well, this time I would like to share this tip to create a service interface to retrieve data from Database with SAP PO/PI.

When you develop the message types to get SQL data you must to check the names and the structure of the Data Types, for example, this is a simple structure to do this query:

SELECT VC_PEDIMENTO, INVNUM, VC_PRC_FILE_FLG
FROM MYTABLE WHERE (INVNUM='4900005421')

For the request sender message type you can write your own structure with any label names:

2018-03-07 11_00_30-MM_ConsultarPedidos_ECC_to_DLX_req_ Display Message Mapping Seguir leyendo