2016-10-23 18:33:07 -07:00
|
|
|
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
|
|
|
//
|
|
|
|
// This software (Documize Community Edition) is licensed under
|
|
|
|
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
|
|
|
//
|
|
|
|
// You can operate outside the AGPL restrictions by purchasing
|
|
|
|
// Documize Enterprise Edition and obtaining a commercial license
|
|
|
|
// by contacting <sales@documize.com>.
|
|
|
|
//
|
|
|
|
// https://documize.com
|
|
|
|
|
|
|
|
package request
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/documize/community/core/api/entity"
|
|
|
|
"github.com/documize/community/core/log"
|
|
|
|
"github.com/documize/community/core/utility"
|
|
|
|
)
|
|
|
|
|
2016-10-25 15:56:08 -07:00
|
|
|
// AddContentLink inserts wiki-link into the store.
|
2016-10-23 18:33:07 -07:00
|
|
|
// These links exist when content references another document or content.
|
2016-10-25 15:56:08 -07:00
|
|
|
func (p *Persister) AddContentLink(l entity.Link) (err error) {
|
2016-10-23 18:33:07 -07:00
|
|
|
l.UserID = p.Context.UserID
|
|
|
|
l.Created = time.Now().UTC()
|
|
|
|
l.Revised = time.Now().UTC()
|
|
|
|
|
2016-10-26 17:31:05 -07:00
|
|
|
stmt, err := p.Context.Transaction.Preparex("INSERT INTO link (refid, orgid, folderid, userid, sourceid, documentid, targetid, linktype, created, revised) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
|
2016-10-23 18:33:07 -07:00
|
|
|
defer utility.Close(stmt)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to prepare insert for link", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-26 17:31:05 -07:00
|
|
|
_, err = stmt.Exec(l.RefID, l.OrgID, l.FolderID, l.UserID, l.SourceID, l.DocumentID, l.TargetID, l.LinkType, l.Created, l.Revised)
|
2016-10-23 18:33:07 -07:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to execute insert for link", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetReferencedLinks returns all links that the specified section is referencing.
|
|
|
|
func (p *Persister) GetReferencedLinks(sectionID string) (links []entity.Link, err error) {
|
|
|
|
err = nil
|
|
|
|
|
2016-10-26 17:31:05 -07:00
|
|
|
sql := "SELECT id,refid,orgid,folderid,userid,sourceid,documentid,targetid,linktype,orphan,created,revised from link WHERE orgid=? AND sourceid=?"
|
2016-10-23 18:33:07 -07:00
|
|
|
|
|
|
|
err = Db.Select(&links, sql, p.Context.OrgID, sectionID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(fmt.Sprintf("Unable to execute select links for org %s", p.Context.OrgID), err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-25 15:56:08 -07:00
|
|
|
// GetContentLinksForSection returns all links that are linking to the specified section.
|
|
|
|
func (p *Persister) GetContentLinksForSection(sectionID string) (links []entity.Link, err error) {
|
2016-10-23 18:33:07 -07:00
|
|
|
err = nil
|
|
|
|
|
2016-10-26 17:31:05 -07:00
|
|
|
sql := "SELECT id,refid,orgid,folderid,userid,sourceid,documentid,targetid,linktype,orphan,created,revised from link WHERE orgid=? AND sectionid=?"
|
2016-10-23 18:33:07 -07:00
|
|
|
|
|
|
|
err = Db.Select(&links, sql, p.Context.OrgID, sectionID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(fmt.Sprintf("Unable to execute select links for org %s", p.Context.OrgID), err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-25 15:56:08 -07:00
|
|
|
// GetContentLinksForDocument returns all links that are linking to the specified document.
|
|
|
|
func (p *Persister) GetContentLinksForDocument(documentID string) (links []entity.Link, err error) {
|
2016-10-23 18:33:07 -07:00
|
|
|
err = nil
|
|
|
|
|
2016-10-26 17:31:05 -07:00
|
|
|
sql := "SELECT id,refid,orgid,folderid,userid,sourceid,documentid,targetid,linktype,orphan,created,revised from link WHERE orgid=? AND documentid=?"
|
2016-10-23 18:33:07 -07:00
|
|
|
|
|
|
|
err = Db.Select(&links, sql, p.Context.OrgID, documentID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(fmt.Sprintf("Unable to execute select links for org %s", p.Context.OrgID), err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-25 15:56:08 -07:00
|
|
|
// MarkOrphanContentLink marks the link record as being invalid.
|
|
|
|
func (p *Persister) MarkOrphanContentLink(l entity.Link) (err error) {
|
2016-10-23 18:33:07 -07:00
|
|
|
l.Orphan = true
|
|
|
|
l.Revised = time.Now().UTC()
|
|
|
|
|
|
|
|
stmt, err := p.Context.Transaction.PrepareNamed("UPDATE link SET orphan=1 revised=:revised WHERE orgid=:orgid AND refid=:refid")
|
|
|
|
defer utility.Close(stmt)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(fmt.Sprintf("Unable to prepare update for link %s", l.RefID), err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = stmt.Exec(&l)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(fmt.Sprintf("Unable to execute update for link %s", l.RefID), err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-25 15:56:08 -07:00
|
|
|
// DeleteSourceLinks removes saved links for given source.
|
|
|
|
func (p *Persister) DeleteSourceLinks(sourceID string) (rows int64, err error) {
|
|
|
|
return p.Base.DeleteWhere(p.Context.Transaction, fmt.Sprintf("DELETE FROM link WHERE orgid=\"%s\" AND sourceid=\"%s\"", p.Context.OrgID, sourceID))
|
|
|
|
}
|
|
|
|
|
2016-10-23 18:33:07 -07:00
|
|
|
// DeleteLink removes saved link from the store.
|
|
|
|
func (p *Persister) DeleteLink(id string) (rows int64, err error) {
|
|
|
|
return p.Base.DeleteConstrained(p.Context.Transaction, "link", p.Context.OrgID, id)
|
|
|
|
}
|