/*
 * Copyright (c) 2017, University of Oslo
 *
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * Neither the name of the HISP project nor the names of its contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.hisp.dhis.android.core.resource;

import java.util.Date;

import javax.inject.Inject;
import javax.inject.Singleton;

@Singleton
public class ResourceHandler {
    private final ResourceStore resourceStore;
    private Date serverDate;

    @Inject
    public ResourceHandler(ResourceStore resourceStore) {
        this.resourceStore = resourceStore;
    }

    public void setServerDate(Date serverDate) {
        this.serverDate = new Date(serverDate.getTime());
    }

    public void handleResource(ResourceModel.Type resourceType) {
        if (resourceType == null || serverDate == null) {
            return;
        }
        int updatedResourceRow = resourceStore.update(resourceType.name(), serverDate, resourceType.name());
        if (updatedResourceRow <= 0) {
            resourceStore.insert(resourceType.name(), serverDate);
        }
    }

    /**
     * A wrapper to expose resourceStore.getLastUpdated(str).
     *
     * @param type Type of the resource.
     * @return a string representing the last synched date
     */
    public String getLastUpdated(ResourceModel.Type type) {
        return resourceStore.getLastUpdated(type);
    }
}

@@ -2183,24 +2183,25 @@
       %7D%0A
+%0A
         
 int upda
@@ -2196,27 +2196,16 @@
     
-int updated
 Resource
 Row 
@@ -2204,13 +2204,8 @@
 urce
-Row =
  res
@@ -2213,54 +2213,60 @@
 urce
-Store.update(resourceType.name(), serverDate, 
+ = Resource.builder()%0A                .resourceType(
 reso
@@ -2277,25 +2277,24 @@
 Type.name())
-;
 %0A        if 
@@ -2294,115 +2294,119 @@
     
-if (updatedResourceRow %3C= 0) %7B%0A            resourceStore.insert(resourceType.name(), serverDate);%0A        %7D
+        .lastSynced(serverDate)%0A                .build();%0A%0A        resourceStore.updateOrInsertWhere(resource);
 %0A   
@@ -2712,9 +2712,8 @@
 %0A    %7D%0A%7D
-%0A

Unify ResourceHandler