/*
 * Copyright (C) 2017 CenturyLink, Inc.
 *
 * 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.centurylink.mdw.util;

import java.io.File;
import java.util.Date;

import com.centurylink.mdw.dataaccess.AssetRef;
import com.centurylink.mdw.dataaccess.DataAccess;
import com.centurylink.mdw.dataaccess.file.LoaderPersisterVcs;
import com.centurylink.mdw.dataaccess.file.VersionControlGit;
import com.centurylink.mdw.model.JsonObject;
import com.centurylink.mdw.model.asset.Asset;
import com.centurylink.mdw.model.task.TaskTemplate;
import com.centurylink.mdw.model.workflow.Process;

public class AssetRefConverter {

    public static Process getProcess(AssetRef assetRef) throws Exception {
        Process proc = null;
        byte[] contentBytes = readAsset(assetRef);
        if (contentBytes != null && contentBytes.length > 0) {
            proc = new Process(new JsonObject(new String(contentBytes)));
            proc.setId(assetRef.getDefinitionId());
            int startIdx = assetRef.getName().lastIndexOf('/') + 1;
            int endIdx = assetRef.getName().lastIndexOf(".proc v");
            proc.setName(assetRef.getName().substring(startIdx, endIdx));
            proc.setLanguage(Asset.PROCESS);
            proc.setVersion(Asset.parseVersion(assetRef.getName().substring(endIdx+6)));
            proc.setPackageName(assetRef.getName().substring(0, startIdx-1));
        }
        return proc;
    }

    public static Asset getAsset(AssetRef assetRef) throws Exception {
        Asset asset = null;
        byte[] contentBytes = readAsset(assetRef);

        if (contentBytes != null && contentBytes.length > 0) {
            asset = new Asset();
            asset.setId(assetRef.getDefinitionId());
            int startIdx = assetRef.getName().lastIndexOf('/') + 1;
            int endIdx = assetRef.getName().lastIndexOf(" v");
            asset.setName(assetRef.getName().substring(startIdx, endIdx));
            asset.setLanguage(Asset.getFormat(asset.getName()));
            asset.setVersion(Asset.parseVersion(assetRef.getName().substring(endIdx+1)));
            asset.setPackageName(assetRef.getName().substring(0, startIdx-1));
            asset.setLoadDate(new Date());
            asset.setRaw(true);
            // do not load jar assets into memory
            if (!Asset.excludedFromMemoryCache(asset.getName()))
                asset.setRawContent(contentBytes);
        }
        return asset;
    }

    public static TaskTemplate getTaskTemplate(AssetRef assetRef) throws Exception {
        TaskTemplate taskVO = null;
        byte[] contentBytes = readAsset(assetRef);

        if (contentBytes != null && contentBytes.length > 0) {
            taskVO = new TaskTemplate(new JsonObject(new String(contentBytes)));
            taskVO.setTaskId(assetRef.getDefinitionId());
            int startIdx = assetRef.getName().lastIndexOf('/') + 1;
            int endIdx = assetRef.getName().lastIndexOf(" v");
            taskVO.setName(assetRef.getName().substring(startIdx, endIdx));
            taskVO.setVersion(Asset.parseVersion(assetRef.getName().substring(endIdx+1)));
            taskVO.setPackageName(assetRef.getName().substring(0, startIdx-1));
        }
        return taskVO;
    }

    private static byte[] readAsset(AssetRef assetRef) throws Exception {
        byte[] contentBytes = null;
        if (assetRef != null) {
            LoaderPersisterVcs lp = (LoaderPersisterVcs)DataAccess.getProcessLoader();
            VersionControlGit vc = (VersionControlGit)lp.getVersionControl();
            String tempName = assetRef.getName().substring(0, assetRef.getName().lastIndexOf(" v"));
            int fileExtIdx = tempName.lastIndexOf(".");
            tempName = tempName.substring(0, fileExtIdx).replace('.', '/');
            String path = getMissingPath(lp.getStorageDir(), "") + tempName + assetRef.getName().substring(0, assetRef.getName().lastIndexOf(" v")).substring(fileExtIdx);
            contentBytes = vc.readFromCommit(assetRef.getRef(), path);
        }
        return contentBytes;
    }

    private static String getMissingPath(File assetLoc, String path) {
        File test = new File(assetLoc.getPath() + "/.git");
        if (test.isDirectory())
            return path;

        return getMissingPath(new File(assetLoc.getParent()), assetLoc.getName() + "/" + path);

    }
}

@@ -1128,42 +1128,222 @@
 s;%0D%0A
-%0D%0Apublic class AssetRefConverter %7B
+import com.centurylink.mdw.util.log.LoggerUtil;%0D%0Aimport com.centurylink.mdw.util.log.StandardLogger;%0D%0A%0D%0Apublic class AssetRefConverter %7B%0D%0A%0D%0A    private static StandardLogger logger = LoggerUtil.getStandardLogger();
 %0D%0A%0D%0A
@@ -4067,33 +4067,16 @@
 ssetRef)
- throws Exception
  %7B%0D%0A    
@@ -4137,24 +4137,43 @@
 != null) %7B%0D%0A
+            try %7B%0D%0A
             
@@ -4803,24 +4803,201 @@
 (), path);%0D%0A
+            %7D%0D%0A            catch (Throwable ex) %7B%0D%0A                logger.severeException(%22Exception trying to read asset from Git: %22 + assetRef.getName(), ex);%0D%0A            %7D%0D%0A
         %7D%0D%0A 

Improve logging when issue reading asset from Git