2023-06-24 20:39:55 +08:00
|
|
|
|
// SiYuan - Refactor your thinking
|
2023-03-02 09:12:28 +08:00
|
|
|
|
// Copyright (c) 2020-present, b3log.org
|
|
|
|
|
//
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
2023-03-02 09:27:07 +08:00
|
|
|
|
// Package av 包含了属性视图(Attribute View)相关的实现。
|
2023-03-02 09:12:28 +08:00
|
|
|
|
package av
|
|
|
|
|
|
|
|
|
|
import (
|
2023-07-11 22:11:15 +08:00
|
|
|
|
"errors"
|
2023-07-16 22:03:39 +08:00
|
|
|
|
"fmt"
|
2023-03-02 18:49:05 +08:00
|
|
|
|
"os"
|
2023-03-02 09:12:28 +08:00
|
|
|
|
"path/filepath"
|
2023-12-01 11:29:44 +08:00
|
|
|
|
"regexp"
|
2023-07-16 22:03:39 +08:00
|
|
|
|
"strconv"
|
2023-07-23 22:15:30 +08:00
|
|
|
|
"time"
|
2023-03-02 09:12:28 +08:00
|
|
|
|
|
|
|
|
|
"github.com/88250/gulu"
|
2023-03-03 10:49:45 +08:00
|
|
|
|
"github.com/88250/lute/ast"
|
2023-03-02 09:12:28 +08:00
|
|
|
|
"github.com/siyuan-note/filelock"
|
|
|
|
|
"github.com/siyuan-note/logging"
|
|
|
|
|
"github.com/siyuan-note/siyuan/kernel/util"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// AttributeView 描述了属性视图的结构。
|
|
|
|
|
type AttributeView struct {
|
2023-07-12 21:39:55 +08:00
|
|
|
|
Spec int `json:"spec"` // 格式版本
|
|
|
|
|
ID string `json:"id"` // 属性视图 ID
|
|
|
|
|
Name string `json:"name"` // 属性视图名称
|
|
|
|
|
KeyValues []*KeyValues `json:"keyValues"` // 属性视图属性列值
|
|
|
|
|
ViewID string `json:"viewID"` // 当前视图 ID
|
|
|
|
|
Views []*View `json:"views"` // 视图
|
2023-07-12 19:10:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// KeyValues 描述了属性视图属性列值的结构。
|
|
|
|
|
type KeyValues struct {
|
2023-07-12 23:52:25 +08:00
|
|
|
|
Key *Key `json:"key"` // 属性视图属性列
|
|
|
|
|
Values []*Value `json:"values,omitempty"` // 属性视图属性列值
|
2023-07-12 19:10:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-24 22:27:38 +08:00
|
|
|
|
func (kValues *KeyValues) GetValue(blockID string) (ret *Value) {
|
|
|
|
|
for _, v := range kValues.Values {
|
|
|
|
|
if v.BlockID == blockID {
|
|
|
|
|
ret = v
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-12 19:10:05 +08:00
|
|
|
|
type KeyType string
|
|
|
|
|
|
|
|
|
|
const (
|
2023-10-01 10:42:12 +08:00
|
|
|
|
KeyTypeBlock KeyType = "block"
|
|
|
|
|
KeyTypeText KeyType = "text"
|
|
|
|
|
KeyTypeNumber KeyType = "number"
|
|
|
|
|
KeyTypeDate KeyType = "date"
|
|
|
|
|
KeyTypeSelect KeyType = "select"
|
|
|
|
|
KeyTypeMSelect KeyType = "mSelect"
|
|
|
|
|
KeyTypeURL KeyType = "url"
|
|
|
|
|
KeyTypeEmail KeyType = "email"
|
|
|
|
|
KeyTypePhone KeyType = "phone"
|
|
|
|
|
KeyTypeMAsset KeyType = "mAsset"
|
|
|
|
|
KeyTypeTemplate KeyType = "template"
|
2023-10-08 12:16:58 +08:00
|
|
|
|
KeyTypeCreated KeyType = "created"
|
|
|
|
|
KeyTypeUpdated KeyType = "updated"
|
2023-11-17 09:03:17 +08:00
|
|
|
|
KeyTypeCheckbox KeyType = "checkbox"
|
2023-12-15 20:05:14 +08:00
|
|
|
|
KeyTypeRelation KeyType = "relation"
|
|
|
|
|
KeyTypeRollup KeyType = "rollup"
|
2023-07-12 19:10:05 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Key 描述了属性视图属性列的基础结构。
|
|
|
|
|
type Key struct {
|
|
|
|
|
ID string `json:"id"` // 列 ID
|
|
|
|
|
Name string `json:"name"` // 列名
|
|
|
|
|
Type KeyType `json:"type"` // 列类型
|
|
|
|
|
Icon string `json:"icon"` // 列图标
|
|
|
|
|
|
|
|
|
|
// 以下是某些列类型的特有属性
|
|
|
|
|
|
2023-12-15 20:05:14 +08:00
|
|
|
|
// 单选/多选列
|
2023-12-23 17:57:45 +08:00
|
|
|
|
Options []*SelectOption `json:"options,omitempty"` // 选项列表
|
2023-12-15 20:05:14 +08:00
|
|
|
|
|
|
|
|
|
// 数字列
|
|
|
|
|
NumberFormat NumberFormat `json:"numberFormat"` // 列数字格式化
|
|
|
|
|
|
|
|
|
|
// 模板列
|
|
|
|
|
Template string `json:"template"` // 模板内容
|
|
|
|
|
|
|
|
|
|
// 关联列
|
2023-12-23 17:45:46 +08:00
|
|
|
|
Relation *Relation `json:"relation,omitempty"` // 关联信息
|
2023-12-15 20:05:14 +08:00
|
|
|
|
|
|
|
|
|
// 汇总列
|
2023-12-23 17:45:46 +08:00
|
|
|
|
Rollup *Rollup `json:"rollup,omitempty"` // 汇总信息
|
2023-07-12 19:10:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-06 10:17:52 +08:00
|
|
|
|
func NewKey(id, name, icon string, keyType KeyType) *Key {
|
2023-07-12 19:10:05 +08:00
|
|
|
|
return &Key{
|
2023-07-17 11:21:46 +08:00
|
|
|
|
ID: id,
|
2023-07-12 19:10:05 +08:00
|
|
|
|
Name: name,
|
|
|
|
|
Type: keyType,
|
2023-10-06 10:17:52 +08:00
|
|
|
|
Icon: icon,
|
2023-07-12 19:10:05 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-23 17:45:46 +08:00
|
|
|
|
type Rollup struct {
|
2023-12-24 21:47:10 +08:00
|
|
|
|
RelationKeyID string `json:"relationKeyID"` // 关联列 ID
|
|
|
|
|
KeyID string `json:"keyID"` // 目标列 ID
|
|
|
|
|
Calc *RollupCalc `json:"calc"` // 计算方式
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type RollupCalc struct {
|
|
|
|
|
Operator CalcOperator `json:"operator"`
|
|
|
|
|
Result *Value `json:"result"`
|
2023-12-23 17:45:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Relation struct {
|
|
|
|
|
AvID string `json:"avID"` // 关联的属性视图 ID
|
|
|
|
|
IsTwoWay bool `json:"isTwoWay"` // 是否双向关联
|
|
|
|
|
BackKeyID string `json:"backKeyID"` // 双向关联时回链关联列的 ID
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-23 17:57:45 +08:00
|
|
|
|
type SelectOption struct {
|
2023-07-12 19:10:05 +08:00
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
Color string `json:"color"`
|
|
|
|
|
}
|
2023-03-02 09:12:28 +08:00
|
|
|
|
|
2023-07-11 19:45:27 +08:00
|
|
|
|
// View 描述了视图的结构。
|
|
|
|
|
type View struct {
|
2023-07-12 10:35:17 +08:00
|
|
|
|
ID string `json:"id"` // 视图 ID
|
2023-12-01 09:48:20 +08:00
|
|
|
|
Icon string `json:"icon"` // 视图图标
|
2023-07-12 10:35:17 +08:00
|
|
|
|
Name string `json:"name"` // 视图名称
|
2023-07-11 19:45:27 +08:00
|
|
|
|
|
2023-07-12 21:39:55 +08:00
|
|
|
|
LayoutType LayoutType `json:"type"` // 当前布局类型
|
|
|
|
|
Table *LayoutTable `json:"table,omitempty"` // 表格布局
|
2023-07-11 19:45:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-12 10:35:17 +08:00
|
|
|
|
// LayoutType 描述了视图布局的类型。
|
|
|
|
|
type LayoutType string
|
2023-03-02 09:16:30 +08:00
|
|
|
|
|
|
|
|
|
const (
|
2023-07-12 10:35:17 +08:00
|
|
|
|
LayoutTypeTable LayoutType = "table" // 属性视图类型 - 表格
|
2023-03-02 09:16:30 +08:00
|
|
|
|
)
|
|
|
|
|
|
2023-12-01 09:17:44 +08:00
|
|
|
|
func NewTableView() (ret *View) {
|
|
|
|
|
ret = &View{
|
|
|
|
|
ID: ast.NewNodeID(),
|
2023-12-14 11:51:31 +08:00
|
|
|
|
Name: getI18nName("table"),
|
2023-12-01 09:17:44 +08:00
|
|
|
|
LayoutType: LayoutTypeTable,
|
|
|
|
|
Table: &LayoutTable{
|
2023-12-08 22:00:23 +08:00
|
|
|
|
Spec: 0,
|
|
|
|
|
ID: ast.NewNodeID(),
|
|
|
|
|
Filters: []*ViewFilter{},
|
|
|
|
|
Sorts: []*ViewSort{},
|
|
|
|
|
PageSize: 50,
|
2023-12-01 09:17:44 +08:00
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewTableViewWithBlockKey(blockKeyID string) (view *View, blockKey *Key) {
|
2023-12-14 11:51:31 +08:00
|
|
|
|
name := getI18nName("table")
|
2023-11-30 23:19:43 +08:00
|
|
|
|
view = &View{
|
2023-07-12 21:39:55 +08:00
|
|
|
|
ID: ast.NewNodeID(),
|
|
|
|
|
Name: name,
|
|
|
|
|
LayoutType: LayoutTypeTable,
|
2023-07-12 10:35:17 +08:00
|
|
|
|
Table: &LayoutTable{
|
2023-12-08 22:00:23 +08:00
|
|
|
|
Spec: 0,
|
|
|
|
|
ID: ast.NewNodeID(),
|
|
|
|
|
Filters: []*ViewFilter{},
|
|
|
|
|
Sorts: []*ViewSort{},
|
|
|
|
|
PageSize: 50,
|
2023-07-11 23:40:05 +08:00
|
|
|
|
},
|
|
|
|
|
}
|
2023-12-14 11:51:31 +08:00
|
|
|
|
blockKey = NewKey(blockKeyID, getI18nName("key"), "", KeyTypeBlock)
|
2023-12-01 08:55:32 +08:00
|
|
|
|
view.Table.Columns = []*ViewTableColumn{{ID: blockKeyID}}
|
2023-11-30 23:19:43 +08:00
|
|
|
|
return
|
2023-07-11 23:40:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-11 19:45:27 +08:00
|
|
|
|
// Viewable 描述了视图的接口。
|
|
|
|
|
type Viewable interface {
|
|
|
|
|
Filterable
|
|
|
|
|
Sortable
|
|
|
|
|
Calculable
|
|
|
|
|
|
2023-07-12 10:35:17 +08:00
|
|
|
|
GetType() LayoutType
|
2023-07-11 21:31:19 +08:00
|
|
|
|
GetID() string
|
2023-07-11 19:45:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-26 09:38:50 +08:00
|
|
|
|
func NewAttributeView(id string) (ret *AttributeView) {
|
2023-12-01 09:17:44 +08:00
|
|
|
|
view, blockKey := NewTableViewWithBlockKey(ast.NewNodeID())
|
2023-07-12 19:55:18 +08:00
|
|
|
|
ret = &AttributeView{
|
2023-07-12 21:39:55 +08:00
|
|
|
|
Spec: 0,
|
|
|
|
|
ID: id,
|
2023-11-30 23:19:43 +08:00
|
|
|
|
KeyValues: []*KeyValues{{Key: blockKey}},
|
2023-07-12 21:39:55 +08:00
|
|
|
|
ViewID: view.ID,
|
|
|
|
|
Views: []*View{view},
|
2023-03-02 09:12:28 +08:00
|
|
|
|
}
|
2023-07-12 19:55:18 +08:00
|
|
|
|
return
|
2023-03-02 09:12:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-02 11:32:39 +08:00
|
|
|
|
func ParseAttributeView(avID string) (ret *AttributeView, err error) {
|
2023-07-31 11:20:58 +08:00
|
|
|
|
avJSONPath := GetAttributeViewDataPath(avID)
|
2023-11-06 22:13:04 +08:00
|
|
|
|
if !filelock.IsExist(avJSONPath) {
|
2023-07-31 11:20:58 +08:00
|
|
|
|
err = ErrViewNotFound
|
|
|
|
|
return
|
2023-03-02 11:32:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-31 11:20:58 +08:00
|
|
|
|
data, readErr := filelock.ReadFile(avJSONPath)
|
|
|
|
|
if nil != readErr {
|
|
|
|
|
logging.LogErrorf("read attribute view [%s] failed: %s", avID, readErr)
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-03-02 09:12:28 +08:00
|
|
|
|
|
2023-07-31 11:20:58 +08:00
|
|
|
|
ret = &AttributeView{}
|
|
|
|
|
if err = gulu.JSON.UnmarshalJSON(data, ret); nil != err {
|
|
|
|
|
logging.LogErrorf("unmarshal attribute view [%s] failed: %s", avID, err)
|
|
|
|
|
return
|
2023-03-02 09:12:28 +08:00
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-02 11:32:39 +08:00
|
|
|
|
func SaveAttributeView(av *AttributeView) (err error) {
|
2023-10-16 23:57:09 +08:00
|
|
|
|
// 做一些数据兼容和订正处理
|
2023-10-10 21:55:43 +08:00
|
|
|
|
now := util.CurrentTimeMillis()
|
|
|
|
|
for _, kv := range av.KeyValues {
|
2023-11-17 10:15:30 +08:00
|
|
|
|
switch kv.Key.Type {
|
|
|
|
|
case KeyTypeBlock:
|
2023-10-10 21:55:43 +08:00
|
|
|
|
// 补全 block 的创建时间和更新时间
|
|
|
|
|
for _, v := range kv.Values {
|
|
|
|
|
if 0 == v.Block.Created {
|
2023-10-13 13:21:53 +08:00
|
|
|
|
if "" == v.Block.ID {
|
2023-10-13 14:03:41 +08:00
|
|
|
|
v.Block.ID = v.BlockID
|
|
|
|
|
if "" == v.Block.ID {
|
|
|
|
|
v.Block.ID = ast.NewNodeID()
|
|
|
|
|
v.BlockID = v.Block.ID
|
|
|
|
|
}
|
2023-10-13 13:21:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-10 21:55:43 +08:00
|
|
|
|
createdStr := v.Block.ID[:len("20060102150405")]
|
|
|
|
|
created, parseErr := time.ParseInLocation("20060102150405", createdStr, time.Local)
|
|
|
|
|
if nil == parseErr {
|
|
|
|
|
v.Block.Created = created.UnixMilli()
|
|
|
|
|
} else {
|
|
|
|
|
v.Block.Created = now
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if 0 == v.Block.Updated {
|
|
|
|
|
v.Block.Updated = now
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-17 10:15:30 +08:00
|
|
|
|
case KeyTypeNumber:
|
|
|
|
|
for _, v := range kv.Values {
|
2023-12-18 12:26:38 +08:00
|
|
|
|
if nil != v.Number && 0 != v.Number.Content && !v.Number.IsNotEmpty {
|
2023-11-17 10:15:30 +08:00
|
|
|
|
v.Number.IsNotEmpty = true
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-10 21:55:43 +08:00
|
|
|
|
}
|
2023-12-30 17:13:53 +08:00
|
|
|
|
|
|
|
|
|
for _, v := range kv.Values {
|
|
|
|
|
if "" == v.KeyID {
|
|
|
|
|
v.KeyID = kv.Key.ID
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-10 21:55:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-08 22:00:23 +08:00
|
|
|
|
// 数据订正
|
2023-10-16 23:57:09 +08:00
|
|
|
|
for _, view := range av.Views {
|
|
|
|
|
if nil != view.Table {
|
2023-12-08 22:00:23 +08:00
|
|
|
|
// 行去重
|
2023-10-16 23:57:09 +08:00
|
|
|
|
view.Table.RowIDs = gulu.Str.RemoveDuplicatedElem(view.Table.RowIDs)
|
2023-12-08 22:00:23 +08:00
|
|
|
|
// 分页大小
|
|
|
|
|
if 1 > view.Table.PageSize {
|
|
|
|
|
view.Table.PageSize = 50
|
|
|
|
|
}
|
2023-10-16 23:57:09 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-27 21:05:21 +08:00
|
|
|
|
data, err := gulu.JSON.MarshalJSON(av)
|
2023-03-02 09:12:28 +08:00
|
|
|
|
if nil != err {
|
2023-03-02 11:32:39 +08:00
|
|
|
|
logging.LogErrorf("marshal attribute view [%s] failed: %s", av.ID, err)
|
2023-03-02 09:12:28 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-31 11:20:58 +08:00
|
|
|
|
avJSONPath := GetAttributeViewDataPath(av.ID)
|
2023-03-02 09:12:28 +08:00
|
|
|
|
if err = filelock.WriteFile(avJSONPath, data); nil != err {
|
2023-03-02 11:32:39 +08:00
|
|
|
|
logging.LogErrorf("save attribute view [%s] failed: %s", av.ID, err)
|
2023-03-02 09:12:28 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-30 20:04:51 +08:00
|
|
|
|
func (av *AttributeView) GetView(viewID string) (ret *View) {
|
|
|
|
|
for _, v := range av.Views {
|
|
|
|
|
if v.ID == viewID {
|
|
|
|
|
ret = v
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (av *AttributeView) GetCurrentView() (ret *View, err error) {
|
2023-07-11 22:11:15 +08:00
|
|
|
|
for _, v := range av.Views {
|
2023-07-12 23:52:25 +08:00
|
|
|
|
if v.ID == av.ViewID {
|
2023-07-11 22:11:15 +08:00
|
|
|
|
ret = v
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-07-11 23:47:17 +08:00
|
|
|
|
err = ErrViewNotFound
|
2023-07-11 22:11:15 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-24 22:27:38 +08:00
|
|
|
|
func (av *AttributeView) GetValue(keyID, blockID string) (ret *Value) {
|
|
|
|
|
for _, kv := range av.KeyValues {
|
|
|
|
|
if kv.Key.ID == keyID {
|
|
|
|
|
for _, v := range kv.Values {
|
|
|
|
|
if v.BlockID == blockID {
|
|
|
|
|
ret = v
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-12 19:10:05 +08:00
|
|
|
|
func (av *AttributeView) GetKey(keyID string) (ret *Key, err error) {
|
|
|
|
|
for _, kv := range av.KeyValues {
|
|
|
|
|
if kv.Key.ID == keyID {
|
|
|
|
|
ret = kv.Key
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
err = ErrKeyNotFound
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (av *AttributeView) GetBlockKeyValues() (ret *KeyValues) {
|
|
|
|
|
for _, kv := range av.KeyValues {
|
|
|
|
|
if KeyTypeBlock == kv.Key.Type {
|
|
|
|
|
ret = kv
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-17 12:00:55 +08:00
|
|
|
|
func (av *AttributeView) GetKeyValues(keyID string) (ret *KeyValues, err error) {
|
|
|
|
|
for _, kv := range av.KeyValues {
|
|
|
|
|
if kv.Key.ID == keyID {
|
|
|
|
|
ret = kv
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
err = ErrKeyNotFound
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-01 08:55:32 +08:00
|
|
|
|
func (av *AttributeView) GetBlockKey() (ret *Key) {
|
|
|
|
|
for _, kv := range av.KeyValues {
|
|
|
|
|
if KeyTypeBlock == kv.Key.Type {
|
|
|
|
|
ret = kv.Key
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-01 11:29:44 +08:00
|
|
|
|
func (av *AttributeView) GetDuplicateViewName(masterViewName string) (ret string) {
|
|
|
|
|
ret = masterViewName + " (1)"
|
|
|
|
|
r := regexp.MustCompile("^(.*) \\((\\d+)\\)$")
|
|
|
|
|
m := r.FindStringSubmatch(masterViewName)
|
|
|
|
|
if nil == m || 3 > len(m) {
|
|
|
|
|
return
|
2023-12-01 10:57:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-01 11:29:44 +08:00
|
|
|
|
num, _ := strconv.Atoi(m[2])
|
|
|
|
|
num++
|
|
|
|
|
ret = fmt.Sprintf("%s (%d)", m[1], num)
|
|
|
|
|
return
|
2023-12-01 10:57:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-15 12:02:27 +08:00
|
|
|
|
func (av *AttributeView) ShallowClone() (ret *AttributeView) {
|
|
|
|
|
ret = &AttributeView{}
|
|
|
|
|
data, err := gulu.JSON.MarshalJSON(av)
|
|
|
|
|
if nil != err {
|
|
|
|
|
logging.LogErrorf("marshal attribute view [%s] failed: %s", av.ID, err)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
if err = gulu.JSON.UnmarshalJSON(data, ret); nil != err {
|
|
|
|
|
logging.LogErrorf("unmarshal attribute view [%s] failed: %s", av.ID, err)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret.ID = ast.NewNodeID()
|
|
|
|
|
view, err := ret.GetCurrentView()
|
|
|
|
|
if nil == err {
|
|
|
|
|
view.ID = ast.NewNodeID()
|
|
|
|
|
ret.ViewID = view.ID
|
|
|
|
|
} else {
|
|
|
|
|
view, _ = NewTableViewWithBlockKey(ast.NewNodeID())
|
|
|
|
|
ret.ViewID = view.ID
|
|
|
|
|
ret.Views = append(ret.Views, view)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
keyIDMap := map[string]string{}
|
|
|
|
|
for _, kv := range ret.KeyValues {
|
|
|
|
|
newID := ast.NewNodeID()
|
|
|
|
|
keyIDMap[kv.Key.ID] = newID
|
|
|
|
|
kv.Key.ID = newID
|
|
|
|
|
kv.Values = []*Value{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
view.Table.ID = ast.NewNodeID()
|
|
|
|
|
for _, column := range view.Table.Columns {
|
|
|
|
|
column.ID = keyIDMap[column.ID]
|
|
|
|
|
}
|
|
|
|
|
view.Table.RowIDs = []string{}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-31 11:20:58 +08:00
|
|
|
|
func GetAttributeViewDataPath(avID string) (ret string) {
|
2023-03-02 18:49:05 +08:00
|
|
|
|
av := filepath.Join(util.DataDir, "storage", "av")
|
|
|
|
|
ret = filepath.Join(av, avID+".json")
|
|
|
|
|
if !gulu.File.IsDir(av) {
|
|
|
|
|
if err := os.MkdirAll(av, 0755); nil != err {
|
|
|
|
|
logging.LogErrorf("create attribute view dir failed: %s", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
2023-03-02 09:12:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-14 11:51:31 +08:00
|
|
|
|
func getI18nName(name string) string {
|
|
|
|
|
return util.AttrViewLangs[util.Lang][name].(string)
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-11 22:11:15 +08:00
|
|
|
|
var (
|
|
|
|
|
ErrViewNotFound = errors.New("view not found")
|
2023-07-12 19:10:05 +08:00
|
|
|
|
ErrKeyNotFound = errors.New("key not found")
|
2023-07-11 22:11:15 +08:00
|
|
|
|
)
|
2023-10-05 12:02:17 +08:00
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
NodeAttrNameAvs = "custom-avs" // 用于标记块所属的属性视图,逗号分隔 av id
|
|
|
|
|
)
|