Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 148 lines (116 sloc) 6.146 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
"use strict"

fs = require "fs"
orient = require "orientdb"
_s = require "underscore.string"
assert = require "assert"

server = new orient.Server
  host: "localhost"
  port: 2424

db = new orient.GraphDb "presentz", server,
  user_name: "admin"
  user_password: "admin"

db.open (err) ->
  assert(!err, err)

  create_schema () ->
    db.createVertex { _type: "root" }, (err, root) ->
      assert(!err, err)
      user =
        _type: "user"
        name: "Federico Fissore"
        user_name: "fridrik"
        twitter_id: 1861911
        link: "https://twitter.com/fridrik"
        email: "federico@fsfe.org"

      db.createVertex user, (err, user) ->
        assert(!err, err)
        db.createEdge root, user, { label: "user" }, (err, edge) ->
          assert(!err, err)
          catalogs = [ "demo", "iad11", "jugtorino", "codemotion12" , "presentations" ]

          load_presentations_for= (user, catalogs) ->
            return db.close() if catalogs.length is 0

            catalog_folder = catalogs.pop()
            fs.readdir catalog_folder, (err, files) ->
              assert(!err, err)
              link_user_to_pres= (user, catalog, presentations) ->
                return load_presentations_for user, catalogs if presentations.length is 0

                presentation = presentations.pop()
                chapters = presentation.chapters
                delete presentation.chapters

                db.createVertex presentation, (err, presentation) ->
                  assert(!err, err)
                  db.createEdge user, presentation, { label: "authored" }, (err) ->
                    assert(!err, err)
                    db.createEdge presentation, catalog, { label: "part_of" }, (err) ->
                      assert(!err, err)

                      link_chapters_to_pres= (chapters, presentation) ->
                        return link_user_to_pres user, catalog, presentations if chapters.length is 0

                        chapter = chapters.pop()
                        chapter.video = chapter.media.video
                        slides = chapter.media.slides
                        delete chapter.media

                        db.createVertex chapter, (err, chapter) ->
                          assert(!err, err)
                          db.createEdge chapter, presentation, { label: "chapter_of" }, (err) ->
                            assert(!err, err)
                            link_slides_to_chapter= (slides, chapter) ->
                              return link_chapters_to_pres chapters, presentation if slides.length is 0

                              slide = slides.pop()

                              db.createVertex slide, (err, slide) ->
                                assert(!err, err)
                                db.createEdge slide, chapter, { label: "slide_of" }, (err) ->
                                  assert(!err, err)
                                  link_slides_to_chapter slides, chapter

                            link_slides_to_chapter slides, chapter

                      link_chapters_to_pres chapters, presentation

              fs.readFile "#{catalog_folder}/catalog.json", "utf-8", (err, catalog) ->
                assert(!err, err)
                catalog = JSON.parse catalog
                catalog._type = "catalog"
                catalog.id = catalog_folder

                db.createVertex catalog, (err, catalog) ->
                  assert(!err, err)
                  db.createEdge root, catalog, { label: "catalog" }, (err) ->
                    assert(!err, err)
                    db.createEdge user, catalog, { label: "admin_of" }, (err) ->
                      assert(!err, err)
                      presentations_files = (file for file in files when !_s.startsWith(file, "catalog") and _s.endsWith(file, ".json"))
                      presentations = []
                      aliases = 0

                      make_presentation= (file, presentations) ->
                        fs.readFile "#{catalog_folder}/#{file}", "utf-8", (err, presentation) ->
                          assert(!err, err)
                          presentation = JSON.parse presentation
                          if presentation.alias_of?
                            aliases++
                          else
                            presentation._type = "presentation"
                            presentation.published = true
                            presentation.id = file.substr(0, file.indexOf(".json"))
                            for chapter_index in [0...presentation.chapters.length]
                              chapter = presentation.chapters[chapter_index]
                              chapter._type = "chapter"
                              chapter._index = chapter_index
                              for slide in chapter.media.slides
                                slide._type = "slide"
                            presentations.push presentation

                          link_user_to_pres(user, catalog, presentations) if (presentations.length + aliases) is presentations_files.length

                      make_presentation(file, presentations) for file in presentations_files

          load_presentations_for user, catalogs

create_schema = (callback) ->
  db.command "CREATE PROPERTY OGraphVertex._type STRING", (err) =>
    assert(!err, err)

    db.command "CREATE PROPERTY OGraphVertex.id STRING", (err) =>
      assert(!err, err)

      db.command "CREATE PROPERTY OGraphVertex.published BOOLEAN", (err) =>
        assert(!err, err)

        db.command "CREATE PROPERTY OGraphEdge.label STRING", (err) =>
          assert(!err, err)

          db.command "CREATE INDEX OGraphVertex._type NOTUNIQUE", (err) =>
            assert(!err, err)

            db.command "CREATE INDEX OGraphVertex.id NOTUNIQUE", (err) =>
              assert(!err, err)
  
              db.command "CREATE INDEX OGraphVertex.published NOTUNIQUE", (err) =>
                assert(!err, err)
  
                db.command "CREATE INDEX OGraphEdge.label NOTUNIQUE", (err) =>
                  assert(!err, err)
  
                  callback()
Something went wrong with that request. Please try again.