       triplets.push triplet
     triplets
 
+
   extractData: (row) ->
     data =
       upvotes: row.upvoteCount

      endResourceConnectionCount: row.endResourceConnectionCount
    data

  findByResourceId: (resourceId, user, _) ->
    if user?
      userNodeId = user.node.id
    else
      userNodeId = 0

    query = """
            START thisResource=node({resourceNodeId}), user=node(#{userNodeId})
            MATCH (thisResource) -[:RELATED_TO]- (connection) -[:RELATED_TO]- (thatResource),
              (thisResource) -[:CREATED_BY]- (thisResourceCreator),
              (thatResource) -[:CREATED_BY]- (thatResourceCreator),
              (thatResourceConnections)-[?:RELATED_TO]-(thatResource),
              (connection) -[?:VOTED_UP]- (upvotes),
              (connection) -[?:VOTED_DOWN]- (downvotes),
              (connection) -[:CREATED_BY]- (connectionCreator),
              (user) -[hasVotedUp?:VOTED_UP]-> (connection),
              (user) -[hasVotedDown?:VOTED_DOWN]-> (connection),
              (connection) -[?:COMMENT_OF]- (comments)
            RETURN connection, connectionCreator, thisResource, thisResourceCreator, thatResource, thatResourceCreator, hasVotedUp, hasVotedDown,
              count(distinct comments) AS commentCount,
              count(distinct thatResourceConnections) AS thatResourceConnectionCount,
              count(distinct upvotes) AS upVoteCount,
              count(distinct downvotes) AS downVoteCount
            """
    resource = Resources.find(resourceId, _)
    params =
      resourceNodeId: resource.node.id

    results = GraphDB.get().query(query, params, _)
    triplets = []
    for row in results
      data =
        upvotes: row.upVoteCount
        downvotes: row.downVoteCount
        userUpvoted: row.hasVotedUp?
        userDownvoted: row.hasVotedDown?
        commentCount: row.commentCount
      connection = new Connection(row.connection, row.connectionCreator)
      thisResource = new Resource(row.thisResource, row.thisResourceCreator)
      thatResource = new Resource(row.thatResource, row.thatResourceCreator)

      startResource
      endResource

      if thisResource.id is connection.startResourceId and thatResource.id is connection.endResourceId
        startResource = thisResource
        endResource = thatResource
        data.startResourceConnectionCount = results.length
        data.endResourceConnectionCount = row.thatResourceConnectionCount
      else if thisResource.id is connection.endResourceId and thatResource.id is connection.startResourceId
        startResource = thatResource
        endResource = thisResource
        data.startResourceConnectionCount = row.thatResourceConnectionCount
        data.endResourceConnectionCount = results.length
      else
        throw 'Should never happen : one of those two conditions should have been true, there must be an error in the code (Triplets.findByResourceId())'
      triplet = new Triplet(connection, startResource, endResource, data)
      triplets.push triplet
    triplets




module.exports =


  findByUserId: (userId, loggedUserId, _) ->
    user = Users.find(userId, _)

    if loggedUserId?
