2017-07-24 16:24:21 +01: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
2018-09-26 17:59:56 +01:00
package document
2017-07-24 16:24:21 +01:00
import (
2017-08-21 17:51:06 +01:00
"database/sql"
2017-07-24 16:24:21 +01:00
"fmt"
2017-07-31 18:17:30 +01:00
"time"
2017-07-24 16:24:21 +01:00
"github.com/documize/community/domain"
2018-09-26 17:59:56 +01:00
"github.com/documize/community/domain/store"
2017-07-26 20:03:23 +01:00
"github.com/documize/community/model/doc"
2017-07-24 16:24:21 +01:00
"github.com/pkg/errors"
)
2018-09-26 17:59:56 +01:00
// Store provides data access to space category information.
type Store struct {
store . Context
domain . DocumentStorer
2017-07-26 10:50:26 +01:00
}
2017-07-31 18:17:30 +01:00
// Add inserts the given document record into the document table and audits that it has been done.
2018-09-26 17:59:56 +01:00
func ( s Store ) Add ( ctx domain . RequestContext , d doc . Document ) ( err error ) {
2018-03-14 13:38:37 +00:00
d . OrgID = ctx . OrgID
d . Created = time . Now ( ) . UTC ( )
d . Revised = d . Created // put same time in both fields
2017-07-31 18:17:30 +01:00
2018-09-26 17:59:56 +01:00
_ , err = ctx . Transaction . Exec ( s . Bind ( `
2018-09-20 11:12:17 +01:00
INSERT INTO dmz_doc ( c_refid , c_orgid , c_spaceid , c_userid , c_job , c_location , c_name , c_desc , c_slug , c_tags , c_template , c_protection , c_approval , c_lifecycle , c_versioned , c_versionid , c_versionorder , c_groupid , c_created , c_revised )
2018-09-26 17:59:56 +01:00
VALUES ( ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? ) ` ) ,
2018-09-18 20:55:40 +01:00
d . RefID , d . OrgID , d . SpaceID , d . UserID , d . Job , d . Location , d . Name , d . Excerpt , d . Slug , d . Tags ,
2018-03-14 13:38:37 +00:00
d . Template , d . Protection , d . Approval , d . Lifecycle , d . Versioned , d . VersionID , d . VersionOrder , d . GroupID , d . Created , d . Revised )
2017-07-31 18:17:30 +01:00
if err != nil {
2018-04-13 11:01:36 +01:00
err = errors . Wrap ( err , "execute insert document" )
2017-07-31 18:17:30 +01:00
}
return
}
2017-07-26 20:03:23 +01:00
// Get fetches the document record with the given id fromt the document table and audits that it has been got.
2018-09-26 17:59:56 +01:00
func ( s Store ) Get ( ctx domain . RequestContext , id string ) ( document doc . Document , err error ) {
err = s . Runtime . Db . Get ( & document , s . Bind ( `
2018-09-18 20:55:40 +01:00
SELECT id , c_refid AS refid , c_orgid AS orgid , c_spaceid AS spaceid , c_userid AS userid ,
c_job AS job , c_location AS location , c_name AS name , c_desc AS excerpt , c_slug AS slug ,
c_tags AS tags , c_template AS template , c_protection AS protection , c_approval AS approval ,
c_lifecycle AS lifecycle , c_versioned AS versioned , c_versionid AS versionid ,
c_versionorder AS versionorder , c_groupid AS groupid , c_created AS created , c_revised AS revised
FROM dmz_doc
2018-09-26 17:59:56 +01:00
WHERE c_orgid = ? and c_refid = ? ` ) ,
2017-09-25 14:37:11 +01:00
ctx . OrgID , id )
2017-07-26 20:03:23 +01:00
if err != nil {
err = errors . Wrap ( err , "execute select document" )
}
return
}
2017-09-26 16:30:16 +01:00
// GetBySpace returns a slice containing the documents for a given space.
2018-03-19 12:24:58 +00:00
//
// No attempt is made to hide documents that are protected by category
// permissions hence caller must filter as required.
//
// All versions of a document are returned, hence caller must
// decide what to do with them.
2018-09-26 17:59:56 +01:00
func ( s Store ) GetBySpace ( ctx domain . RequestContext , spaceID string ) ( documents [ ] doc . Document , err error ) {
2018-04-05 14:22:23 +01:00
documents = [ ] doc . Document { }
2018-09-26 17:59:56 +01:00
err = s . Runtime . Db . Select ( & documents , s . Bind ( `
2018-09-18 20:55:40 +01:00
SELECT id , c_refid AS refid , c_orgid AS orgid , c_spaceid AS spaceid , c_userid AS userid ,
c_job AS job , c_location AS location , c_name AS name , c_desc AS excerpt , c_slug AS slug ,
c_tags AS tags , c_template AS template , c_protection AS protection , c_approval AS approval ,
c_lifecycle AS lifecycle , c_versioned AS versioned , c_versionid AS versionid ,
c_versionorder AS versionorder , c_groupid AS groupid , c_created AS created , c_revised AS revised
FROM dmz_doc
2018-09-19 16:03:29 +01:00
WHERE c_orgid = ? AND c_template = 0 AND c_spaceid IN
2018-09-18 20:55:40 +01:00
( SELECT c_refid FROM dmz_space WHERE c_orgid = ? AND c_refid IN
( SELECT c_refid FROM dmz_permission WHERE c_orgid = ? AND c_location = ' space ' AND c_refid = ? AND c_refid IN
( SELECT c_refid from dmz_permission WHERE c_orgid = ? AND c_who = ' user ' AND ( c_whoid = ? OR c_whoid = '0' ) AND c_location = ' space ' AND c_action = ' view '
UNION ALL
2018-09-19 16:03:29 +01:00
SELECT p . c_refid from dmz_permission p LEFT JOIN dmz_group_member r ON p . c_whoid = r . c_groupid WHERE p . c_orgid = ?
AND p . c_who = ' role ' AND p . c_location = ' space ' AND p . c_refid = ? AND p . c_action = ' view ' AND ( r . c_userid = ? OR r . c_userid = '0' )
)
2018-09-18 20:55:40 +01:00
)
2018-09-19 16:03:29 +01:00
)
2018-09-26 17:59:56 +01:00
ORDER BY c_name , c_versionorder ` ) ,
2018-09-18 20:55:40 +01:00
ctx . OrgID , ctx . OrgID , ctx . OrgID , spaceID , ctx . OrgID , ctx . UserID , ctx . OrgID , spaceID , ctx . UserID )
2017-07-31 18:17:30 +01:00
2018-04-05 14:22:23 +01:00
if err == sql . ErrNoRows {
2018-01-10 16:07:17 +00:00
err = nil
}
2017-07-31 18:17:30 +01:00
if err != nil {
2017-09-26 16:30:16 +01:00
err = errors . Wrap ( err , "select documents by space" )
2017-07-31 18:17:30 +01:00
}
return
}
2017-08-17 09:37:33 +01:00
// TemplatesBySpace returns a slice containing the documents available as templates for given space.
2018-09-26 17:59:56 +01:00
func ( s Store ) TemplatesBySpace ( ctx domain . RequestContext , spaceID string ) ( documents [ ] doc . Document , err error ) {
err = s . Runtime . Db . Select ( & documents , s . Bind ( `
2018-09-18 20:55:40 +01:00
SELECT id , c_refid AS refid , c_orgid AS orgid , c_spaceid AS spaceid , c_userid AS userid ,
c_job AS job , c_location AS location , c_name AS name , c_desc AS excerpt , c_slug AS slug ,
c_tags AS tags , c_template AS template , c_protection AS protection , c_approval AS approval ,
c_lifecycle AS lifecycle , c_versioned AS versioned , c_versionid AS versionid ,
c_versionorder AS versionorder , c_groupid AS groupid , c_created AS created , c_revised AS revised
FROM dmz_doc
WHERE c_orgid = ? AND c_spaceid = ? AND c_template = 1 AND c_lifecycle = 1
AND c_spaceid IN
( SELECT c_refid FROM dmz_space WHERE c_orgid = ? AND c_refid IN
( SELECT c_refid FROM dmz_permission WHERE c_orgid = ? AND c_location = ' space ' AND c_refid IN
2018-09-19 16:03:29 +01:00
( SELECT c_refid FROM dmz_permission WHERE c_orgid = ? AND c_who = ' user ' AND ( c_whoid = ? OR c_whoid = '0' ) AND c_location = ' space ' AND c_action = ' view '
2017-09-18 13:02:15 +01:00
UNION ALL
2018-09-19 16:03:29 +01:00
SELECT p . c_refid FROM dmz_permission p LEFT JOIN dmz_group_member r ON p . c_whoid = r . c_groupid WHERE p . c_orgid = ? AND p . c_who = ' role ' AND p . c_location = ' space ' AND p . c_action = ' view ' AND ( r . c_userid = ? OR r . c_userid = '0' ) )
2018-09-18 20:55:40 +01:00
)
2017-09-18 13:02:15 +01:00
)
2018-09-26 17:59:56 +01:00
ORDER BY c_name ` ) ,
ctx . OrgID , spaceID , ctx . OrgID , ctx . OrgID , ctx . OrgID , ctx . UserID , ctx . OrgID , ctx . UserID )
2017-08-17 09:37:33 +01:00
2017-08-21 17:51:06 +01:00
if err == sql . ErrNoRows {
err = nil
documents = [ ] doc . Document { }
}
2017-08-17 09:37:33 +01:00
if err != nil {
err = errors . Wrap ( err , "select space document templates" )
2017-07-24 16:24:21 +01:00
}
return
}
2017-07-26 20:03:23 +01:00
2018-03-19 12:24:58 +00:00
// PublicDocuments returns a slice of SitemapDocument records
// linking to documents in public spaces.
// These documents can then be seen by search crawlers.
2018-09-26 17:59:56 +01:00
func ( s Store ) PublicDocuments ( ctx domain . RequestContext , orgID string ) ( documents [ ] doc . SitemapDocument , err error ) {
err = s . Runtime . Db . Select ( & documents , s . Bind ( `
2018-09-19 16:38:05 +01:00
SELECT d . c_refid AS documentid , d . c_name AS document , d . c_revised as revised , l . c_refid AS spaceid , l . c_name AS space
FROM dmz_doc d
2018-09-18 20:55:40 +01:00
LEFT JOIN dmz_space l ON l . c_refid = d . c_spaceid
2018-09-26 17:59:56 +01:00
WHERE d . c_orgid = ? AND l . c_type = 1 AND d . c_lifecycle = 1 AND d . c_template = 0 ` ) ,
orgID )
2017-07-26 20:03:23 +01:00
2018-03-14 13:38:37 +00:00
if err == sql . ErrNoRows {
err = nil
documents = [ ] doc . SitemapDocument { }
}
2017-07-26 20:03:23 +01:00
if err != nil {
2018-09-19 16:38:05 +01:00
err = errors . Wrap ( err , fmt . Sprintf ( "execute GetPublicDocuments for org %s" , orgID ) )
2017-07-26 20:03:23 +01:00
}
return
}
2017-07-31 18:17:30 +01:00
2018-09-19 16:38:05 +01:00
/ *
FROM document d LEFT JOIN label l ON l . refid = d . labelid
* /
2017-07-31 18:17:30 +01:00
// Update changes the given document record to the new values, updates search information and audits the action.
2018-09-26 17:59:56 +01:00
func ( s Store ) Update ( ctx domain . RequestContext , document doc . Document ) ( err error ) {
2017-07-31 18:17:30 +01:00
document . Revised = time . Now ( ) . UTC ( )
2018-09-26 17:59:56 +01:00
_ , err = ctx . Transaction . NamedExec ( s . Bind ( `
2018-09-18 20:55:40 +01:00
UPDATE dmz_doc SET
c_spaceid = : spaceid , c_userid = : userid , c_job = : job , c_location = : location , c_name = : name ,
c_desc = : excerpt , c_slug = : slug , c_tags = : tags , c_template = : template ,
c_protection = : protection , c_approval = : approval , c_lifecycle = : lifecycle ,
c_versioned = : versioned , c_versionid = : versionid , c_versionorder = : versionorder ,
c_groupid = : groupid , c_revised = : revised
2018-09-26 17:59:56 +01:00
WHERE c_orgid = : orgid AND c_refid = : refid ` ) ,
2017-09-25 14:37:11 +01:00
& document )
2017-07-31 18:17:30 +01:00
if err != nil {
2018-03-19 12:24:58 +00:00
err = errors . Wrap ( err , "document.store.Update" )
}
return
}
2018-09-18 20:55:40 +01:00
// UpdateGroup applies same values to all documents with the same group ID.
2018-09-26 17:59:56 +01:00
func ( s Store ) UpdateGroup ( ctx domain . RequestContext , d doc . Document ) ( err error ) {
_ , err = ctx . Transaction . Exec ( s . Bind ( ` UPDATE dmz_doc SET c_name=?, c_desc=? WHERE c_orgid=? AND c_groupid=? ` ) ,
2018-09-18 20:55:40 +01:00
d . Name , d . Excerpt , ctx . OrgID , d . GroupID )
2018-03-19 12:24:58 +00:00
if err == sql . ErrNoRows {
err = nil
}
if err != nil {
2018-09-20 17:23:26 +01:00
err = errors . Wrap ( err , "document.store.UpdateGroup" )
2017-07-31 18:17:30 +01:00
}
return
}
// ChangeDocumentSpace assigns the specified space to the document.
2018-09-26 17:59:56 +01:00
func ( s Store ) ChangeDocumentSpace ( ctx domain . RequestContext , document , space string ) ( err error ) {
2017-07-31 18:17:30 +01:00
revised := time . Now ( ) . UTC ( )
2018-09-26 17:59:56 +01:00
_ , err = ctx . Transaction . Exec ( s . Bind ( "UPDATE dmz_doc SET c_spaceid=?, c_revised=? WHERE c_orgid=? AND c_refid=?" ) ,
2017-09-25 14:37:11 +01:00
space , revised , ctx . OrgID , document )
2017-07-31 18:17:30 +01:00
if err != nil {
err = errors . Wrap ( err , fmt . Sprintf ( "execute change document space %s" , document ) )
}
return
}
// MoveDocumentSpace changes the space for client's organization's documents which have space "id", to "move".
2018-09-26 17:59:56 +01:00
func ( s Store ) MoveDocumentSpace ( ctx domain . RequestContext , id , move string ) ( err error ) {
_ , err = ctx . Transaction . Exec ( s . Bind ( "UPDATE dmz_doc SET c_spaceid=? WHERE c_orgid=? AND c_spaceid=?" ) ,
2017-09-25 14:37:11 +01:00
move , ctx . OrgID , id )
2017-07-31 18:17:30 +01:00
2018-04-05 20:00:42 +01:00
if err == sql . ErrNoRows {
err = nil
}
2017-07-31 18:17:30 +01:00
if err != nil {
err = errors . Wrap ( err , fmt . Sprintf ( "execute document space move %s" , id ) )
}
return
}
2018-04-05 20:00:42 +01:00
// MoveActivity changes the space for all document activity records.
2018-09-26 17:59:56 +01:00
func ( s Store ) MoveActivity ( ctx domain . RequestContext , documentID , oldSpaceID , newSpaceID string ) ( err error ) {
_ , err = ctx . Transaction . Exec ( s . Bind ( "UPDATE dmz_user_activity SET c_spaceid=? WHERE c_orgid=? AND c_spaceid=? AND c_docid=?" ) ,
2018-04-05 20:00:42 +01:00
newSpaceID , ctx . OrgID , oldSpaceID , documentID )
if err != nil {
err = errors . Wrap ( err , fmt . Sprintf ( "execute document activity move %s" , documentID ) )
}
return
}
2017-09-18 13:35:51 +01:00
// Delete removes the specified document.
// Remove document pages, revisions, attachments, updates the search subsystem.
2018-09-26 17:59:56 +01:00
func ( s Store ) Delete ( ctx domain . RequestContext , documentID string ) ( rows int64 , err error ) {
rows , err = s . DeleteWhere ( ctx . Transaction , fmt . Sprintf ( "DELETE FROM dmz_section WHERE c_docid=\"%s\" AND c_orgid=\"%s\"" , documentID , ctx . OrgID ) )
2017-07-31 18:17:30 +01:00
if err != nil {
return
}
2018-09-26 17:59:56 +01:00
_ , err = s . DeleteWhere ( ctx . Transaction , fmt . Sprintf ( "DELETE FROM dmz_section_revision WHERE c_docid=\"%s\" AND c_orgid=\"%s\"" , documentID , ctx . OrgID ) )
2017-07-31 18:17:30 +01:00
if err != nil {
return
}
2018-09-26 17:59:56 +01:00
_ , err = s . DeleteWhere ( ctx . Transaction , fmt . Sprintf ( "DELETE FROM dmz_doc_attachment WHERE c_docid=\"%s\" AND c_orgid=\"%s\"" , documentID , ctx . OrgID ) )
2017-07-31 18:17:30 +01:00
if err != nil {
return
}
2018-09-26 17:59:56 +01:00
_ , err = s . DeleteWhere ( ctx . Transaction , fmt . Sprintf ( "DELETE FROM dmz_category_member WHERE c_docid=\"%s\" AND c_orgid=\"%s\"" , documentID , ctx . OrgID ) )
2018-04-13 11:01:36 +01:00
if err != nil {
return
}
2018-09-26 17:59:56 +01:00
_ , err = s . DeleteWhere ( ctx . Transaction , fmt . Sprintf ( "DELETE FROM dmz_doc_vote WHERE c_docid=\"%s\" AND c_orgid=\"%s\"" , documentID , ctx . OrgID ) )
2018-01-10 16:07:17 +00:00
if err != nil {
return
}
2018-09-26 17:59:56 +01:00
return s . DeleteConstrained ( ctx . Transaction , "dmz_doc" , ctx . OrgID , documentID )
2017-07-31 18:17:30 +01:00
}
2017-09-18 13:35:51 +01:00
2017-09-25 14:37:11 +01:00
// DeleteBySpace removes all documents for given space.
2017-09-18 13:35:51 +01:00
// Remove document pages, revisions, attachments, updates the search subsystem.
2018-09-26 17:59:56 +01:00
func ( s Store ) DeleteBySpace ( ctx domain . RequestContext , spaceID string ) ( rows int64 , err error ) {
rows , err = s . DeleteWhere ( ctx . Transaction , fmt . Sprintf ( "DELETE FROM dmz_section WHERE c_docid IN (SELECT c_refid FROM dmz_doc WHERE c_spaceid=\"%s\" AND c_orgid=\"%s\")" , spaceID , ctx . OrgID ) )
2018-04-13 11:01:36 +01:00
if err != nil {
return
}
2017-09-18 13:35:51 +01:00
2018-09-26 17:59:56 +01:00
_ , err = s . DeleteWhere ( ctx . Transaction , fmt . Sprintf ( "DELETE FROM dmz_section_revision WHERE c_docid IN (SELECT c_refid FROM dmz_doc WHERE c_spaceid=\"%s\" AND c_orgid=\"%s\")" , spaceID , ctx . OrgID ) )
2017-09-18 13:35:51 +01:00
if err != nil {
return
}
2018-09-26 17:59:56 +01:00
_ , err = s . DeleteWhere ( ctx . Transaction , fmt . Sprintf ( "DELETE FROM dmz_doc_attachment WHERE c_docid IN (SELECT c_refid FROM dmz_doc WHERE c_spaceid=\"%s\" AND c_orgid=\"%s\")" , spaceID , ctx . OrgID ) )
2017-09-18 13:35:51 +01:00
if err != nil {
return
}
2018-09-26 17:59:56 +01:00
_ , err = s . DeleteWhere ( ctx . Transaction , fmt . Sprintf ( "DELETE FROM dmz_doc_vote WHERE c_docid IN (SELECT c_refid FROM dmz_doc WHERE c_spaceid=\"%s\" AND c_orgid=\"%s\")" , spaceID , ctx . OrgID ) )
2017-09-18 13:35:51 +01:00
if err != nil {
return
}
2018-09-26 17:59:56 +01:00
return s . DeleteConstrained ( ctx . Transaction , "dmz_doc" , ctx . OrgID , spaceID )
2017-09-18 13:35:51 +01:00
}
2018-03-19 12:24:58 +00:00
// GetVersions returns a slice containing the documents for a given space.
//
// No attempt is made to hide documents that are protected by category
// permissions hence caller must filter as required.
//
// All versions of a document are returned, hence caller must
// decide what to do with them.
2018-09-26 17:59:56 +01:00
func ( s Store ) GetVersions ( ctx domain . RequestContext , groupID string ) ( v [ ] doc . Version , err error ) {
2018-04-05 14:22:23 +01:00
v = [ ] doc . Version { }
2018-09-26 17:59:56 +01:00
err = s . Runtime . Db . Select ( & v , s . Bind ( `
2018-09-20 17:23:26 +01:00
SELECT c_versionid AS versionid , c_refid As documentid
2018-09-18 20:55:40 +01:00
FROM dmz_doc
WHERE c_orgid = ? AND c_groupid = ?
2018-09-26 17:59:56 +01:00
ORDER BY c_versionorder ` ) ,
2018-09-20 17:23:26 +01:00
ctx . OrgID , groupID )
2018-03-19 12:24:58 +00:00
2018-04-05 14:22:23 +01:00
if err == sql . ErrNoRows {
2018-03-19 12:24:58 +00:00
err = nil
}
if err != nil {
err = errors . Wrap ( err , "document.store.GetVersions" )
}
return
}
2018-04-13 11:01:36 +01:00
// Vote records document content vote.
// Any existing vote by the user is replaced.
2018-09-26 17:59:56 +01:00
func ( s Store ) Vote ( ctx domain . RequestContext , refID , orgID , documentID , userID string , vote int ) ( err error ) {
_ , err = s . DeleteWhere ( ctx . Transaction ,
2018-09-18 20:55:40 +01:00
fmt . Sprintf ( "DELETE FROM dmz_doc_vote WHERE c_orgid=\"%s\" AND c_docid=\"%s\" AND c_voter=\"%s\"" ,
2018-04-13 11:01:36 +01:00
orgID , documentID , userID ) )
if err != nil {
s . Runtime . Log . Error ( "store.Vote" , err )
}
2018-09-26 17:59:56 +01:00
_ , err = ctx . Transaction . Exec ( s . Bind ( ` INSERT INTO dmz_doc_vote (c_refid, c_orgid, c_docid, c_voter, c_vote) VALUES (?, ?, ?, ?, ?) ` ) ,
2018-04-13 11:01:36 +01:00
refID , orgID , documentID , userID , vote )
if err != nil {
err = errors . Wrap ( err , "execute insert vote" )
}
return
}