2

I have 25+ tables and I have used Content Provider with Database.

I have created separate files for each tables with following structure:

  • TProductUnit.java in package of com.myapp.db.tables

    public class TProductUnit {
    
        /***
         * Fields of TABLE_PRODUCT_UNIT Table
         ***/
        public static final String TABLE_PRODUCT_UNIT = "product_unit";
        /**
         * Columns of TABLE_PRODUCT_UNIT
         */
        public static final String PRODUCT_UNIT_SERVER_ID = "id";
        public static final String PRODUCT_UNIT_NAME = "name";
        public static final String PRODUCT_UNIT_ITP = "itp";
        public static final String PRODUCT_UNIT_UTP = "utp";
        public static final String PRODUCT_UNIT_STATUS = "status";
    
        public static String[] PRODUCT_UNIT_COLUMNS = new String[] {
                BaseColumns._ID,
                PRODUCT_UNIT_SERVER_ID,
                PRODUCT_UNIT_NAME,
                PRODUCT_UNIT_ITP,
                PRODUCT_UNIT_UTP,
                PRODUCT_UNIT_STATUS
        };
    }
    
  • ProductUnit.java is POJO class which will helpful when First time get data from Server.

    public class ProductUnit {
    
            @SerializedName("id")
            @Expose
            private Integer id;
            @SerializedName("product_id")
            @Expose
            private Integer productId;
            @SerializedName("url")
            @Expose
            private String url;
            @SerializedName("bit")
            @Expose
            private int bit;
            @SerializedName("status")
            @Expose
            private Integer status;
            @SerializedName("itp")
            @Expose
            private String itp;
            @SerializedName("utp")
            @Expose
            private String utp;
    
            /**
             * @return The id
             */
            public Integer getId() {
                return id;
            }
    
            /**
             * @param id The id
             */
            public void setId(Integer id) {
                this.id = id;
            }
    
            public Integer getProductId() {
                return productId;
            }
    
            public void setProductId(Integer productId) {
                this.productId = productId;
            }
    
            public int getBit() {
                return bit;
            }
    
            public void setBit(int bit) {
                this.bit = bit;
            }
    
            public String getUrl() {
                return url;
            }
    
            public void setUrl(String url) {
                this.url = url;
            }
    
            /**
             * @return The status
             */
            public Integer getStatus() {
                return status;
            }
    
            /**
             * @param status The status
             */
            public void setStatus(Integer status) {
                this.status = status;
            }
    
            /**
             * @return The itp
             */
            public String getItp() {
                return itp;
            }
    
            /**
             * @param itp The itp
             */
            public void setItp(String itp) {
                this.itp = itp;
            }
    
            /**
             * @return The utp
             */
            public String getUtp() {
                return utp;
            }
    
            /**
             * @param utp The utp
             */
            public void setUtp(String utp) {
                this.utp = utp;
            }
    
            /**
             * Convenient method to get the objects data members in ContentValues object.
             * This will be useful for Content Provider operations,
             * which use ContentValues object to represent the data.
             *
             * @return
             */
            public ContentValues getContentValues() {
                ContentValues values = new ContentValues();
    
                values.put(PRODUCT_UNIT_SERVER_ID, id);
                values.put(PRODUCT_UNIT_NAME, name);
                values.put(PRODUCT_UNIT_ITP, itp);
                values.put(PRODUCT_UNIT_UTP, utp);
                values.put(PRODUCT_UNIT_STATUS, status);
    
                return values;
            }
    }
    

Both classes have most of the same number of fields with same values if we think about @SerializedName

Problem:

  1. Whenever I need to add some fields in any Particular Table then I have to add in all Table file and JSON POJO Class too.
  2. When any field name changed by server side then I have to change in both file.

My Question is: Is there any better solution for this optimization. Have you ever manage like this?

P.S. I have 25+ tables so I have to create 50+ classes.

Help please. Thanks.

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
  • Probably observer design pattern can be implemented. http://www.journaldev.com/1739/observer-design-pattern-in-java – Sreehari Oct 01 '16 at 07:35
  • Use a ORM package so you can just have one model class that also handles the database side. GreenDao, Realm, ActiveAndroid, etc. – nasch Oct 01 '16 at 17:18

0 Answers0