From 7c2051cc7af758b4455311d37eae7f7b0e12401b Mon Sep 17 00:00:00 2001 From: Harvey Kandola Date: Thu, 27 Oct 2016 16:53:36 -0700 Subject: [PATCH] completed red-line links --- app/app/components/document/document-view.js | 4 +- core/api/entity/objects.go | 2 +- core/api/request/attachment.go | 7 ++- core/api/request/link.go | 48 ++++++++++++++++---- core/api/request/page.go | 19 +++++++- core/api/util/links.go | 2 +- core/database/scripts/autobuild/db_00000.sql | 2 +- core/database/scripts/autobuild/db_00004.sql | 2 +- 8 files changed, 70 insertions(+), 16 deletions(-) diff --git a/app/app/components/document/document-view.js b/app/app/components/document/document-view.js index 526e9cd2..d18f5079 100644 --- a/app/app/components/document/document-view.js +++ b/app/app/components/document/document-view.js @@ -70,7 +70,7 @@ export default Ember.Component.extend(NotifierMixin, TooltipMixin, { let doc = this.get('document'); let self = this; - $("a[data-documize='true']").off('click').on('click', function() { + $("a[data-documize='true']").off('click').on('click', function(e) { let link = links.getLinkObject(self.get('meta.outboundLinks'), this); // local link? exists? @@ -88,6 +88,8 @@ export default Ember.Component.extend(NotifierMixin, TooltipMixin, { if (link.orphan) { $(this).addClass('broken-link'); self.showNotification('Broken link!'); + e.preventDefault(); + e.stopPropagation(); return false; } diff --git a/core/api/entity/objects.go b/core/api/entity/objects.go index a50cb353..f694c8fd 100644 --- a/core/api/entity/objects.go +++ b/core/api/entity/objects.go @@ -355,7 +355,7 @@ type Link struct { SourceDocumentID string `json:"sourceDocumentId"` SourcePageID string `json:"sourcePageId"` TargetDocumentID string `json:"targetDocumentId"` - TargetPageID string `json:"targetPageId"` + TargetID string `json:"targetId"` Orphan bool `json:"orphan"` } diff --git a/core/api/request/attachment.go b/core/api/request/attachment.go index ac18e286..4046219c 100644 --- a/core/api/request/attachment.go +++ b/core/api/request/attachment.go @@ -102,5 +102,10 @@ func (p *Persister) GetAttachmentsWithData(docID string) (attachments []entity.A // DeleteAttachment deletes the id record from the database attachment table. func (p *Persister) DeleteAttachment(id string) (rows int64, err error) { - return p.Base.DeleteConstrained(p.Context.Transaction, "attachment", p.Context.OrgID, id) + rows, err = p.Base.DeleteConstrained(p.Context.Transaction, "attachment", p.Context.OrgID, id) + + // Mark references to this document as orphaned + err = p.MarkOrphanAttachmentLink(id) + + return } diff --git a/core/api/request/link.go b/core/api/request/link.go index e44204f3..e9be55ea 100644 --- a/core/api/request/link.go +++ b/core/api/request/link.go @@ -27,7 +27,7 @@ func (p *Persister) AddContentLink(l entity.Link) (err error) { l.Created = time.Now().UTC() l.Revised = time.Now().UTC() - stmt, err := p.Context.Transaction.Preparex("INSERT INTO link (refid, orgid, folderid, userid, sourcedocumentid, sourcepageid, targetdocumentid, targetpageid, linktype, created, revised) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)") + stmt, err := p.Context.Transaction.Preparex("INSERT INTO link (refid, orgid, folderid, userid, sourcedocumentid, sourcepageid, targetdocumentid, targetid, linktype, orphan, created, revised) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)") defer utility.Close(stmt) if err != nil { @@ -35,7 +35,7 @@ func (p *Persister) AddContentLink(l entity.Link) (err error) { return } - _, err = stmt.Exec(l.RefID, l.OrgID, l.FolderID, l.UserID, l.SourceDocumentID, l.SourcePageID, l.TargetDocumentID, l.TargetPageID, l.LinkType, l.Created, l.Revised) + _, err = stmt.Exec(l.RefID, l.OrgID, l.FolderID, l.UserID, l.SourceDocumentID, l.SourcePageID, l.TargetDocumentID, l.TargetID, l.LinkType, l.Orphan, l.Created, l.Revised) if err != nil { log.Error("Unable to execute insert for link", err) @@ -185,7 +185,7 @@ func (p *Persister) GetDocumentOutboundLinks(documentID string) (links []entity. err = nil err = Db.Select(&links, - `select l.refid, l.orgid, l.folderid, l.userid, l.sourcedocumentid, l.sourcepageid, l.targetdocumentid, l.targetpageid, l.linktype, l.orphan, l.created, l.revised + `select l.refid, l.orgid, l.folderid, l.userid, l.sourcedocumentid, l.sourcepageid, l.targetdocumentid, l.targetid, l.linktype, l.orphan, l.created, l.revised FROM link l WHERE l.orgid=? AND l.sourcedocumentid=?`, p.Context.OrgID, @@ -202,6 +202,29 @@ func (p *Persister) GetDocumentOutboundLinks(documentID string) (links []entity. return } +// GetPageLinks returns outbound links for specified page in document. +func (p *Persister) GetPageLinks(documentID, pageID string) (links []entity.Link, err error) { + err = nil + + err = Db.Select(&links, + `select l.refid, l.orgid, l.folderid, l.userid, l.sourcedocumentid, l.sourcepageid, l.targetdocumentid, l.targetid, l.linktype, l.orphan, l.created, l.revised + FROM link l + WHERE l.orgid=? AND l.sourcedocumentid=? AND l.sourcepageid=?`, + p.Context.OrgID, + documentID, + pageID) + + if err != nil { + return + } + + if len(links) == 0 { + links = []entity.Link{} + } + + return +} + // MarkOrphanDocumentLink marks all link records referencing specified document. func (p *Persister) MarkOrphanDocumentLink(documentID string) (err error) { revised := time.Now().UTC() @@ -216,10 +239,6 @@ func (p *Persister) MarkOrphanDocumentLink(documentID string) (err error) { _, err = stmt.Exec(revised, p.Context.OrgID, documentID) - if err != nil { - return - } - return } @@ -227,7 +246,7 @@ func (p *Persister) MarkOrphanDocumentLink(documentID string) (err error) { func (p *Persister) MarkOrphanPageLink(pageID string) (err error) { revised := time.Now().UTC() - stmt, err := p.Context.Transaction.Preparex("UPDATE link SET orphan=1, revised=? WHERE linktype='section' AND orgid=? AND targetpageid=?") + stmt, err := p.Context.Transaction.Preparex("UPDATE link SET orphan=1, revised=? WHERE linktype='section' AND orgid=? AND targetid=?") if err != nil { return @@ -237,10 +256,23 @@ func (p *Persister) MarkOrphanPageLink(pageID string) (err error) { _, err = stmt.Exec(revised, p.Context.OrgID, pageID) + return +} + +// MarkOrphanAttachmentLink marks all link records referencing specified attachment. +func (p *Persister) MarkOrphanAttachmentLink(attachmentID string) (err error) { + revised := time.Now().UTC() + + stmt, err := p.Context.Transaction.Preparex("UPDATE link SET orphan=1, revised=? WHERE linktype='file' AND orgid=? AND targetid=?") + if err != nil { return } + defer utility.Close(stmt) + + _, err = stmt.Exec(revised, p.Context.OrgID, attachmentID) + return } diff --git a/core/api/request/page.go b/core/api/request/page.go index d4170a1f..b6abf765 100644 --- a/core/api/request/page.go +++ b/core/api/request/page.go @@ -290,6 +290,10 @@ func (p *Persister) UpdatePage(page entity.Page, refID, userID string, skipRevis // find any content links in the HTML links := util.GetContentLinks(page.Body) + // get a copy of previously saved links + previousLinks, _ := p.GetPageLinks(page.DocumentID, page.RefID) + fmt.Println(len(previousLinks)) + // delete previous content links for this page _, _ = p.DeleteSourcePageLinks(page.RefID) @@ -299,11 +303,22 @@ func (p *Persister) UpdatePage(page entity.Page, refID, userID string, skipRevis link.OrgID = p.Context.OrgID link.UserID = p.Context.UserID link.SourceDocumentID = page.DocumentID + link.SourcePageID = page.RefID - if link.LinkType == "section" { - link.SourcePageID = page.RefID + if link.LinkType == "document" { + link.TargetID = "" } + // We check if there was a previously saved version of this link. + // If we find one, we carry forward the orphan flag. + for _, p := range previousLinks { + if link.TargetID == p.TargetID && link.LinkType == p.LinkType { + link.Orphan = p.Orphan + break + } + } + + // save err := p.AddContentLink(link) if err != nil { diff --git a/core/api/util/links.go b/core/api/util/links.go index 93c32e38..85bdeed5 100644 --- a/core/api/util/links.go +++ b/core/api/util/links.go @@ -65,7 +65,7 @@ func getLink(t html.Token) (ok bool, link entity.Link) { case "data-link-target-document-id": link.TargetDocumentID = strings.TrimSpace(a.Val) case "data-link-target-id": - link.TargetPageID = strings.TrimSpace(a.Val) + link.TargetID = strings.TrimSpace(a.Val) case "data-link-type": link.LinkType = strings.TrimSpace(a.Val) } diff --git a/core/database/scripts/autobuild/db_00000.sql b/core/database/scripts/autobuild/db_00000.sql index a489768d..7e82db8c 100644 --- a/core/database/scripts/autobuild/db_00000.sql +++ b/core/database/scripts/autobuild/db_00000.sql @@ -325,7 +325,7 @@ CREATE TABLE IF NOT EXISTS `link` ( `sourcepageid` CHAR(16) NOT NULL COLLATE utf8_bin, `linktype` CHAR(16) NOT NULL COLLATE utf8_bin, `targetdocumentid` CHAR(16) NOT NULL COLLATE utf8_bin, - `targetpageid` CHAR(16) DEFAULT '' COLLATE utf8_bin, + `targetid` CHAR(16) NOT NULL DEFAULT '' COLLATE utf8_bin, `orphan` BOOL NOT NULL DEFAULT 0, `created` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, `revised` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, diff --git a/core/database/scripts/autobuild/db_00004.sql b/core/database/scripts/autobuild/db_00004.sql index 0e1c7d07..d2245fb7 100644 --- a/core/database/scripts/autobuild/db_00004.sql +++ b/core/database/scripts/autobuild/db_00004.sql @@ -11,7 +11,7 @@ CREATE TABLE IF NOT EXISTS `link` ( `sourcepageid` CHAR(16) NOT NULL COLLATE utf8_bin, `linktype` CHAR(16) NOT NULL COLLATE utf8_bin, `targetdocumentid` CHAR(16) NOT NULL COLLATE utf8_bin, - `targetpageid` CHAR(16) DEFAULT '' COLLATE utf8_bin, + `targetid` CHAR(16) NOT NULL DEFAULT '' COLLATE utf8_bin, `orphan` BOOL NOT NULL DEFAULT 0, `created` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, `revised` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,