Get doclines from docstore when cloning project

This commit is contained in:
James Allen 2014-04-30 16:45:53 +01:00
parent 465b5ca9db
commit 82b56d0b4f
2 changed files with 62 additions and 36 deletions

View file

@ -3,6 +3,7 @@ projectEntityHandler = require('./ProjectEntityHandler')
projectLocator = require('./ProjectLocator')
projectOptionsHandler = require('./ProjectOptionsHandler')
DocumentUpdaterHandler = require("../DocumentUpdater/DocumentUpdaterHandler")
DocstoreManager = require "../Docstore/DocstoreManager"
Project = require("../../models/Project").Project
_ = require('underscore')
async = require('async')
@ -16,38 +17,48 @@ module.exports =
projectCreationHandler.createBlankProject owner._id, newProjectName, (err, newProject)->
return callback(err) if err?
projectLocator.findRootDoc {project:originalProject}, (err, originalRootDoc)->
projectOptionsHandler.setCompiler newProject._id, originalProject.compiler
return callback(err) if err?
DocstoreManager.getAllDocs originalProjectId, (err, docContentsArray) ->
return callback(err) if err?
setRootDoc = _.once (doc_id)->
projectEntityHandler.setRootDoc newProject, doc_id
docContents = {}
for docContent in docContentsArray
docContents[docContent._id] = docContent
copyDocs = (originalFolder, newParentFolder, callback)->
jobs = originalFolder.docs.map (doc)->
return (callback)->
projectEntityHandler.addDoc newProject, newParentFolder._id, doc.name, doc.lines, (err, newDoc)->
if originalRootDoc? and newDoc.name == originalRootDoc.name
setRootDoc newDoc._id
callback()
async.series jobs, callback
projectOptionsHandler.setCompiler newProject._id, originalProject.compiler
copyFiles = (originalFolder, newParentFolder, callback)->
jobs = originalFolder.fileRefs.map (file)->
return (callback)->
projectEntityHandler.copyFileFromExistingProject newProject, newParentFolder._id, originalProject._id, file, callback
async.parallelLimit jobs, 5, callback
setRootDoc = _.once (doc_id)->
projectEntityHandler.setRootDoc newProject, doc_id
copyFolder = (folder, desFolder, callback)->
jobs = folder.folders.map (childFolder)->
return (callback)->
projectEntityHandler.addFolder newProject, desFolder._id, childFolder.name, (err, newFolder)->
copyFolder childFolder, newFolder, callback
jobs.push (cb)->
copyDocs folder, desFolder, cb
jobs.push (cb)->
copyFiles folder, desFolder, cb
copyDocs = (originalFolder, newParentFolder, callback)->
jobs = originalFolder.docs.map (doc)->
return (callback)->
content = docContents[doc._id.toString()]
return callback(new Error("doc_id not found: #{doc._id}")) if !content?
projectEntityHandler.addDoc newProject, newParentFolder._id, doc.name, content.lines, (err, newDoc)->
if originalRootDoc? and newDoc.name == originalRootDoc.name
setRootDoc newDoc._id
callback()
async.series jobs, callback
async.series jobs, callback
copyFiles = (originalFolder, newParentFolder, callback)->
jobs = originalFolder.fileRefs.map (file)->
return (callback)->
projectEntityHandler.copyFileFromExistingProject newProject, newParentFolder._id, originalProject._id, file, callback
async.parallelLimit jobs, 5, callback
copyFolder originalProject.rootFolder[0], newProject.rootFolder[0], ->
callback(err, newProject)
copyFolder = (folder, desFolder, callback)->
jobs = folder.folders.map (childFolder)->
return (callback)->
projectEntityHandler.addFolder newProject, desFolder._id, childFolder.name, (err, newFolder)->
copyFolder childFolder, newFolder, callback
jobs.push (cb)->
copyDocs folder, desFolder, cb
jobs.push (cb)->
copyFiles folder, desFolder, cb
async.series jobs, callback
copyFolder originalProject.rootFolder[0], newProject.rootFolder[0], ->
callback(err, newProject)

View file

@ -3,25 +3,25 @@ chai = require('chai').should()
modulePath = "../../../../app/js/Features/Project/ProjectDuplicator.js"
SandboxedModule = require('sandboxed-module')
describe 'duplicating a project', ->
describe 'ProjectDuplicator', ->
beforeEach ->
@level2folder =
name: "level2folderName"
_id:"level2folderId"
docs:[{name:"level2folderDocName",docLines:"level2Doc"}]
docs:[@doc2 = {_id: "doc2_id", name:"level2folderDocName"}]
folders:[]
fileRefs:[{name:"file2", _id:"file2"}]
@level1folder =
name:"level1folder"
_id:"level1folderId"
docs:[{name:"level1folderDocName",docLines:"level1Doc"}]
docs:[@doc1 = {_id: "doc1_id", name:"level1folderDocName"}]
folders:[@level2folder]
fileRefs:[{name:"file1", _id:"file1"}]
@rootFolder =
name:"rootFolder"
_id:"rootFolderId"
docs:[{name:"rootDocHere", docLines:["level0Doc"]}]
docs:[@doc0 = {_id: "doc0_id", name:"rootDocHere"}]
folders:[@level1folder]
fileRefs:[{name:"file0", _id:"file0"}]
@project =
@ -30,6 +30,19 @@ describe 'duplicating a project', ->
rootFolder:[@rootFolder]
compiler: "this_is_a_Compiler"
@docContents = [{
_id: @doc0._id
lines: @doc0_lines = ["zero"]
}, {
_id: @doc1._id
lines: @doc1_lines = ["one"]
}, {
_id: @doc2._id
lines: @doc2_lines = ["two"]
}]
@DocstoreManager =
getAllDocs: sinon.stub().callsArgWith(1, null, @docContents)
@owner = {_id:"this_is_the_owner"}
@stubbedNewProject =
_id:"new_project_id"
@ -69,6 +82,7 @@ describe 'duplicating a project', ->
'./ProjectEntityHandler': @entityHandler
'./ProjectLocator': @locator
'./ProjectOptionsHandler': @projectOptionsHandler
"../Docstore/DocstoreManager": @DocstoreManager
'logger-sharelatex':{log:->}
it "should look up the original project", (done) ->
@ -101,7 +115,7 @@ describe 'duplicating a project', ->
@duplicator.duplicate @owner, @project_id, "", (err, newProject)=>
@entityHandler.setRootDoc.calledWith(@stubbedNewProject, @rootFolder.docs[0]._id).should.equal true
done()
it 'should not copy the collaberators or read only refs', (done)->
@duplicator.duplicate @owner, @project_id, "", (err, newProject)=>
newProject.collaberator_refs.length.should.equal 0
@ -117,9 +131,10 @@ describe 'duplicating a project', ->
it 'should copy all the docs', (done)->
@duplicator.duplicate @owner, @project_id, "", (err, newProject)=>
@entityHandler.addDoc.calledWith(@stubbedNewProject, @stubbedNewProject.rootFolder[0]._id, @rootFolder.docs[0].name, @rootFolder.docs[0].lines).should.equal true
@entityHandler.addDoc.calledWith(@stubbedNewProject, @newFolder._id, @level1folder.docs[0].name, @level1folder.docs[0].lines).should.equal true
@entityHandler.addDoc.calledWith(@stubbedNewProject, @newFolder._id, @level2folder.docs[0].name, @level2folder.docs[0].lines).should.equal true
@DocstoreManager.getAllDocs.calledWith(@project_id).should.equal true
@entityHandler.addDoc.calledWith(@stubbedNewProject, @stubbedNewProject.rootFolder[0]._id, @doc0.name, @doc0_lines).should.equal true
@entityHandler.addDoc.calledWith(@stubbedNewProject, @newFolder._id, @doc1.name, @doc1_lines).should.equal true
@entityHandler.addDoc.calledWith(@stubbedNewProject, @newFolder._id, @doc2.name, @doc2_lines).should.equal true
done()
it 'should copy all the files', (done)->