1

I'm developping an application using spring-boot, I need to merge two object that mean that copy only the new values into another.

I tried this but it copy all the values frm obj1 to obj2:

public static void copyObj(User c1, User c2) {

        //String[] ignoredFields = { "legalForm", "email" };
        BeanUtils.copyProperties(c1, c2);
    }

what I need is to copy only the diffrent value from c1 to c2. This is a snippet:

**User c1**
{
    "name": "LALA",
    "email": "tst@tst.com"
}

**user c2**
{
    "name": "tata",
    "email": "email@tst.fr"
    "firsName": "toto"   
    "city": "Paris"
}

**expected result:** 
{
    "name": "LALA",
    "email": "tst@tst.com"
    "firsName": "toto",
    "city": "Paris"
}

The method above return:

{
    "name": "LALA",
    "email": "tst@tst.com"
}

Would you have any ideas ?

Best regards

Victor
  • 385
  • 2
  • 12
  • 26
  • 2
    Possible duplicate of [Helper in order to copy non null properties from object to another ? (Java)](https://stackoverflow.com/questions/1301697/helper-in-order-to-copy-non-null-properties-from-object-to-another-java) – Alejandro C. Jun 07 '17 at 17:52

1 Answers1

0

Here is one of ways to do:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JFileChooser;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class JsonParser {

    JFileChooser chooser = new JFileChooser();
    File f;

    static String fn = "";
    static String js1 = "{\"name\": \"LALA\", \"email\": \"tst@tst.com\"}";
    static String js2 = "{\"name\": \"tata\", \"email\": \"email@tst.fr\", \"firsName\": \"toto\", \"city\": \"Paris\"}";
    String name = "name";
    String email = "email";
    String fName = "firsName";
    String city = "city";
    // ... other needed fields
    User u1 = null;
    User u2 = null;
    User nu = null;

    public JsonParser() {
        parseFile();
        nu = copyObj(u1, u2);
        System.out.println("\n" + nu.toShortString());
    }

    private String openFchooser() throws FileNotFoundException, IOException, InterruptedException, Exception {
        int returnVal = chooser.showOpenDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            f = chooser.getSelectedFile();
        }
        return f.getAbsolutePath();

    }

    // To parse JSON files with data
    //===========================================
    public void parseFile() {
        JSONParser parser = new JSONParser();

        try {
//            fn = openFchooser();
//            Object obj = parser.parse(new FileReader(fn));
            // To parse obj 1
            Object obj1 = parser.parse(js1);
            System.out.println("User 1: " + obj1.toString());
            System.out.println();

            JSONObject jobj1 = (JSONObject) obj1;
            String from_name = jobj1.get(name).toString();
            String from_email = jobj1.get(email).toString();
//            String from_fName = jobj1.get(fName).toString();
//            String from_city = jobj1.get(city).toString();
            u1 = new User(from_name, from_email, null, null);
//            System.out.println(u1.toString() + "\n");

            Object obj2 = parser.parse(js2);
            System.out.println("User 2: " + obj2.toString());
            JSONObject jobj2 = (JSONObject) obj2;
            from_name = jobj2.get(name).toString();
            from_email = jobj2.get(email).toString();
            String from_fName = jobj2.get(fName).toString();
            String from_city = jobj2.get(city).toString();
            u2 = new User(from_name, from_email, from_fName, from_city);
//            System.out.println(u2.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new JsonParser();
    }

    public User copyObj(User u1, User u2) {
        User nuser = new User(null, null, null, null);
        String fn1 = u1.getFirsName();
        String fn2 = u2.getFirsName();
        boolean hasUppercase1 = !fn1.equals(fn1.toLowerCase());
        boolean hasLowercase1 = !fn1.equals(fn1.toUpperCase());
        boolean hasUppercase2 = !fn2.equals(fn2.toLowerCase());
        boolean hasLowercase2 = !fn2.equals(fn2.toUpperCase());

        // To do something needed
        if (hasUppercase1 && hasLowercase2) {
            nuser.setFirsName(u1.name);
            nuser.setEmail(u1.email);
        }
        if (hasLowercase1 && hasUppercase2) {
            nuser.setFirsName(u2.name);
            nuser.setEmail(u2.email);
        }
        // also some conditions
        return nuser;
    }

    class User {

        String name = null;
        String email = null;
        String fName = null;
        String city = null;

        public User(String n, String e, String f, String c) {
            this.name = n;
            this.email = e;
            this.fName = f;
            this.city = c;
        }

        public String getFirsName() {
            return this.name;
        }

        public String setFirsName(String s) {
            return this.name = s;
        }

        public String getEmail() {
            return this.email;
        }

        public String setEmail(String s) {
            return this.email = s;
        }

        public String toString() {
            return "{\"name\":" + this.name + ", "
                    + "\"email\":" + this.email + ", "
                    + "\"firsName\":" + this.fName + ", "
                    + "\"city\":" + this.city + "\"}";
        }

        public String toShortString() {
            return "{\"name\": \"" + this.name + "\", "
                    + "\"email\": \"" + this.email + "\"}";
        }
    };
}

OUTPUT:

User 1: {"name":"LALA","email":"tst@tst.com"}

User 2: {"city":"Paris","firsName":"toto","name":"tata","email":"email@tst.fr"}

{"name": "LALA", "email": "tst@tst.com"}

When you want other conditions you can do it in the method copyObj(). Good Luck!

Vasyl Lyashkevych
  • 1,920
  • 2
  • 23
  • 38