2024-03-14 19:25:27 +00:00
|
|
|
import type { HttpContext } from '@adonisjs/core/http';
|
2024-04-30 09:50:50 +00:00
|
|
|
import User from '#models/user';
|
|
|
|
import Dataset from '#models/dataset';
|
|
|
|
import License from '#models/license';
|
|
|
|
import Project from '#models/project';
|
|
|
|
import Title from '#models/title';
|
|
|
|
import Description from '#models/description';
|
|
|
|
import Language from '#models/language';
|
|
|
|
import Coverage from '#models/coverage';
|
|
|
|
import Collection from '#models/collection';
|
2023-03-24 10:41:52 +00:00
|
|
|
import dayjs from 'dayjs';
|
2024-04-30 09:50:50 +00:00
|
|
|
import Person from '#models/person';
|
2024-03-14 19:25:27 +00:00
|
|
|
import db from '@adonisjs/lucid/services/db';
|
2024-04-23 17:36:45 +00:00
|
|
|
import { TransactionClientContract } from '@adonisjs/lucid/types/database';
|
2024-04-30 09:50:50 +00:00
|
|
|
import Subject from '#models/subject';
|
2024-05-16 11:47:06 +00:00
|
|
|
// import CreateDatasetValidator from '#validators/create_dataset_validator';
|
|
|
|
import { createDatasetValidator, updateDatasetValidator } from '#validators/dataset';
|
|
|
|
// import UpdateDatasetValidator from '#validators/update_dataset_validator';
|
2023-09-05 16:18:42 +00:00
|
|
|
import {
|
|
|
|
TitleTypes,
|
|
|
|
DescriptionTypes,
|
|
|
|
ContributorTypes,
|
|
|
|
PersonNameTypes,
|
|
|
|
ReferenceIdentifierTypes,
|
|
|
|
RelationTypes,
|
|
|
|
DatasetTypes,
|
2023-09-12 14:09:04 +00:00
|
|
|
SubjectTypes,
|
2024-03-14 19:25:27 +00:00
|
|
|
} from '#contracts/enums';
|
2024-04-23 17:36:45 +00:00
|
|
|
import { ModelQueryBuilderContract } from '@adonisjs/lucid/types/model';
|
2024-04-30 09:50:50 +00:00
|
|
|
import DatasetReference from '#models/dataset_reference';
|
2024-03-14 19:25:27 +00:00
|
|
|
import { cuid } from '@adonisjs/core/helpers';
|
2024-04-30 09:50:50 +00:00
|
|
|
import File from '#models/file';
|
2023-09-04 11:24:58 +00:00
|
|
|
import ClamScan from 'clamscan';
|
2024-05-16 11:47:06 +00:00
|
|
|
// import { ValidationException } from '@adonisjs/validator';
|
2024-03-14 19:25:27 +00:00
|
|
|
// import Drive from '@ioc:Adonis/Core/Drive';
|
2024-04-23 17:36:45 +00:00
|
|
|
import drive from '#services/drive';
|
|
|
|
import { Exception } from '@adonisjs/core/exceptions';
|
2024-03-14 19:25:27 +00:00
|
|
|
import { MultipartFile } from '@adonisjs/core/types/bodyparser';
|
2024-02-06 15:39:33 +00:00
|
|
|
import * as crypto from 'crypto';
|
2024-03-14 19:25:27 +00:00
|
|
|
interface Dictionary {
|
|
|
|
[index: string]: string;
|
|
|
|
}
|
2024-05-16 11:47:06 +00:00
|
|
|
import vine, { SimpleMessagesProvider, errors } from '@vinejs/vine';
|
2023-03-17 15:13:37 +00:00
|
|
|
|
|
|
|
export default class DatasetController {
|
2024-03-14 19:25:27 +00:00
|
|
|
public async index({ auth, request, inertia }: HttpContext) {
|
2023-06-27 16:23:18 +00:00
|
|
|
const user = (await User.find(auth.user?.id)) as User;
|
|
|
|
const page = request.input('page', 1);
|
|
|
|
let datasets: ModelQueryBuilderContract<typeof Dataset, Dataset> = Dataset.query();
|
|
|
|
|
|
|
|
// if (request.input('search')) {
|
|
|
|
// // users = users.whereRaw('name like %?%', [request.input('search')])
|
|
|
|
// const searchTerm = request.input('search');
|
|
|
|
// datasets.where('name', 'ilike', `%${searchTerm}%`);
|
|
|
|
// }
|
|
|
|
|
|
|
|
if (request.input('sort')) {
|
|
|
|
type SortOrder = 'asc' | 'desc' | undefined;
|
|
|
|
let attribute = request.input('sort');
|
2023-07-17 17:13:30 +00:00
|
|
|
let sortOrder: SortOrder = 'asc';
|
2023-06-27 16:23:18 +00:00
|
|
|
|
2023-07-17 17:13:30 +00:00
|
|
|
if (attribute.substr(0, 1) === '-') {
|
|
|
|
sortOrder = 'desc';
|
2023-06-27 16:23:18 +00:00
|
|
|
// attribute = substr(attribute, 1);
|
|
|
|
attribute = attribute.substr(1);
|
|
|
|
}
|
2023-07-17 17:13:30 +00:00
|
|
|
datasets.orderBy(attribute, sortOrder);
|
2023-06-27 16:23:18 +00:00
|
|
|
} else {
|
|
|
|
// users.orderBy('created_at', 'desc');
|
|
|
|
datasets.orderBy('id', 'asc');
|
|
|
|
}
|
|
|
|
|
2023-08-23 15:07:26 +00:00
|
|
|
// const results = await Database
|
2023-09-04 11:24:58 +00:00
|
|
|
// .query()
|
2023-08-23 15:07:26 +00:00
|
|
|
// .select(Database.raw("CONCAT('https://doi.org/', b.value) AS concatenated_value"))
|
|
|
|
// .from('documents as doc')
|
|
|
|
// .innerJoin('dataset_identifiers as b', 'doc.id', 'b.dataset_id')
|
|
|
|
// .groupBy('a.id').toQuery();
|
|
|
|
|
2023-06-27 16:23:18 +00:00
|
|
|
// const users = await User.query().orderBy('login').paginate(page, limit);
|
|
|
|
const myDatasets = await datasets
|
|
|
|
.whereIn('server_state', [
|
|
|
|
'inprogress',
|
|
|
|
'released',
|
|
|
|
'editor_accepted',
|
|
|
|
'approved',
|
|
|
|
'reviewed',
|
|
|
|
'rejected_editor',
|
|
|
|
'rejected_reviewer',
|
|
|
|
])
|
|
|
|
.where('account_id', user.id)
|
|
|
|
.preload('titles')
|
|
|
|
.preload('user', (query) => query.select('id', 'login'))
|
|
|
|
// .preload('titles', (builder) => {
|
|
|
|
// // pull the actual preload data
|
|
|
|
|
|
|
|
// builder.where('type', 'Main');
|
|
|
|
// })
|
|
|
|
.paginate(page, 10);
|
|
|
|
|
|
|
|
return inertia.render('Submitter/Dataset/Index', {
|
|
|
|
// testing: 'this is a test',
|
|
|
|
datasets: myDatasets.serialize(),
|
|
|
|
filters: request.all(),
|
|
|
|
can: {
|
2023-07-17 17:13:30 +00:00
|
|
|
// create: await auth.user?.can(['dataset-submit']),
|
2023-06-27 16:23:18 +00:00
|
|
|
edit: await auth.user?.can(['dataset-edit']),
|
|
|
|
delete: await auth.user?.can(['dataset-delete']),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2023-07-27 12:53:34 +00:00
|
|
|
|
2024-03-14 19:25:27 +00:00
|
|
|
public async create({ inertia }: HttpContext) {
|
2023-09-04 11:24:58 +00:00
|
|
|
const licenses = await License.query().select('id', 'name_long').where('active', 'true').pluck('name_long', 'id');
|
2023-03-17 15:13:37 +00:00
|
|
|
|
2023-03-24 10:41:52 +00:00
|
|
|
const projects = await Project.query().pluck('label', 'id');
|
|
|
|
|
2023-09-05 16:18:42 +00:00
|
|
|
// const doctypes = {
|
|
|
|
// analysisdata: { label: 'Analysis', value: 'analysisdata' },
|
|
|
|
// measurementdata: { label: 'Measurements', value: 'measurementdata' },
|
|
|
|
// monitoring: 'Monitoring',
|
|
|
|
// remotesensing: 'Remote Sensing',
|
|
|
|
// gis: 'GIS',
|
|
|
|
// models: 'Models',
|
|
|
|
// mixedtype: 'Mixed Type',
|
|
|
|
// vocabulary: 'Vocabulary',
|
2023-03-17 15:13:37 +00:00
|
|
|
// };
|
|
|
|
return inertia.render('Submitter/Dataset/Create', {
|
|
|
|
licenses: licenses,
|
2023-09-05 16:18:42 +00:00
|
|
|
doctypes: DatasetTypes,
|
2023-03-24 10:41:52 +00:00
|
|
|
titletypes: Object.entries(TitleTypes)
|
|
|
|
.filter(([value]) => value !== 'Main')
|
|
|
|
.map(([key, value]) => ({ value: key, label: value })),
|
|
|
|
descriptiontypes: Object.entries(DescriptionTypes)
|
|
|
|
.filter(([value]) => value !== 'Abstract')
|
|
|
|
.map(([key, value]) => ({ value: key, label: value })),
|
2023-03-17 15:13:37 +00:00
|
|
|
// descriptiontypes: DescriptionTypes
|
2023-03-24 10:41:52 +00:00
|
|
|
projects: projects,
|
2023-08-23 15:07:26 +00:00
|
|
|
referenceIdentifierTypes: Object.entries(ReferenceIdentifierTypes).map(([key, value]) => ({ value: key, label: value })),
|
|
|
|
relationTypes: Object.entries(RelationTypes).map(([key, value]) => ({ value: key, label: value })),
|
2023-09-12 14:09:04 +00:00
|
|
|
contributorTypes: ContributorTypes,
|
2023-09-14 13:37:36 +00:00
|
|
|
subjectTypes: SubjectTypes,
|
2023-03-17 15:13:37 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-03-14 19:25:27 +00:00
|
|
|
public async firstStep({ request, response }: HttpContext) {
|
2024-05-16 11:47:06 +00:00
|
|
|
// const newDatasetSchema = schema.create({
|
|
|
|
// language: schema.string({ trim: true }, [
|
|
|
|
// rules.regex(/^[a-zA-Z0-9-_]+$/), //Must be alphanumeric with hyphens or underscores
|
|
|
|
// ]),
|
|
|
|
// licenses: schema.array([rules.minLength(1)]).members(schema.number()), // define at least one license for the new dataset
|
|
|
|
// rights: schema.string([rules.equalTo('true')]),
|
|
|
|
// });
|
|
|
|
const newDatasetSchema = vine.object({
|
|
|
|
// first step
|
|
|
|
language: vine
|
|
|
|
.string()
|
|
|
|
.trim()
|
|
|
|
.regex(/^[a-zA-Z0-9]+$/),
|
|
|
|
licenses: vine.array(vine.number()).minLength(1), // define at least one license for the new dataset
|
|
|
|
rights: vine.string().in(['true']),
|
2023-03-17 15:13:37 +00:00
|
|
|
});
|
2024-04-29 09:25:50 +00:00
|
|
|
// await request.validate({ schema: newDatasetSchema, messages: this.messages });
|
2023-03-17 15:13:37 +00:00
|
|
|
try {
|
|
|
|
// Step 2 - Validate request body against the schema
|
2024-05-16 11:47:06 +00:00
|
|
|
// await request.validate({ schema: newDatasetSchema, messages: this.messages });
|
|
|
|
const validator = vine.compile(newDatasetSchema);
|
|
|
|
await request.validateUsing(validator, { messagesProvider: new SimpleMessagesProvider(this.messages) });
|
2023-03-17 15:13:37 +00:00
|
|
|
} catch (error) {
|
|
|
|
// Step 3 - Handle errors
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
return response.redirect().back();
|
|
|
|
}
|
|
|
|
|
2024-03-14 19:25:27 +00:00
|
|
|
public async secondStep({ request, response }: HttpContext) {
|
2024-05-16 11:47:06 +00:00
|
|
|
const newDatasetSchema = vine.object({
|
2023-03-24 10:41:52 +00:00
|
|
|
// first step
|
2024-05-16 11:47:06 +00:00
|
|
|
language: vine
|
|
|
|
.string()
|
|
|
|
.trim()
|
|
|
|
.regex(/^[a-zA-Z0-9]+$/),
|
|
|
|
licenses: vine.array(vine.number()).minLength(1), // define at least one license for the new dataset
|
|
|
|
rights: vine.string().in(['true']),
|
2023-03-24 10:41:52 +00:00
|
|
|
// second step
|
2024-05-16 11:47:06 +00:00
|
|
|
type: vine.string().trim().minLength(3).maxLength(255),
|
|
|
|
creating_corporation: vine.string().trim().minLength(3).maxLength(255),
|
|
|
|
titles: vine
|
|
|
|
.array(
|
|
|
|
vine.object({
|
|
|
|
value: vine.string().trim().minLength(3).maxLength(255),
|
|
|
|
type: vine.enum(Object.values(TitleTypes)),
|
|
|
|
language: vine
|
|
|
|
.string()
|
|
|
|
.trim()
|
|
|
|
.minLength(2)
|
|
|
|
.maxLength(255)
|
|
|
|
.translatedLanguage({ mainLanguageField: 'language', typeField: 'type' }),
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.minLength(1),
|
|
|
|
descriptions: vine
|
|
|
|
.array(
|
|
|
|
vine.object({
|
|
|
|
value: vine.string().trim().minLength(3).maxLength(255),
|
|
|
|
type: vine.enum(Object.values(DescriptionTypes)),
|
|
|
|
language: vine
|
|
|
|
.string()
|
|
|
|
.trim()
|
|
|
|
.minLength(2)
|
|
|
|
.maxLength(255)
|
|
|
|
.translatedLanguage({ mainLanguageField: 'language', typeField: 'type' }),
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.minLength(1),
|
|
|
|
authors: vine
|
|
|
|
.array(
|
|
|
|
vine.object({
|
2024-06-14 10:38:04 +00:00
|
|
|
email: vine.string().trim().maxLength(255).email().normalizeEmail().isUniquePerson({ table: 'persons', column: 'email', idField: 'id' }),
|
|
|
|
first_name: vine.string().trim().minLength(3).maxLength(255),
|
|
|
|
last_name: vine.string().trim().minLength(3).maxLength(255),
|
2024-05-16 11:47:06 +00:00
|
|
|
}),
|
|
|
|
)
|
2024-06-14 10:38:04 +00:00
|
|
|
.minLength(1)
|
|
|
|
.distinct('email'),
|
2024-05-16 11:47:06 +00:00
|
|
|
contributors: vine
|
|
|
|
.array(
|
|
|
|
vine.object({
|
2024-06-14 10:38:04 +00:00
|
|
|
email: vine.string().trim().maxLength(255).email().normalizeEmail().isUniquePerson({ table: 'persons', column: 'email', idField: 'id' }),
|
|
|
|
first_name: vine.string().trim().minLength(3).maxLength(255),
|
|
|
|
last_name: vine.string().trim().minLength(3).maxLength(255),
|
2024-05-16 11:47:06 +00:00
|
|
|
pivot_contributor_type: vine.enum(Object.keys(ContributorTypes)),
|
|
|
|
}),
|
|
|
|
)
|
2024-06-14 10:38:04 +00:00
|
|
|
.distinct('email')
|
|
|
|
.optional(),
|
2024-05-16 11:47:06 +00:00
|
|
|
project_id: vine.number().optional(),
|
2023-03-24 10:41:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Step 2 - Validate request body against the schema
|
2024-05-16 11:47:06 +00:00
|
|
|
// await request.validate({ schema: newDatasetSchema, messages: this.messages });
|
|
|
|
const validator = vine.compile(newDatasetSchema);
|
|
|
|
await request.validateUsing(validator, { messagesProvider: new SimpleMessagesProvider(this.messages) });
|
2023-03-24 10:41:52 +00:00
|
|
|
} catch (error) {
|
|
|
|
// Step 3 - Handle errors
|
|
|
|
// return response.badRequest(error.messages);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
return response.redirect().back();
|
|
|
|
}
|
|
|
|
|
2024-03-14 19:25:27 +00:00
|
|
|
public async thirdStep({ request, response }: HttpContext) {
|
2024-05-16 11:47:06 +00:00
|
|
|
const newDatasetSchema = vine.object({
|
2023-03-24 10:41:52 +00:00
|
|
|
// first step
|
2024-05-16 11:47:06 +00:00
|
|
|
language: vine
|
|
|
|
.string()
|
|
|
|
.trim()
|
|
|
|
.regex(/^[a-zA-Z0-9]+$/),
|
|
|
|
licenses: vine.array(vine.number()).minLength(1), // define at least one license for the new dataset
|
|
|
|
rights: vine.string().in(['true']),
|
2023-03-24 10:41:52 +00:00
|
|
|
// second step
|
2024-05-16 11:47:06 +00:00
|
|
|
type: vine.string().trim().minLength(3).maxLength(255),
|
|
|
|
creating_corporation: vine.string().trim().minLength(3).maxLength(255),
|
|
|
|
titles: vine
|
|
|
|
.array(
|
|
|
|
vine.object({
|
|
|
|
value: vine.string().trim().minLength(3).maxLength(255),
|
|
|
|
type: vine.enum(Object.values(TitleTypes)),
|
|
|
|
language: vine
|
|
|
|
.string()
|
|
|
|
.trim()
|
|
|
|
.minLength(2)
|
|
|
|
.maxLength(255)
|
|
|
|
.translatedLanguage({ mainLanguageField: 'language', typeField: 'type' }),
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.minLength(1),
|
|
|
|
descriptions: vine
|
|
|
|
.array(
|
|
|
|
vine.object({
|
|
|
|
value: vine.string().trim().minLength(3).maxLength(255),
|
|
|
|
type: vine.enum(Object.values(DescriptionTypes)),
|
|
|
|
language: vine
|
|
|
|
.string()
|
|
|
|
.trim()
|
|
|
|
.minLength(2)
|
|
|
|
.maxLength(255)
|
|
|
|
.translatedLanguage({ mainLanguageField: 'language', typeField: 'type' }),
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.minLength(1),
|
|
|
|
authors: vine
|
|
|
|
.array(
|
|
|
|
vine.object({
|
2024-06-14 10:38:04 +00:00
|
|
|
email: vine.string().trim().maxLength(255).email().normalizeEmail().isUniquePerson({ table: 'persons', column: 'email', idField: 'id' }),
|
|
|
|
first_name: vine.string().trim().minLength(3).maxLength(255),
|
|
|
|
last_name: vine.string().trim().minLength(3).maxLength(255),
|
2024-05-16 11:47:06 +00:00
|
|
|
}),
|
|
|
|
)
|
2024-06-14 10:38:04 +00:00
|
|
|
.minLength(1)
|
|
|
|
.distinct('email'),
|
2024-05-16 11:47:06 +00:00
|
|
|
contributors: vine
|
|
|
|
.array(
|
|
|
|
vine.object({
|
2024-06-14 10:38:04 +00:00
|
|
|
email: vine.string().trim().maxLength(255).email().normalizeEmail().isUniquePerson({ table: 'persons', column: 'email', idField: 'id' }),
|
|
|
|
first_name: vine.string().trim().minLength(3).maxLength(255),
|
|
|
|
last_name: vine.string().trim().minLength(3).maxLength(255),
|
2024-05-16 11:47:06 +00:00
|
|
|
pivot_contributor_type: vine.enum(Object.keys(ContributorTypes)),
|
|
|
|
}),
|
|
|
|
)
|
2024-06-14 10:38:04 +00:00
|
|
|
.distinct('email')
|
|
|
|
.optional(),
|
2023-03-24 10:41:52 +00:00
|
|
|
// third step
|
2024-05-16 11:47:06 +00:00
|
|
|
project_id: vine.number().optional(),
|
|
|
|
// embargo_date: schema.date.optional({ format: 'yyyy-MM-dd' }, [rules.after(10, 'days')]),
|
|
|
|
embargo_date: vine
|
|
|
|
.date({
|
|
|
|
formats: ['YYYY-MM-DD'],
|
|
|
|
})
|
|
|
|
.afterOrEqual((_field) => {
|
|
|
|
return dayjs().add(10, 'day').format('YYYY-MM-DD');
|
|
|
|
})
|
|
|
|
.optional(),
|
|
|
|
coverage: vine.object({
|
|
|
|
x_min: vine.number(),
|
|
|
|
x_max: vine.number(),
|
|
|
|
y_min: vine.number(),
|
|
|
|
y_max: vine.number(),
|
2024-07-08 11:52:20 +00:00
|
|
|
elevation_absolut: vine.number().positive().optional(),
|
|
|
|
elevation_min: vine.number().positive().optional().requiredIfExists('elevation_max'),
|
|
|
|
elevation_max: vine.number().positive().optional().requiredIfExists('elevation_min'),
|
2024-05-16 11:47:06 +00:00
|
|
|
// type: vine.enum(Object.values(DescriptionTypes)),
|
2024-07-08 11:52:20 +00:00
|
|
|
depth_absolut: vine.number().negative().optional(),
|
|
|
|
depth_min: vine.number().negative().optional().requiredIfExists('depth_max'),
|
|
|
|
depth_max: vine.number().negative().optional().requiredIfExists('depth_min'),
|
2023-05-02 16:10:32 +00:00
|
|
|
}),
|
2024-05-16 11:47:06 +00:00
|
|
|
references: vine
|
|
|
|
.array(
|
|
|
|
vine.object({
|
|
|
|
value: vine.string().trim().minLength(3).maxLength(255),
|
|
|
|
type: vine.enum(Object.values(ReferenceIdentifierTypes)),
|
|
|
|
relation: vine.enum(Object.values(RelationTypes)),
|
|
|
|
label: vine.string().trim().minLength(2).maxLength(255),
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.optional(),
|
|
|
|
subjects: vine
|
|
|
|
.array(
|
|
|
|
vine.object({
|
|
|
|
value: vine.string().trim().minLength(3).maxLength(255),
|
|
|
|
// pivot_contributor_type: vine.enum(Object.keys(ContributorTypes)),
|
|
|
|
language: vine.string().trim().minLength(2).maxLength(255),
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.minLength(3)
|
|
|
|
.distinct('value')
|
|
|
|
.optional(),
|
2023-06-01 12:29:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
2024-05-16 11:47:06 +00:00
|
|
|
// Step 3 - Validate request body against the schema
|
|
|
|
// await request.validate({ schema: newDatasetSchema, messages: this.messages });
|
|
|
|
const validator = vine.compile(newDatasetSchema);
|
|
|
|
await request.validateUsing(validator, { messagesProvider: new SimpleMessagesProvider(this.messages) });
|
2023-06-01 12:29:56 +00:00
|
|
|
// console.log({ payload });
|
|
|
|
} catch (error) {
|
|
|
|
// Step 3 - Handle errors
|
|
|
|
// return response.badRequest(error.messages);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
return response.redirect().back();
|
|
|
|
}
|
2023-08-01 15:06:51 +00:00
|
|
|
|
2024-03-14 19:25:27 +00:00
|
|
|
public async store({ auth, request, response, session }: HttpContext) {
|
2023-06-22 15:20:04 +00:00
|
|
|
// node ace make:validator CreateDataset
|
2023-03-17 15:13:37 +00:00
|
|
|
try {
|
|
|
|
// Step 2 - Validate request body against the schema
|
2023-06-22 15:20:04 +00:00
|
|
|
// await request.validate({ schema: newDatasetSchema, messages: this.messages });
|
2024-05-16 11:47:06 +00:00
|
|
|
// await request.validate(CreateDatasetValidator);
|
|
|
|
await request.validateUsing(createDatasetValidator);
|
2023-03-17 15:13:37 +00:00
|
|
|
// console.log({ payload });
|
|
|
|
} catch (error) {
|
|
|
|
// Step 3 - Handle errors
|
|
|
|
// return response.badRequest(error.messages);
|
|
|
|
throw error;
|
|
|
|
}
|
2023-06-22 15:20:04 +00:00
|
|
|
|
|
|
|
let trx: TransactionClientContract | null = null;
|
|
|
|
try {
|
2024-03-14 19:25:27 +00:00
|
|
|
trx = await db.transaction();
|
2023-06-22 15:20:04 +00:00
|
|
|
const user = (await User.find(auth.user?.id)) as User;
|
|
|
|
|
2023-08-01 15:06:51 +00:00
|
|
|
await this.createDatasetAndAssociations(user, request, trx);
|
2023-06-22 15:20:04 +00:00
|
|
|
|
|
|
|
await trx.commit();
|
2023-08-01 15:06:51 +00:00
|
|
|
console.log('Dataset and related models created successfully');
|
2023-06-22 15:20:04 +00:00
|
|
|
} catch (error) {
|
|
|
|
if (trx !== null) {
|
|
|
|
await trx.rollback();
|
|
|
|
}
|
|
|
|
console.error('Failed to create dataset and related models:', error);
|
2023-09-04 11:24:58 +00:00
|
|
|
// throw new ValidationException(true, { 'upload error': `failed to create dataset and related models. ${error}` });
|
2023-06-22 15:20:04 +00:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2023-06-01 12:29:56 +00:00
|
|
|
session.flash('message', 'Dataset has been created successfully');
|
2023-11-29 15:52:41 +00:00
|
|
|
return response.redirect().toRoute('dataset.list');
|
2023-11-27 16:17:22 +00:00
|
|
|
// return response.redirect().back();
|
2023-03-17 15:13:37 +00:00
|
|
|
}
|
|
|
|
|
2024-03-14 19:25:27 +00:00
|
|
|
private async createDatasetAndAssociations(user: User, request: HttpContext['request'], trx: TransactionClientContract) {
|
2023-08-01 15:06:51 +00:00
|
|
|
// Create a new instance of the Dataset model:
|
|
|
|
const dataset = new Dataset();
|
|
|
|
dataset.type = request.input('type');
|
2023-09-26 15:53:00 +00:00
|
|
|
dataset.creating_corporation = request.input('creating_corporation');
|
2023-08-01 15:06:51 +00:00
|
|
|
dataset.language = request.input('language');
|
2023-09-26 15:53:00 +00:00
|
|
|
dataset.embargo_date = request.input('embargo_date');
|
2023-08-01 15:06:51 +00:00
|
|
|
//await dataset.related('user').associate(user); // speichert schon ab
|
|
|
|
// Dataset.$getRelation('user').boot();
|
|
|
|
// Dataset.$getRelation('user').setRelated(dataset, user);
|
|
|
|
// dataset.$setRelated('user', user);
|
|
|
|
await user.useTransaction(trx).related('datasets').save(dataset);
|
|
|
|
|
|
|
|
//store licenses:
|
|
|
|
const licenses: number[] = request.input('licenses', []);
|
2023-11-22 16:06:55 +00:00
|
|
|
await dataset.useTransaction(trx).related('licenses').sync(licenses);
|
2023-08-01 15:06:51 +00:00
|
|
|
|
|
|
|
// save authors and contributors
|
|
|
|
await this.savePersons(dataset, request.input('authors', []), 'author', trx);
|
|
|
|
await this.savePersons(dataset, request.input('contributors', []), 'contributor', trx);
|
|
|
|
|
|
|
|
//save main and additional titles
|
|
|
|
const titles = request.input('titles', []);
|
|
|
|
for (const titleData of titles) {
|
|
|
|
const title = new Title();
|
|
|
|
title.value = titleData.value;
|
|
|
|
title.language = titleData.language;
|
|
|
|
title.type = titleData.type;
|
|
|
|
await dataset.useTransaction(trx).related('titles').save(title);
|
|
|
|
}
|
|
|
|
|
|
|
|
// save descriptions
|
|
|
|
const descriptions = request.input('descriptions', []);
|
|
|
|
for (const descriptionData of descriptions) {
|
|
|
|
const description = new Description();
|
|
|
|
description.value = descriptionData.value;
|
|
|
|
description.language = descriptionData.language;
|
|
|
|
description.type = descriptionData.type;
|
|
|
|
await dataset.useTransaction(trx).related('descriptions').save(description);
|
|
|
|
}
|
|
|
|
|
|
|
|
//save references
|
|
|
|
const references = request.input('references', []);
|
|
|
|
for (const referencePayload of references) {
|
|
|
|
const dataReference = new DatasetReference();
|
|
|
|
dataReference.fill(referencePayload);
|
|
|
|
// $dataReference = new DatasetReference($reference);
|
|
|
|
dataset.related('references').save(dataReference);
|
|
|
|
}
|
|
|
|
|
|
|
|
//save keywords
|
|
|
|
const keywords = request.input('subjects', []);
|
|
|
|
for (const keywordData of keywords) {
|
|
|
|
// $dataKeyword = new Subject($keyword);
|
|
|
|
// $dataset->subjects()->save($dataKeyword);
|
|
|
|
const keyword = await Subject.firstOrNew({ value: keywordData.value, type: keywordData.type }, keywordData);
|
|
|
|
if (keyword.$isNew === true) {
|
|
|
|
await dataset.useTransaction(trx).related('subjects').save(keyword);
|
|
|
|
} else {
|
|
|
|
await dataset.useTransaction(trx).related('subjects').attach([keyword.id]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-23 15:07:26 +00:00
|
|
|
// save collection
|
|
|
|
const collection: Collection | null = await Collection.query().where('id', 21).first();
|
|
|
|
collection && (await dataset.useTransaction(trx).related('collections').attach([collection.id]));
|
|
|
|
|
2023-08-01 15:06:51 +00:00
|
|
|
// save coverage
|
|
|
|
const coverageData = request.input('coverage');
|
|
|
|
if (coverageData) {
|
|
|
|
// const formCoverage = request.input('coverage');
|
|
|
|
const coverage = new Coverage();
|
|
|
|
coverage.fill(coverageData);
|
|
|
|
// await dataset.coverage().save(coverageData);
|
|
|
|
await dataset.useTransaction(trx).related('coverage').save(coverage);
|
|
|
|
// Alternatively, you can associate the dataset with the coverage and then save it:
|
|
|
|
// await coverage.dataset().associate(dataset).save();
|
|
|
|
// await coverage.useTransaction(trx).related('dataset').associate(dataset);
|
|
|
|
}
|
2023-09-04 11:24:58 +00:00
|
|
|
|
|
|
|
// save data files
|
2024-04-23 17:36:45 +00:00
|
|
|
const uploadedFiles: MultipartFile[] = request.files('files');
|
2023-09-04 11:24:58 +00:00
|
|
|
for (const [index, file] of uploadedFiles.entries()) {
|
|
|
|
try {
|
|
|
|
await this.scanFileForViruses(file.tmpPath); //, 'gitea.lan', 3310);
|
|
|
|
// await this.scanFileForViruses("/tmp/testfile.txt");
|
|
|
|
} catch (error) {
|
|
|
|
// If the file is infected or there's an error scanning the file, throw a validation exception
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
// clientName: 'Gehaltsschema.png'
|
|
|
|
// extname: 'png'
|
2024-04-23 17:36:45 +00:00
|
|
|
// fieldName: 'file'
|
2024-02-06 15:39:33 +00:00
|
|
|
// const fileName = `file-${this.generateRandomString(32)}.${file.extname}`;
|
|
|
|
const fileName = this.generateFilename(file.extname as string);
|
2023-09-04 11:24:58 +00:00
|
|
|
const mimeType = file.headers['content-type'] || 'application/octet-stream'; // Fallback to a default MIME type
|
|
|
|
const datasetFolder = `files/${dataset.id}`;
|
|
|
|
// const size = file.size;
|
2024-04-25 13:17:22 +00:00
|
|
|
await file.move(drive.makePath(datasetFolder), {
|
2024-04-23 17:36:45 +00:00
|
|
|
name: fileName,
|
|
|
|
overwrite: true, // overwrite in case of conflict
|
|
|
|
});
|
2023-09-04 11:24:58 +00:00
|
|
|
// save file metadata into db
|
|
|
|
const newFile = new File();
|
|
|
|
newFile.pathName = `${datasetFolder}/${fileName}`;
|
|
|
|
newFile.fileSize = file.size;
|
|
|
|
newFile.mimeType = mimeType;
|
|
|
|
newFile.label = file.clientName;
|
|
|
|
newFile.sortOrder = index;
|
|
|
|
newFile.visibleInFrontdoor = true;
|
|
|
|
newFile.visibleInOai = true;
|
|
|
|
// let path = coverImage.filePath;
|
|
|
|
await dataset.useTransaction(trx).related('files').save(newFile);
|
2023-11-22 16:06:55 +00:00
|
|
|
await newFile.createHashValues(trx);
|
2023-09-04 11:24:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-06 15:39:33 +00:00
|
|
|
private generateRandomString(length: number): string {
|
2024-04-23 17:36:45 +00:00
|
|
|
return crypto
|
|
|
|
.randomBytes(Math.ceil(length / 2))
|
|
|
|
.toString('hex')
|
|
|
|
.slice(0, length);
|
2024-02-06 15:39:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private generateFilename(extension: string): string {
|
|
|
|
const randomString1 = this.generateRandomString(8);
|
|
|
|
const randomString2 = this.generateRandomString(4);
|
|
|
|
const randomString3 = this.generateRandomString(4);
|
|
|
|
const randomString4 = this.generateRandomString(4);
|
|
|
|
const randomString5 = this.generateRandomString(12);
|
2024-04-23 17:36:45 +00:00
|
|
|
|
2024-02-06 15:39:33 +00:00
|
|
|
return `file-${randomString1}-${randomString2}-${randomString3}-${randomString4}-${randomString5}.${extension}`;
|
2024-04-23 17:36:45 +00:00
|
|
|
}
|
2024-02-06 15:39:33 +00:00
|
|
|
|
2024-03-14 19:25:27 +00:00
|
|
|
private async scanFileForViruses(filePath: string | undefined, host?: string, port?: number): Promise<void> {
|
2023-09-04 11:24:58 +00:00
|
|
|
// const clamscan = await (new ClamScan().init());
|
|
|
|
const opts: ClamScan.Options = {
|
|
|
|
removeInfected: true, // If true, removes infected files
|
|
|
|
debugMode: false, // Whether or not to log info/debug/error msgs to the console
|
|
|
|
scanRecursively: true, // If true, deep scan folders recursively
|
|
|
|
clamdscan: {
|
|
|
|
active: true, // If true, this module will consider using the clamdscan binary
|
|
|
|
host,
|
|
|
|
port,
|
|
|
|
multiscan: true, // Scan using all available cores! Yay!
|
|
|
|
},
|
|
|
|
preference: 'clamdscan', // If clamdscan is found and active, it will be used by default
|
|
|
|
};
|
|
|
|
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
try {
|
|
|
|
const clamscan = await new ClamScan().init(opts);
|
|
|
|
// You can re-use the `clamscan` object as many times as you want
|
|
|
|
// const version = await clamscan.getVersion();
|
|
|
|
// console.log(`ClamAV Version: ${version}`);
|
|
|
|
const { file, isInfected, viruses } = await clamscan.isInfected(filePath);
|
|
|
|
if (isInfected) {
|
|
|
|
console.log(`${file} is infected with ${viruses}!`);
|
2024-05-16 11:47:06 +00:00
|
|
|
// reject(new ValidationException(true, { 'upload error': `File ${file} is infected!` }));
|
|
|
|
reject(new errors.E_VALIDATION_ERROR({ 'upload error': `File ${file} is infected!` }));
|
2023-09-04 11:24:58 +00:00
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
// If there's an error scanning the file, throw a validation exception
|
2024-05-16 11:47:06 +00:00
|
|
|
// reject(new ValidationException(true, { 'upload error': `${error.message}` }));
|
|
|
|
reject(new errors.E_VALIDATION_ERROR({ 'upload error': `${error.message}!` }));
|
2023-09-04 11:24:58 +00:00
|
|
|
}
|
|
|
|
});
|
2023-08-01 15:06:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private async savePersons(dataset: Dataset, persons: any[], role: string, trx: TransactionClientContract) {
|
|
|
|
for (const [key, person] of persons.entries()) {
|
2023-09-12 14:09:04 +00:00
|
|
|
const pivotData = {
|
|
|
|
role: role,
|
|
|
|
sort_order: key + 1,
|
|
|
|
allow_email_contact: false,
|
|
|
|
...this.extractPivotAttributes(person), // Merge pivot attributes here
|
|
|
|
};
|
2023-08-01 15:06:51 +00:00
|
|
|
|
|
|
|
if (person.id !== undefined) {
|
|
|
|
await dataset
|
|
|
|
.useTransaction(trx)
|
|
|
|
.related('persons')
|
|
|
|
.attach({
|
2023-09-12 14:09:04 +00:00
|
|
|
[person.id]: pivotData,
|
2023-08-01 15:06:51 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
const dataPerson = new Person();
|
|
|
|
dataPerson.fill(person);
|
2023-09-12 14:09:04 +00:00
|
|
|
await dataset.useTransaction(trx).related('persons').save(dataPerson, false, pivotData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper function to extract pivot attributes from a person object
|
|
|
|
private extractPivotAttributes(person: any) {
|
2024-03-14 19:25:27 +00:00
|
|
|
const pivotAttributes: Dictionary = {};
|
2023-09-12 14:09:04 +00:00
|
|
|
for (const key in person) {
|
|
|
|
if (key.startsWith('pivot_')) {
|
|
|
|
// pivotAttributes[key] = person[key];
|
|
|
|
const cleanKey = key.replace('pivot_', ''); // Remove 'pivot_' prefix
|
|
|
|
pivotAttributes[cleanKey] = person[key];
|
2023-08-01 15:06:51 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-12 14:09:04 +00:00
|
|
|
return pivotAttributes;
|
2023-08-01 15:06:51 +00:00
|
|
|
}
|
|
|
|
|
2024-05-16 11:47:06 +00:00
|
|
|
public messages = {
|
|
|
|
'minLength': '{{ field }} must be at least {{ min }} characters long',
|
|
|
|
'maxLength': '{{ field }} must be less then {{ max }} characters long',
|
2023-03-24 10:41:52 +00:00
|
|
|
'required': '{{ field }} is required',
|
|
|
|
'unique': '{{ field }} must be unique, and this value is already taken',
|
|
|
|
// 'confirmed': '{{ field }} is not correct',
|
2024-05-16 11:47:06 +00:00
|
|
|
'licenses.minLength': 'at least {{ min }} permission must be defined',
|
2023-09-04 11:24:58 +00:00
|
|
|
'licenses.*.number': 'Define roles as valid numbers',
|
2024-05-16 11:47:06 +00:00
|
|
|
'rights.in': 'you must agree to continue',
|
2023-03-24 10:41:52 +00:00
|
|
|
|
2024-05-16 11:47:06 +00:00
|
|
|
'titles.0.value.minLength': 'Main Title must be at least {{ min }} characters long',
|
2023-03-17 15:13:37 +00:00
|
|
|
'titles.0.value.required': 'Main Title is required',
|
|
|
|
'titles.*.value.required': 'Additional title is required, if defined',
|
|
|
|
'titles.*.type.required': 'Additional title type is required',
|
|
|
|
'titles.*.language.required': 'Additional title language is required',
|
2023-03-24 10:41:52 +00:00
|
|
|
'titles.*.language.translatedLanguage': 'The language of the translated title must be different from the language of the dataset',
|
|
|
|
|
2024-05-16 11:47:06 +00:00
|
|
|
'descriptions.0.value.minLength': 'Main Abstract must be at least {{ min }} characters long',
|
2023-03-17 15:13:37 +00:00
|
|
|
'descriptions.0.value.required': 'Main Abstract is required',
|
|
|
|
'descriptions.*.value.required': 'Additional description is required, if defined',
|
|
|
|
'descriptions.*.type.required': 'Additional description type is required',
|
|
|
|
'descriptions.*.language.required': 'Additional description language is required',
|
2023-05-02 16:10:32 +00:00
|
|
|
'descriptions.*.language.translatedLanguage':
|
|
|
|
'The language of the translated description must be different from the language of the dataset',
|
2023-03-24 10:41:52 +00:00
|
|
|
|
2024-05-16 11:47:06 +00:00
|
|
|
'authors.array.minLength': 'at least {{ min }} author must be defined',
|
2024-06-14 10:38:04 +00:00
|
|
|
'authors.distinct': 'The {{ field }} array must have unique values based on the {{ fields }} attribute.',
|
2023-09-12 14:09:04 +00:00
|
|
|
'contributors.*.pivot_contributor_type.required': 'contributor type is required, if defined',
|
2024-06-14 10:38:04 +00:00
|
|
|
'contributors.distinct': 'The {{ field }} array must have unique values based on the {{ fields }} attribute.',
|
2023-03-24 10:41:52 +00:00
|
|
|
|
|
|
|
'after': `{{ field }} must be older than ${dayjs().add(10, 'day')}`,
|
2023-05-19 09:30:49 +00:00
|
|
|
|
2024-05-16 11:47:06 +00:00
|
|
|
'subjects.array.minLength': 'at least {{ min }} keywords must be defined',
|
2023-05-19 09:30:49 +00:00
|
|
|
'subjects.*.value.required': 'keyword value is required',
|
2024-05-16 11:47:06 +00:00
|
|
|
'subjects.*.value.minLength': 'keyword value must be at least {{ min }} characters long',
|
2023-05-19 09:30:49 +00:00
|
|
|
'subjects.*.type.required': 'keyword type is required',
|
|
|
|
'subjects.*.language.required': 'language of keyword is required',
|
2024-05-16 11:47:06 +00:00
|
|
|
'subjects.distinct': 'The {{ field }} array must have unique values based on the {{ fields }} attribute.',
|
2023-06-01 12:29:56 +00:00
|
|
|
|
2023-08-23 15:07:26 +00:00
|
|
|
'references.*.value.required': 'Additional reference value is required, if defined',
|
|
|
|
'references.*.type.required': 'Additional reference identifier type is required',
|
|
|
|
'references.*.relation.required': 'Additional reference relation type is required',
|
|
|
|
'references.*.label.required': 'Additional reference label is required',
|
|
|
|
|
2024-05-16 11:47:06 +00:00
|
|
|
'files.array.minLength': 'At least {{ min }} file upload is required.',
|
2023-06-16 14:44:28 +00:00
|
|
|
'files.*.size': 'file size is to big',
|
2024-05-16 11:47:06 +00:00
|
|
|
'files.*.extnames': 'file extension is not supported',
|
2023-03-24 10:41:52 +00:00
|
|
|
};
|
2023-06-27 16:23:18 +00:00
|
|
|
|
|
|
|
// public async release({ params, view }) {
|
2024-03-14 19:25:27 +00:00
|
|
|
public async release({ request, inertia, response }: HttpContext) {
|
2023-06-27 16:23:18 +00:00
|
|
|
const id = request.param('id');
|
|
|
|
|
|
|
|
const dataset = await Dataset.query()
|
|
|
|
.preload('user', (builder) => {
|
|
|
|
builder.select('id', 'login');
|
|
|
|
})
|
|
|
|
.where('id', id)
|
|
|
|
.firstOrFail();
|
|
|
|
|
2023-07-17 17:13:30 +00:00
|
|
|
const validStates = ['inprogress', 'rejected_editor'];
|
|
|
|
if (!validStates.includes(dataset.server_state)) {
|
|
|
|
// session.flash('errors', 'Invalid server state!');
|
|
|
|
return response
|
|
|
|
.flash(
|
|
|
|
'warning',
|
|
|
|
`Invalid server state. Dataset with id ${id} cannot be released to editor. Datset has server state ${dataset.server_state}.`,
|
|
|
|
)
|
|
|
|
.redirect()
|
|
|
|
.back();
|
|
|
|
}
|
2023-06-27 16:23:18 +00:00
|
|
|
|
|
|
|
return inertia.render('Submitter/Dataset/Release', {
|
|
|
|
dataset,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-03-14 19:25:27 +00:00
|
|
|
public async releaseUpdate({ request, response }: HttpContext) {
|
2023-06-27 16:23:18 +00:00
|
|
|
const id = request.param('id');
|
|
|
|
const dataset = await Dataset.query().preload('files').where('id', id).firstOrFail();
|
2023-07-17 17:13:30 +00:00
|
|
|
|
|
|
|
const validStates = ['inprogress', 'rejected_editor'];
|
|
|
|
if (!validStates.includes(dataset.server_state)) {
|
|
|
|
// throw new Error('Invalid server state!');
|
|
|
|
// return response.flash('warning', 'Invalid server state. Dataset cannot be released to editor').redirect().back();
|
2023-07-27 12:53:34 +00:00
|
|
|
return response
|
|
|
|
.flash(
|
|
|
|
'warning',
|
|
|
|
`Invalid server state. Dataset with id ${id} cannot be released to editor. Datset has server state ${dataset.server_state}.`,
|
|
|
|
)
|
|
|
|
.redirect()
|
|
|
|
.toRoute('dataset.list');
|
2023-07-17 17:13:30 +00:00
|
|
|
}
|
|
|
|
|
2023-06-27 16:23:18 +00:00
|
|
|
if (dataset.files.length === 0) {
|
2023-07-17 17:13:30 +00:00
|
|
|
return response.flash('warning', 'At least minimum one file is required.').redirect('back');
|
2023-06-27 16:23:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const preferation = request.input('preferation', '');
|
|
|
|
const preferredReviewer = request.input('preferred_reviewer');
|
|
|
|
const preferredReviewerEmail = request.input('preferred_reviewer_email');
|
|
|
|
|
|
|
|
if (preferation === 'yes_preferation') {
|
2024-05-16 11:47:06 +00:00
|
|
|
const newSchema = vine.object({
|
|
|
|
preferred_reviewer: vine.string().alphaNumeric().trim().minLength(3).maxLength(255),
|
|
|
|
preferred_reviewer_email: vine.string().maxLength(255).email().normalizeEmail(),
|
2023-06-27 16:23:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
2024-05-16 11:47:06 +00:00
|
|
|
// await request.validate({
|
|
|
|
// schema: newSchema,
|
|
|
|
// // reporter: validator.reporters.vanilla,
|
|
|
|
// });
|
|
|
|
const validator = vine.compile(newSchema);
|
|
|
|
await request.validateUsing(validator);
|
2023-06-27 16:23:18 +00:00
|
|
|
} catch (error) {
|
|
|
|
// return response.badRequest(error.messages);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const input = {
|
|
|
|
preferred_reviewer: preferredReviewer || null,
|
|
|
|
preferred_reviewer_email: preferredReviewerEmail || null,
|
|
|
|
server_state: 'released',
|
|
|
|
editor_id: null,
|
|
|
|
reviewer_id: null,
|
|
|
|
reject_editor_note: null,
|
|
|
|
reject_reviewer_note: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Clear editor_id if it exists
|
|
|
|
if (dataset.editor_id !== null) {
|
|
|
|
input.editor_id = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear reject_editor_note if it exists
|
|
|
|
if (dataset.reject_editor_note !== null) {
|
|
|
|
input.reject_editor_note = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear reviewer_id if it exists
|
|
|
|
if (dataset.reviewer_id !== null) {
|
|
|
|
input.reviewer_id = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear reject_reviewer_note if it exists
|
|
|
|
if (dataset.reject_reviewer_note !== null) {
|
|
|
|
input.reject_reviewer_note = null;
|
|
|
|
}
|
|
|
|
|
2023-07-17 17:13:30 +00:00
|
|
|
if (await dataset.merge(input).save()) {
|
|
|
|
return response.toRoute('dataset.list').flash('message', 'You have released your dataset!');
|
|
|
|
}
|
2023-06-27 16:23:18 +00:00
|
|
|
// throw new GeneralException(trans('exceptions.publish.release.update_error'));
|
|
|
|
}
|
2023-07-17 17:13:30 +00:00
|
|
|
|
2024-03-14 19:25:27 +00:00
|
|
|
public async edit({ request, inertia, response }: HttpContext) {
|
2023-11-22 16:06:55 +00:00
|
|
|
const id = request.param('id');
|
|
|
|
const datasetQuery = Dataset.query().where('id', id);
|
|
|
|
datasetQuery
|
|
|
|
.preload('titles', (query) => query.orderBy('id', 'asc'))
|
|
|
|
.preload('descriptions', (query) => query.orderBy('id', 'asc'))
|
|
|
|
.preload('coverage')
|
|
|
|
.preload('licenses')
|
|
|
|
.preload('authors')
|
|
|
|
.preload('contributors')
|
2023-11-27 16:17:22 +00:00
|
|
|
// .preload('subjects')
|
|
|
|
.preload('subjects', (builder) => {
|
|
|
|
builder.orderBy('id', 'asc').withCount('datasets');
|
|
|
|
})
|
2023-11-22 16:06:55 +00:00
|
|
|
.preload('references')
|
|
|
|
.preload('files');
|
2023-07-17 17:13:30 +00:00
|
|
|
|
2023-11-22 16:06:55 +00:00
|
|
|
const dataset = await datasetQuery.firstOrFail();
|
|
|
|
const validStates = ['inprogress', 'rejected_editor'];
|
|
|
|
if (!validStates.includes(dataset.server_state)) {
|
|
|
|
// session.flash('errors', 'Invalid server state!');
|
|
|
|
return response
|
|
|
|
.flash(
|
|
|
|
'warning',
|
|
|
|
`Invalid server state. Dataset with id ${id} cannot be edited. Datset has server state ${dataset.server_state}.`,
|
|
|
|
)
|
|
|
|
.redirect()
|
|
|
|
.toRoute('dataset.list');
|
|
|
|
}
|
2023-07-17 17:13:30 +00:00
|
|
|
|
|
|
|
const titleTypes = Object.entries(TitleTypes)
|
2023-11-22 16:06:55 +00:00
|
|
|
.filter(([value]) => value !== 'Main')
|
2023-07-17 17:13:30 +00:00
|
|
|
.map(([key, value]) => ({ value: key, label: value }));
|
|
|
|
|
|
|
|
const descriptionTypes = Object.entries(DescriptionTypes)
|
2023-11-22 16:06:55 +00:00
|
|
|
.filter(([value]) => value !== 'Abstract')
|
2023-07-17 17:13:30 +00:00
|
|
|
.map(([key, value]) => ({ value: key, label: value }));
|
|
|
|
|
|
|
|
const languages = await Language.query().where('active', true).pluck('part1', 'part1');
|
|
|
|
|
|
|
|
// const contributorTypes = Config.get('enums.contributor_types');
|
|
|
|
const contributorTypes = Object.entries(ContributorTypes).map(([key, value]) => ({ value: key, label: value }));
|
|
|
|
|
|
|
|
// const nameTypes = Config.get('enums.name_types');
|
|
|
|
const nameTypes = Object.entries(PersonNameTypes).map(([key, value]) => ({ value: key, label: value }));
|
|
|
|
|
|
|
|
// const messages = await Database.table('messages')
|
|
|
|
// .pluck('help_text', 'metadata_element');
|
|
|
|
|
|
|
|
const projects = await Project.query().pluck('label', 'id');
|
|
|
|
|
|
|
|
const currentDate = new Date();
|
|
|
|
const currentYear = currentDate.getFullYear();
|
|
|
|
const years = Array.from({ length: currentYear - 1990 + 1 }, (_, index) => 1990 + index);
|
|
|
|
|
2023-11-22 16:06:55 +00:00
|
|
|
const licenses = await License.query().select('id', 'name_long').where('active', 'true').pluck('name_long', 'id');
|
|
|
|
// const userHasRoles = user.roles;
|
|
|
|
// const datasetHasLicenses = await dataset.related('licenses').query().pluck('id');
|
2023-07-17 17:13:30 +00:00
|
|
|
// const checkeds = dataset.licenses.first().id;
|
|
|
|
|
|
|
|
const doctypes = {
|
|
|
|
analysisdata: { label: 'Analysis', value: 'analysisdata' },
|
|
|
|
measurementdata: { label: 'Measurements', value: 'measurementdata' },
|
|
|
|
monitoring: 'Monitoring',
|
|
|
|
remotesensing: 'Remote Sensing',
|
|
|
|
gis: 'GIS',
|
|
|
|
models: 'Models',
|
|
|
|
mixedtype: 'Mixed Type',
|
|
|
|
};
|
|
|
|
|
|
|
|
return inertia.render('Submitter/Dataset/Edit', {
|
|
|
|
dataset,
|
2023-07-27 12:53:34 +00:00
|
|
|
titletypes: titleTypes,
|
2023-07-28 15:08:20 +00:00
|
|
|
descriptiontypes: descriptionTypes,
|
2023-07-17 17:13:30 +00:00
|
|
|
contributorTypes,
|
|
|
|
nameTypes,
|
|
|
|
languages,
|
|
|
|
// messages,
|
|
|
|
projects,
|
|
|
|
licenses,
|
2023-11-22 16:06:55 +00:00
|
|
|
// datasetHasLicenses: Object.keys(datasetHasLicenses).map((key) => datasetHasLicenses[key]), //convert object to array with license ids
|
2023-07-17 17:13:30 +00:00
|
|
|
// checkeds,
|
|
|
|
years,
|
|
|
|
// languages,
|
2023-11-22 16:06:55 +00:00
|
|
|
subjectTypes: SubjectTypes,
|
|
|
|
referenceIdentifierTypes: Object.entries(ReferenceIdentifierTypes).map(([key, value]) => ({ value: key, label: value })),
|
|
|
|
relationTypes: Object.entries(RelationTypes).map(([key, value]) => ({ value: key, label: value })),
|
2023-07-17 17:13:30 +00:00
|
|
|
doctypes,
|
|
|
|
});
|
|
|
|
}
|
2023-09-05 16:18:42 +00:00
|
|
|
|
2024-03-14 19:25:27 +00:00
|
|
|
public async update({ request, response, session }: HttpContext) {
|
2023-11-22 16:06:55 +00:00
|
|
|
try {
|
2024-05-16 11:47:06 +00:00
|
|
|
// await request.validate(UpdateDatasetValidator);
|
|
|
|
await request.validateUsing(updateDatasetValidator);
|
2023-11-22 16:06:55 +00:00
|
|
|
} catch (error) {
|
|
|
|
// - Handle errors
|
|
|
|
// return response.badRequest(error.messages);
|
|
|
|
throw error;
|
|
|
|
// return response.badRequest(error.messages);
|
|
|
|
}
|
2023-11-27 16:17:22 +00:00
|
|
|
// await request.validate(UpdateDatasetValidator);
|
2023-11-22 16:06:55 +00:00
|
|
|
const id = request.param('id');
|
|
|
|
|
|
|
|
let trx: TransactionClientContract | null = null;
|
|
|
|
try {
|
2024-03-14 19:25:27 +00:00
|
|
|
trx = await db.transaction();
|
2023-11-22 16:06:55 +00:00
|
|
|
// const user = (await User.find(auth.user?.id)) as User;
|
|
|
|
// await this.createDatasetAndAssociations(user, request, trx);
|
|
|
|
const dataset = await Dataset.findOrFail(id);
|
|
|
|
|
|
|
|
// save the licenses
|
|
|
|
const licenses: number[] = request.input('licenses', []);
|
|
|
|
// await dataset.useTransaction(trx).related('licenses').sync(licenses);
|
|
|
|
await dataset.useTransaction(trx).related('licenses').sync(licenses);
|
|
|
|
|
|
|
|
// save authors and contributors
|
|
|
|
await dataset.useTransaction(trx).related('authors').sync([]);
|
|
|
|
await dataset.useTransaction(trx).related('contributors').sync([]);
|
|
|
|
await this.savePersons(dataset, request.input('authors', []), 'author', trx);
|
|
|
|
await this.savePersons(dataset, request.input('contributors', []), 'contributor', trx);
|
|
|
|
|
|
|
|
//save the titles:
|
|
|
|
const titles = request.input('titles', []);
|
|
|
|
// const savedTitles:Array<Title> = [];
|
|
|
|
for (const titleData of titles) {
|
|
|
|
if (titleData.id) {
|
|
|
|
const title = await Title.findOrFail(titleData.id);
|
|
|
|
title.value = titleData.value;
|
|
|
|
title.language = titleData.language;
|
|
|
|
title.type = titleData.type;
|
|
|
|
if (title.$isDirty) {
|
|
|
|
await title.useTransaction(trx).save();
|
|
|
|
// await dataset.useTransaction(trx).related('titles').save(title);
|
|
|
|
// savedTitles.push(title);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const title = new Title();
|
|
|
|
title.fill(titleData);
|
|
|
|
// savedTitles.push(title);
|
|
|
|
await dataset.useTransaction(trx).related('titles').save(title);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// save the abstracts
|
|
|
|
const descriptions = request.input('descriptions', []);
|
|
|
|
// const savedTitles:Array<Title> = [];
|
|
|
|
for (const descriptionData of descriptions) {
|
|
|
|
if (descriptionData.id) {
|
|
|
|
const description = await Description.findOrFail(descriptionData.id);
|
|
|
|
description.value = descriptionData.value;
|
|
|
|
description.language = descriptionData.language;
|
|
|
|
description.type = descriptionData.type;
|
|
|
|
if (description.$isDirty) {
|
|
|
|
await description.useTransaction(trx).save();
|
|
|
|
// await dataset.useTransaction(trx).related('titles').save(title);
|
|
|
|
// savedTitles.push(title);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const description = new Description();
|
|
|
|
description.fill(descriptionData);
|
|
|
|
// savedTitles.push(title);
|
|
|
|
await dataset.useTransaction(trx).related('descriptions').save(description);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-27 16:17:22 +00:00
|
|
|
// await dataset.useTransaction(trx).related('subjects').sync([]);
|
|
|
|
const keywords = request.input('subjects');
|
|
|
|
for (const keywordData of keywords) {
|
|
|
|
if (keywordData.id) {
|
|
|
|
const subject = await Subject.findOrFail(keywordData.id);
|
|
|
|
// await dataset.useTransaction(trx).related('subjects').attach([keywordData.id]);
|
|
|
|
subject.value = keywordData.value;
|
|
|
|
subject.type = keywordData.type;
|
|
|
|
subject.external_key = keywordData.external_key;
|
|
|
|
if (subject.$isDirty) {
|
|
|
|
await subject.save();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const keyword = new Subject();
|
|
|
|
keyword.fill(keywordData);
|
|
|
|
await dataset.useTransaction(trx).related('subjects').save(keyword, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-22 16:06:55 +00:00
|
|
|
// Save already existing files
|
|
|
|
const files = request.input('fileInputs', []);
|
|
|
|
for (const fileData of files) {
|
|
|
|
if (fileData.id) {
|
|
|
|
const file = await File.findOrFail(fileData.id);
|
|
|
|
file.label = fileData.label;
|
|
|
|
file.sortOrder = fileData.sort_order;
|
|
|
|
if (file.$isDirty) {
|
|
|
|
await file.useTransaction(trx).save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle new uploaded files:
|
2024-03-14 19:25:27 +00:00
|
|
|
const uploadedFiles: MultipartFile[] = request.files('files');
|
2023-11-22 16:06:55 +00:00
|
|
|
if (Array.isArray(uploadedFiles) && uploadedFiles.length > 0) {
|
|
|
|
for (const [index, fileData] of uploadedFiles.entries()) {
|
2023-11-30 12:40:32 +00:00
|
|
|
try {
|
|
|
|
await this.scanFileForViruses(fileData.tmpPath); //, 'gitea.lan', 3310);
|
|
|
|
// await this.scanFileForViruses("/tmp/testfile.txt");
|
|
|
|
} catch (error) {
|
|
|
|
// If the file is infected or there's an error scanning the file, throw a validation exception
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// move to disk:
|
2024-04-25 13:17:22 +00:00
|
|
|
const fileName = `file-${cuid()}.${fileData.extname}`; //'file-ls0jyb8xbzqtrclufu2z2e0c.pdf'
|
|
|
|
const datasetFolder = `files/${dataset.id}`; // 'files/307'
|
|
|
|
// await fileData.moveToDisk(datasetFolder, { name: fileName, overwrite: true }, 'local');
|
|
|
|
await fileData.move(drive.makePath(datasetFolder), {
|
|
|
|
name: fileName,
|
|
|
|
overwrite: true, // overwrite in case of conflict
|
|
|
|
});
|
2023-11-27 16:17:22 +00:00
|
|
|
|
2023-11-30 12:40:32 +00:00
|
|
|
//save to db:
|
2023-11-27 16:17:22 +00:00
|
|
|
const { clientFileName, sortOrder } = this.extractVariableNameAndSortOrder(fileData.clientName);
|
|
|
|
const mimeType = fileData.headers['content-type'] || 'application/octet-stream'; // Fallback to a default MIME type
|
|
|
|
const newFile = await dataset
|
|
|
|
.useTransaction(trx)
|
|
|
|
.related('files')
|
|
|
|
.create({
|
|
|
|
pathName: `${datasetFolder}/${fileName}`,
|
|
|
|
fileSize: fileData.size,
|
|
|
|
mimeType,
|
|
|
|
label: clientFileName,
|
|
|
|
sortOrder: sortOrder || index,
|
|
|
|
visibleInFrontdoor: true,
|
|
|
|
visibleInOai: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
// save many related HashValue Instances to the file:
|
|
|
|
await newFile.createHashValues(trx);
|
2023-11-22 16:06:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-27 16:17:22 +00:00
|
|
|
// save collection
|
|
|
|
// const collection: Collection | null = await Collection.query().where('id', 21).first();
|
|
|
|
// collection && (await dataset.useTransaction(trx).related('collections').attach([collection.id]));
|
|
|
|
|
|
|
|
// // Save coverage
|
|
|
|
// if (data.coverage && !this.containsOnlyNull(data.coverage)) {
|
|
|
|
// const formCoverage = request.input('coverage');
|
|
|
|
// const coverage = await dataset.related('coverage').updateOrCreate({ dataset_id: dataset.id }, formCoverage);
|
|
|
|
// } else if (data.coverage && this.containsOnlyNull(data.coverage) && !dataset.coverage) {
|
|
|
|
// await dataset.coverage().delete();
|
|
|
|
// }
|
|
|
|
|
2023-11-22 16:06:55 +00:00
|
|
|
const input = request.only(['project_id', 'embargo_date', 'language', 'type', 'creating_corporation']);
|
|
|
|
// dataset.type = request.input('type');
|
|
|
|
dataset.merge(input);
|
|
|
|
// let test: boolean = dataset.$isDirty;
|
|
|
|
await dataset.useTransaction(trx).save();
|
|
|
|
|
|
|
|
await trx.commit();
|
|
|
|
console.log('Dataset and related models created successfully');
|
|
|
|
} catch (error) {
|
|
|
|
if (trx !== null) {
|
|
|
|
await trx.rollback();
|
|
|
|
}
|
|
|
|
console.error('Failed to create dataset and related models:', error);
|
|
|
|
// throw new ValidationException(true, { 'upload error': `failed to create dataset and related models. ${error}` });
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2023-11-27 16:17:22 +00:00
|
|
|
session.flash('message', 'Dataset has been updated successfully');
|
2023-11-22 16:06:55 +00:00
|
|
|
// return response.redirect().toRoute('user.index');
|
|
|
|
return response.redirect().back();
|
|
|
|
}
|
|
|
|
|
2023-11-27 16:17:22 +00:00
|
|
|
private extractVariableNameAndSortOrder(inputString: string): { clientFileName: string; sortOrder?: number } {
|
|
|
|
const regex = /^([^?]+)(?:\?([^=]+)=([^&]+))?/;
|
|
|
|
const match = inputString.match(regex);
|
|
|
|
|
|
|
|
if (match) {
|
|
|
|
const clientFileName = match[1];
|
|
|
|
|
|
|
|
const param = match[2];
|
|
|
|
let sortOrder;
|
|
|
|
if (param && param.toLowerCase() === 'sortorder') {
|
|
|
|
sortOrder = parseInt(match[3], 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
return { clientFileName, sortOrder };
|
|
|
|
} else {
|
|
|
|
return { clientFileName: '', sortOrder: undefined }; // Or handle as needed for no match
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-14 19:25:27 +00:00
|
|
|
public async delete({ request, inertia, response, session }: HttpContext) {
|
2023-09-05 16:18:42 +00:00
|
|
|
const id = request.param('id');
|
|
|
|
try {
|
|
|
|
const dataset = await Dataset.query()
|
|
|
|
.preload('user', (builder) => {
|
|
|
|
builder.select('id', 'login');
|
|
|
|
})
|
|
|
|
.where('id', id)
|
2023-11-27 16:17:22 +00:00
|
|
|
.preload('files')
|
2023-09-05 16:18:42 +00:00
|
|
|
.firstOrFail();
|
|
|
|
const validStates = ['inprogress', 'rejected_editor'];
|
|
|
|
if (!validStates.includes(dataset.server_state)) {
|
|
|
|
// session.flash('errors', 'Invalid server state!');
|
|
|
|
return response
|
|
|
|
.flash(
|
|
|
|
'warning',
|
|
|
|
`Invalid server state. Dataset with id ${id} cannot be deleted. Datset has server state ${dataset.server_state}.`,
|
|
|
|
)
|
|
|
|
.redirect()
|
|
|
|
.toRoute('dataset.list');
|
|
|
|
}
|
|
|
|
return inertia.render('Submitter/Dataset/Delete', {
|
|
|
|
dataset,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
if (error.code == 'E_ROW_NOT_FOUND') {
|
|
|
|
session.flash({ warning: 'Dataset is not found in database' });
|
|
|
|
} else {
|
|
|
|
session.flash({ warning: 'general error occured, you cannot delete the dataset' });
|
|
|
|
}
|
|
|
|
return response.redirect().toRoute('dataset.list');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-14 19:25:27 +00:00
|
|
|
public async deleteUpdate({ params, session, response }: HttpContext) {
|
2023-09-05 16:18:42 +00:00
|
|
|
try {
|
|
|
|
const dataset = await Dataset.query().where('id', params.id).preload('files').firstOrFail();
|
|
|
|
|
|
|
|
const validStates = ['inprogress', 'rejected_editor'];
|
|
|
|
if (validStates.includes(dataset.server_state)) {
|
|
|
|
if (dataset.files && dataset.files.length > 0) {
|
|
|
|
for (const file of dataset.files) {
|
2023-11-29 15:52:41 +00:00
|
|
|
// overwritten delete method also delets file on filespace
|
2023-11-27 16:17:22 +00:00
|
|
|
await file.delete();
|
2023-09-05 16:18:42 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-27 16:17:22 +00:00
|
|
|
const datasetFolder = `files/${params.id}`;
|
2024-04-23 17:36:45 +00:00
|
|
|
const folderExists = await drive.exists(datasetFolder);
|
2023-11-27 16:17:22 +00:00
|
|
|
if (folderExists) {
|
2024-04-23 17:36:45 +00:00
|
|
|
const dirListing = drive.list(datasetFolder);
|
|
|
|
const folderContents = await dirListing.toArray();
|
2023-11-27 16:17:22 +00:00
|
|
|
if (folderContents.length === 0) {
|
2024-04-23 17:36:45 +00:00
|
|
|
await drive.delete(datasetFolder);
|
2023-11-27 16:17:22 +00:00
|
|
|
}
|
2024-03-14 19:25:27 +00:00
|
|
|
// delete dataset wirh relation in db
|
2023-11-27 16:17:22 +00:00
|
|
|
await dataset.delete();
|
|
|
|
session.flash({ message: 'You have deleted 1 dataset!' });
|
|
|
|
return response.redirect().toRoute('dataset.list');
|
|
|
|
} else {
|
2024-04-23 17:36:45 +00:00
|
|
|
// session.flash({
|
|
|
|
// warning: `You cannot delete this dataset! Invalid server_state: "${dataset.server_state}"!`,
|
|
|
|
// });
|
|
|
|
return response
|
|
|
|
.flash({ warning: `You cannot delete this dataset! Dataset folder "${datasetFolder}" doesn't exist!` })
|
|
|
|
.redirect()
|
|
|
|
.back();
|
2023-11-27 16:17:22 +00:00
|
|
|
}
|
2023-09-05 16:18:42 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2024-05-16 11:47:06 +00:00
|
|
|
if (error instanceof errors.E_VALIDATION_ERROR) {
|
2023-09-05 16:18:42 +00:00
|
|
|
// Validation exception handling
|
|
|
|
throw error;
|
|
|
|
} else if (error instanceof Exception) {
|
|
|
|
// General exception handling
|
2024-04-23 17:36:45 +00:00
|
|
|
return response.flash('errors', { error: error.message }).redirect().back();
|
2023-09-05 16:18:42 +00:00
|
|
|
} else {
|
|
|
|
session.flash({ error: 'An error occurred while deleting the dataset.' });
|
|
|
|
return response.redirect().back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-17 15:13:37 +00:00
|
|
|
}
|