/*
 * Copyright 2017 flow.ci
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.flow.platform.api.util;

import com.flow.platform.api.domain.Job;
import com.flow.platform.api.domain.NodeStatus;
import com.flow.platform.domain.Jsonable;
import com.google.common.base.Strings;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author yh@firim
 */
public class JobConvertUtil {

    public static Job convert(Object[] objects) {

        return new Job(((BigDecimal) objects[0]).toBigInteger(),
            (Integer) objects[1],
            (String) objects[2],
            (String) objects[3],
            (String) objects[4],
            tsToZoneDateTime((Timestamp) objects[5]),
            tsToZoneDateTime((Timestamp) objects[6]),
            convertStatus((String) objects[7]),
            convertInteger(objects[8]),
            Jsonable.GSON_CONFIG.fromJson((String) objects[9], Map.class),
            (convertBigInteger(objects[10])).longValue(),
            (String) objects[11],
            tsToZoneDateTime((Timestamp) objects[12]),
            tsToZoneDateTime((Timestamp) objects[13]));
    }

    private static ZonedDateTime tsToZoneDateTime(Timestamp timestamp) {
        if (timestamp == null) {
            return null;
        }
        Instant i = timestamp.toInstant();
        ZonedDateTime z;
        z = ZonedDateTime.ofInstant(i, ZoneId.systemDefault());
        return z;
    }

    private static NodeStatus convertStatus(String name) {
        if (Strings.isNullOrEmpty(name)) {
            return NodeStatus.PENDING;
        }
        return NodeStatus.valueOf(name);
    }

    private static Integer convertInteger(Object object){
        if(object == null){
            return null;
        }
        return (Integer)object;
    }

    private static BigInteger convertBigInteger(Object object){
        if(object == null){
            return new BigInteger("0");
        }
        return (BigInteger)object;
    }

    public static List<Job> convert(List<Object[]> objects) {
        List<Job> jobs = new ArrayList<>();
        for (Object[] object : objects) {
            jobs.add(convert(object));
        }

        return jobs;
    }
}

@@ -983,24 +983,50 @@
 .ArrayList;%0A
+import java.util.HashMap;%0A
 import java.
@@ -1250,18 +1250,23 @@
     
-(
+convert
 Integer
-) 
+(
 obje
@@ -1271,16 +1271,17 @@
 jects%5B1%5D
+)
 ,%0A      
@@ -1282,33 +1282,38 @@
             
-(
+convert
 String
-) 
+(
 objects%5B2%5D,%0A
@@ -1310,16 +1310,17 @@
 jects%5B2%5D
+)
 ,%0A      
@@ -1321,33 +1321,38 @@
             
-(
+convert
 String
-) 
+(
 objects%5B3%5D,%0A
@@ -1349,16 +1349,17 @@
 jects%5B3%5D
+)
 ,%0A      
@@ -1360,33 +1360,38 @@
             
-(
+convert
 String
-) 
+(
 objects%5B4%5D,%0A
@@ -1388,16 +1388,17 @@
 jects%5B4%5D
+)
 ,%0A      
@@ -1603,68 +1603,29 @@
     
-Jsonable.GSON_CONFIG.fromJson((String) objects%5B9%5D, Map.class
+convertMap(objects%5B9%5D
 ),%0A 
@@ -1693,25 +1693,30 @@
         
-(
+convert
 String
-) 
+(
 objects%5B
@@ -1718,16 +1718,17 @@
 ects%5B11%5D
+)
 ,%0A      
@@ -2384,32 +2384,33 @@
 r(Object object)
+ 
 %7B%0A        if(obj
@@ -2397,32 +2397,33 @@
 ct) %7B%0A        if
+ 
 (object == null)
@@ -2414,32 +2414,33 @@
 (object == null)
+ 
 %7B%0A            re
@@ -2476,32 +2476,33 @@
 return (Integer)
+ 
 object;%0A    %7D%0A%0A 
@@ -2492,32 +2492,256 @@
  object;%0A    %7D%0A%0A
+    private static Map%3CString, String%3E convertMap(Object object) %7B%0A        if (object == null) %7B%0A            return new HashMap%3C%3E();%0A        %7D%0A        return Jsonable.GSON_CONFIG.fromJson((String) object, Map.class);%0A    %7D%0A%0A
     private stat
@@ -2786,16 +2786,17 @@
  object)
+ 
 %7B%0A      
@@ -2799,16 +2799,17 @@
       if
+ 
 (object 
@@ -2816,16 +2816,17 @@
 == null)
+ 
 %7B%0A      
@@ -2896,16 +2896,178 @@
 Integer)
+ object;%0A    %7D%0A%0A    private static String convertString(Object object) %7B%0A        if (object == null) %7B%0A            return null;%0A        %7D%0A        return (String) 
 object;%0A

fix typo