package io.lumify.palantir.dataImport;

import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import io.lumify.core.exception.LumifyException;
import io.lumify.palantir.dataImport.model.PtLinkType;
import io.lumify.palantir.dataImport.model.PtObjectObject;
import org.securegraph.Vertex;

import java.util.List;
import java.util.Map;

import static com.google.common.base.Preconditions.checkNotNull;

public class PtObjectObjectImporter extends PtGroupingImporterBase<PtObjectObject, Long> {
    protected PtObjectObjectImporter(final DataImporter dataImporter) {
        super(dataImporter, PtObjectObject.class);
    }

    @Override
    protected void beforeProcessRows() {
        super.beforeProcessRows();
        getDataImporter().getGraph().flush();
    }

    @Override
    protected void processGroup(Long parentObjectId, List<PtObjectObject> rows) throws Exception {
        String sourceObjectId = getObjectVertexId(parentObjectId);
        Vertex sourceVertex = getVertexCache().get(sourceObjectId);
        checkNotNull(sourceVertex, "Could not find source vertex: " + sourceObjectId);

        Iterable<String> destVertexIds = Iterables.transform(rows, new Function<PtObjectObject, String>() {
            @Override
            public String apply(PtObjectObject row) {
                return getObjectVertexId(row.getChildObjectId());
            }
        });
        Map<String, Vertex> destVertices = getVertexCacheVertices(destVertexIds);

        for (PtObjectObject row : rows) {
            PtLinkType ptLinkType = getDataImporter().getLinkTypes().get(row.getType());
            if (ptLinkType == null) {
                throw new LumifyException("Could not find link type: " + row.getType());
            }
            String linkTypeUri = getLinkTypeUri(ptLinkType.getUri());

            String destObjectId = getObjectVertexId(row.getChildObjectId());
            Vertex destVertex = destVertices.get(destObjectId);
            checkNotNull(destVertex, "Could not find dest vertex: " + destObjectId);

            String edgeId = getEdgeId(row);
            getDataImporter().getGraph().addEdge(edgeId, sourceVertex, destVertex, linkTypeUri, getDataImporter().getVisibility(), getDataImporter().getAuthorizations());
        }
    }

    @Override
    protected Long getGroupKey(PtObjectObject ptObjectObject) {
        return ptObjectObject.getParentObjectId();
    }

    protected String getEdgeId(PtObjectObject ptObjectObject) {
        return getDataImporter().getIdPrefix() + ptObjectObject.getLinkId();
    }

    protected String getLinkTypeUri(String uri) {
        return getDataImporter().getOwlPrefix() + uri;
    }

    @Override
    protected String getSql() {
        // order by parent_object_id to improve cache hits
        return "select * from {namespace}.PT_OBJECT_OBJECT ORDER BY parent_object_id";
    }
}

@@ -37,92 +37,8 @@
 t;%0A%0A
-import com.google.common.base.Function;%0Aimport com.google.common.collect.Iterables;%0A
 impo
@@ -232,54 +232,8 @@
 x;%0A%0A
-import java.util.List;%0Aimport java.util.Map;%0A%0A
 impo
@@ -344,16 +344,11 @@
 s Pt
-Grouping
+Row
 Impo
@@ -374,14 +374,8 @@
 ject
-, Long
 %3E %7B%0A
@@ -691,40 +691,12 @@
 cess
-Group(Long parentObjectId, List%3C
+Row(
 PtOb
@@ -709,14 +709,12 @@
 ject
-%3E
  row
-s
 ) th
@@ -751,22 +751,22 @@
 g source
-Object
+Vertex
 Id = get
@@ -784,177 +784,29 @@
 xId(
-parentObjectId);%0A        Vertex sourceVertex = getVertexCache().get(sourceObjectId);%0A        checkNotNull(sourceVertex, %22Could not find source vertex: %22 + source
+row.getParent
 ObjectId
 );%0A%0A
@@ -801,28 +801,29 @@
 ObjectId
+()
 );%0A
-%0A
         
 Iterable
@@ -818,24 +818,14 @@
     
-Iterable%3C
 String
-%3E
  des
@@ -837,376 +837,55 @@
 exId
-s
  = 
-Iterables.transform(rows, new Function%3CPtObjectObject, String%3E() %7B%0A            @Override%0A            public String apply(PtObjectObject row) %7B%0A                return getObjectVertexId(row.getChildObjectId());%0A            %7D%0A        %7D);%0A        Map%3CString, Vertex%3E destVertices = getVertexCacheVertices(destVertexIds);%0A%0A        for (PtObjectObject row : rows) %7B%0A    
+getObjectVertexId(row.getChildObjectId());%0A%0A
     
@@ -973,20 +973,16 @@
         
-   
- 
 if (ptLi
@@ -995,28 +995,24 @@
  == null) %7B%0A
-    
             
@@ -1092,26 +1092,18 @@
         
-  
-  %7D%0A    
+%7D%0A
         
@@ -1165,239 +1165,8 @@
 );%0A%0A
-            String destObjectId = getObjectVertexId(row.getChildObjectId());%0A            Vertex destVertex = destVertices.get(destObjectId);%0A            checkNotNull(destVertex, %22Could not find dest vertex: %22 + destObjectId);%0A%0A    
     
@@ -1205,28 +1205,24 @@
 w);%0A        
-  
-  
 getDataImpor
@@ -1266,16 +1266,18 @@
 ceVertex
+Id
 , destVe
@@ -1280,16 +1280,18 @@
 stVertex
+Id
 , linkTy
@@ -1376,154 +1376,8 @@
 ));%0A
-        %7D%0A    %7D%0A%0A    @Override%0A    protected Long getGroupKey(PtObjectObject ptObjectObject) %7B%0A        return ptObjectObject.getParentObjectId();%0A
     

change to using the new addEdge using vertex ids