Skip to content

Commit

Permalink
Merge branch 'space-templates'
Browse files Browse the repository at this point in the history
  • Loading branch information
HarveyKandola committed Aug 21, 2017
2 parents 81cd22e + 5a2c447 commit 07c8238
Show file tree
Hide file tree
Showing 19 changed files with 322 additions and 113 deletions.
29 changes: 11 additions & 18 deletions domain/conversion/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,6 @@ func processDocument(ctx domain.RequestContext, r *env.Runtime, store *domain.St
return
}

//err = processPage(documentID, fileResult.PageFiles, fileResult.Pages.Children[0], 1, p)

for k, v := range fileResult.Pages {
var p page.Page
p.OrgID = ctx.OrgID
Expand All @@ -196,7 +194,6 @@ func processDocument(ctx domain.RequestContext, r *env.Runtime, store *domain.St
model.Meta = meta

err = store.Page.Add(ctx, model)

if err != nil {
ctx.Transaction.Rollback()
err = errors.Wrap(err, "cannot insert new page for new document")
Expand All @@ -216,15 +213,24 @@ func processDocument(ctx domain.RequestContext, r *env.Runtime, store *domain.St
a.RefID = refID

err = store.Attachment.Add(ctx, a)

if err != nil {
ctx.Transaction.Rollback()
err = errors.Wrap(err, "cannot insert attachment for new document")
return
}
}

ctx.Transaction.Commit()
store.Activity.RecordUserActivity(ctx, activity.UserActivity{
LabelID: newDocument.LabelID,
SourceID: newDocument.RefID,
SourceType: activity.SourceTypeDocument,
ActivityType: activity.TypeCreated})

err = ctx.Transaction.Commit()
if err != nil {
err = errors.Wrap(err, "cannot commit new document import")
return
}

newDocument, err = store.Document.Get(ctx, documentID)
if err != nil {
Expand All @@ -233,19 +239,6 @@ func processDocument(ctx domain.RequestContext, r *env.Runtime, store *domain.St
return
}

// err = store.Document.Update(ctx, newDocument) // TODO review - this seems to write-back an unaltered record from that read above, but within that it calls searches.UpdateDocument() to reindex the doc.
// if err != nil {
// ctx.Transaction.Rollback()
// err = errors.Wrap(err, "cannot updater new document")
// return
// }

store.Activity.RecordUserActivity(ctx, activity.UserActivity{
LabelID: newDocument.LabelID,
SourceID: newDocument.RefID,
SourceType: activity.SourceTypeDocument,
ActivityType: activity.TypeCreated})

store.Audit.Record(ctx, audit.EventTypeDocumentUpload)

return
Expand Down
11 changes: 11 additions & 0 deletions domain/document/mysql/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
package mysql

import (
"database/sql"
"fmt"
"time"

Expand Down Expand Up @@ -199,6 +200,11 @@ func (s Scope) TemplatesBySpace(ctx domain.RequestContext, spaceID string) (docu
ctx.OrgID,
ctx.UserID)

if err == sql.ErrNoRows {
err = nil
documents = []doc.Document{}
}

if err != nil {
err = errors.Wrap(err, "select space document templates")
return
Expand Down Expand Up @@ -241,6 +247,11 @@ func (s Scope) DocumentList(ctx domain.RequestContext) (documents []doc.Document
ctx.OrgID,
ctx.UserID)

if err == sql.ErrNoRows {
err = nil
documents = []doc.Document{}
}

if err != nil {
err = errors.Wrap(err, "select documents list")
return
Expand Down
40 changes: 22 additions & 18 deletions domain/link/mysql/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package mysql

import (
"fmt"
"strings"
"time"

"github.com/documize/community/core/env"
Expand Down Expand Up @@ -162,15 +163,18 @@ func (s Scope) DeleteLink(ctx domain.RequestContext, id string) (rows int64, err
// SearchCandidates returns matching documents, sections and attachments using keywords.
func (s Scope) SearchCandidates(ctx domain.RequestContext, keywords string) (docs []link.Candidate,
pages []link.Candidate, attachments []link.Candidate, err error) {

// find matching documents
temp := []link.Candidate{}
likeQuery := "title LIKE '%" + keywords + "%'"
keywords = strings.TrimSpace(strings.ToLower(keywords))
likeQuery := "LOWER(title) LIKE '%" + keywords + "%'"

err = s.Runtime.Db.Select(&temp,
`SELECT refid as documentid, labelid as folderid,title from document WHERE orgid=? AND `+likeQuery+` AND labelid IN
(SELECT refid from label WHERE orgid=? AND type=2 AND userid=?
UNION ALL SELECT refid FROM label a where orgid=? AND type=1 AND refid IN (SELECT labelid from labelrole WHERE orgid=? AND userid='' AND (canedit=1 OR canview=1))
UNION ALL SELECT refid FROM label a where orgid=? AND type=3 AND refid IN (SELECT labelid from labelrole WHERE orgid=? AND userid=? AND (canedit=1 OR canview=1)))
`SELECT d.refid as documentid, d. labelid as folderid, d.title, l.label as context
FROM document d LEFT JOIN label l ON d.labelid=l.refid WHERE l.orgid=? AND `+likeQuery+` AND d.labelid IN
(SELECT refid FROM label WHERE orgid=? AND type=2 AND userid=?
UNION ALL SELECT refid FROM label a WHERE orgid=? AND type=1 AND refid IN (SELECT labelid FROM labelrole WHERE orgid=? AND userid='' AND (canedit=1 OR canview=1))
UNION ALL SELECT refid FROM label a WHERE orgid=? AND type=3 AND refid IN (SELECT labelid FROM labelrole WHERE orgid=? AND userid=? AND (canedit=1 OR canview=1)))
ORDER BY title`,
ctx.OrgID,
ctx.OrgID,
Expand All @@ -194,22 +198,22 @@ func (s Scope) SearchCandidates(ctx domain.RequestContext, keywords string) (doc
TargetID: r.DocumentID,
LinkType: "document",
Title: r.Title,
Context: "",
Context: r.Context,
}

docs = append(docs, c)
}

// find matching sections
likeQuery = "p.title LIKE '%" + keywords + "%'"
likeQuery = "LOWER(p.title) LIKE '%" + keywords + "%'"
temp = []link.Candidate{}

err = s.Runtime.Db.Select(&temp,
`SELECT p.refid as targetid, p.documentid as documentid, p.title as title, p.pagetype as linktype, d.title as context, d.labelid as folderid from page p
LEFT JOIN document d ON d.refid=p.documentid WHERE p.orgid=? AND `+likeQuery+` AND d.labelid IN
(SELECT refid from label WHERE orgid=? AND type=2 AND userid=?
UNION ALL SELECT refid FROM label a where orgid=? AND type=1 AND refid IN (SELECT labelid from labelrole WHERE orgid=? AND userid='' AND (canedit=1 OR canview=1))
UNION ALL SELECT refid FROM label a where orgid=? AND type=3 AND refid IN (SELECT labelid from labelrole WHERE orgid=? AND userid=? AND (canedit=1 OR canview=1)))
`SELECT p.refid as targetid, p.documentid as documentid, p.title as title, p.pagetype as linktype, d.title as context, d.labelid as folderid
FROM page p LEFT JOIN document d ON d.refid=p.documentid WHERE p.orgid=? AND `+likeQuery+` AND d.labelid IN
(SELECT refid FROM label WHERE orgid=? AND type=2 AND userid=?
UNION ALL SELECT refid FROM label a WHERE orgid=? AND type=1 AND refid IN (SELECT labelid FROM labelrole WHERE orgid=? AND userid='' AND (canedit=1 OR canview=1))
UNION ALL SELECT refid FROM label a WHERE orgid=? AND type=3 AND refid IN (SELECT labelid FROM labelrole WHERE orgid=? AND userid=? AND (canedit=1 OR canview=1)))
ORDER BY p.title`,
ctx.OrgID,
ctx.OrgID,
Expand Down Expand Up @@ -240,15 +244,15 @@ func (s Scope) SearchCandidates(ctx domain.RequestContext, keywords string) (doc
}

// find matching attachments
likeQuery = "a.filename LIKE '%" + keywords + "%'"
likeQuery = "LOWER(a.filename) LIKE '%" + keywords + "%'"
temp = []link.Candidate{}

err = s.Runtime.Db.Select(&temp,
`SELECT a.refid as targetid, a.documentid as documentid, a.filename as title, a.extension as context, d.labelid as folderid from attachment a
LEFT JOIN document d ON d.refid=a.documentid WHERE a.orgid=? AND `+likeQuery+` AND d.labelid IN
(SELECT refid from label WHERE orgid=? AND type=2 AND userid=?
UNION ALL SELECT refid FROM label a where orgid=? AND type=1 AND refid IN (SELECT labelid from labelrole WHERE orgid=? AND userid='' AND (canedit=1 OR canview=1))
UNION ALL SELECT refid FROM label a where orgid=? AND type=3 AND refid IN (SELECT labelid from labelrole WHERE orgid=? AND userid=? AND (canedit=1 OR canview=1)))
`SELECT a.refid as targetid, a.documentid as documentid, a.filename as title, a.extension as context, d.labelid as folderid
FROM attachment a LEFT JOIN document d ON d.refid=a.documentid WHERE a.orgid=? AND `+likeQuery+` AND d.labelid IN
(SELECT refid FROM label WHERE orgid=? AND type=2 AND userid=?
UNION ALL SELECT refid FROM label a WHERE orgid=? AND type=1 AND refid IN (SELECT labelid FROM labelrole WHERE orgid=? AND userid='' AND (canedit=1 OR canview=1))
UNION ALL SELECT refid FROM label a WHERE orgid=? AND type=3 AND refid IN (SELECT labelid FROM labelrole WHERE orgid=? AND userid=? AND (canedit=1 OR canview=1)))
ORDER BY a.filename`,
ctx.OrgID,
ctx.OrgID,
Expand Down
156 changes: 153 additions & 3 deletions domain/space/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ import (
"github.com/documize/community/domain/mail"
"github.com/documize/community/model/account"
"github.com/documize/community/model/audit"
"github.com/documize/community/model/doc"
"github.com/documize/community/model/page"
"github.com/documize/community/model/space"
"github.com/documize/community/model/user"
uuid "github.com/nu7hatch/gouuid"
)

// Handler contains the runtime information such as logging and database.
Expand Down Expand Up @@ -65,15 +68,17 @@ func (h *Handler) Add(w http.ResponseWriter, r *http.Request) {
return
}

var sp = space.Space{}
err = json.Unmarshal(body, &sp)
var model = space.NewSpaceRequest{}
err = json.Unmarshal(body, &model)
if err != nil {
response.WriteServerError(w, method, err)
h.Runtime.Log.Error(method, err)
return
}

if len(sp.Name) == 0 {
model.Name = strings.TrimSpace(model.Name)
model.CloneID = strings.TrimSpace(model.CloneID)
if len(model.Name) == 0 {
response.WriteMissingDataError(w, method, "name")
return
}
Expand All @@ -85,6 +90,8 @@ func (h *Handler) Add(w http.ResponseWriter, r *http.Request) {
return
}

var sp space.Space
sp.Name = model.Name
sp.RefID = uniqueid.Generate()
sp.OrgID = ctx.OrgID
sp.Type = space.ScopePrivate
Expand Down Expand Up @@ -118,8 +125,151 @@ func (h *Handler) Add(w http.ResponseWriter, r *http.Request) {

h.Store.Audit.Record(ctx, audit.EventTypeSpaceAdd)

// Get back new space
sp, _ = h.Store.Space.Get(ctx, sp.RefID)

// clone existing space?
if model.CloneID != "" && (model.CopyDocument || model.CopyPermission || model.CopyTemplate) {
ctx.Transaction, err = h.Runtime.Db.Beginx()
if err != nil {
response.WriteServerError(w, method, err)
h.Runtime.Log.Error(method, err)
return
}

spCloneRoles, err := h.Store.Space.GetRoles(ctx, model.CloneID)
if err != nil {
response.WriteServerError(w, method, err)
h.Runtime.Log.Error(method, err)
return
}

if model.CopyPermission {
for _, r := range spCloneRoles {
r.RefID = uniqueid.Generate()
r.LabelID = sp.RefID

err = h.Store.Space.AddRole(ctx, r)
if err != nil {
ctx.Transaction.Rollback()
response.WriteServerError(w, method, err)
h.Runtime.Log.Error(method, err)
return
}
}
}

toCopy := []doc.Document{}
spCloneTemplates, err := h.Store.Document.TemplatesBySpace(ctx, model.CloneID)
if err != nil {
response.WriteServerError(w, method, err)
h.Runtime.Log.Error(method, err)
return
}
toCopy = append(toCopy, spCloneTemplates...)

if model.CopyDocument {
docs, err := h.Store.Document.GetBySpace(ctx, model.CloneID)

if err != nil {
ctx.Transaction.Rollback()
response.WriteServerError(w, method, err)
h.Runtime.Log.Error(method, err)
return
}

toCopy = append(toCopy, docs...)
}

if len(toCopy) > 0 {
for _, t := range toCopy {
origID := t.RefID

documentID := uniqueid.Generate()
t.RefID = documentID
t.LabelID = sp.RefID

err = h.Store.Document.Add(ctx, t)
if err != nil {
ctx.Transaction.Rollback()
response.WriteServerError(w, method, err)
h.Runtime.Log.Error(method, err)
return
}

pages, _ := h.Store.Page.GetPages(ctx, origID)
for _, p := range pages {
meta, err2 := h.Store.Page.GetPageMeta(ctx, p.RefID)
if err2 != nil {
ctx.Transaction.Rollback()
response.WriteServerError(w, method, err)
h.Runtime.Log.Error(method, err)
return
}

p.DocumentID = documentID
pageID := uniqueid.Generate()
p.RefID = pageID

meta.PageID = pageID
meta.DocumentID = documentID

model := page.NewPage{}
model.Page = p
model.Meta = meta

err = h.Store.Page.Add(ctx, model)

if err != nil {
ctx.Transaction.Rollback()
response.WriteServerError(w, method, err)
h.Runtime.Log.Error(method, err)
return
}
}

newUUID, _ := uuid.NewV4()
attachments, _ := h.Store.Attachment.GetAttachmentsWithData(ctx, origID)
for _, a := range attachments {
attachmentID := uniqueid.Generate()
a.RefID = attachmentID
a.DocumentID = documentID
a.Job = newUUID.String()
random := secrets.GenerateSalt()
a.FileID = random[0:9]

err = h.Store.Attachment.Add(ctx, a)
if err != nil {
ctx.Transaction.Rollback()
response.WriteServerError(w, method, err)
h.Runtime.Log.Error(method, err)
return
}
}
}
}

if model.CopyTemplate {
blocks, err := h.Store.Block.GetBySpace(ctx, model.CloneID)

for _, b := range blocks {
b.RefID = uniqueid.Generate()
b.LabelID = sp.RefID
b.UserID = ctx.UserID

err = h.Store.Block.Add(ctx, b)
if err != nil {
ctx.Transaction.Rollback()
response.WriteServerError(w, method, err)
h.Runtime.Log.Error(method, err)
return
}
}
}

ctx.Transaction.Commit()
}

response.WriteJSON(w, sp)
}

Expand Down
Loading

0 comments on commit 07c8238

Please sign in to comment.