Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 8.1 snapshot #293

Merged
merged 11 commits into from
Feb 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

package org.projectforge.plugins.banking

import jakarta.servlet.http.HttpServletRequest
import mu.KotlinLogging
import org.projectforge.Constants
import org.projectforge.common.FormatterUtils
Expand All @@ -37,63 +38,64 @@ import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile
import jakarta.servlet.http.HttpServletRequest

private val log = KotlinLogging.logger {}

@RestController
@RequestMapping(BankingServicesRest.REST_PATH)
class BankingServicesRest {
@Autowired
private lateinit var bankAccountDao: BankAccountDao
@Autowired
private lateinit var bankAccountDao: BankAccountDao

@Autowired
private lateinit var transactionsImporter: TransactionsImporter
@Autowired
private lateinit var transactionsImporter: TransactionsImporter

@PostMapping("import/{id}")
fun import(
request: HttpServletRequest,
@PathVariable("id", required = true) id: Int,
@RequestParam("file") file: MultipartFile
): ResponseEntity<*> {
val filename = file.originalFilename ?: "unknown"
log.info {
"User tries to upload serial execution file: id='$id', filename='$filename', size=${
FormatterUtils.formatBytes(
file.size
@PostMapping("import/{id}")
fun import(
request: HttpServletRequest,
@PathVariable("id", required = true) id: Int,
@RequestParam("file") file: MultipartFile
): ResponseEntity<*> {
val filename = file.originalFilename ?: "unknown"
log.info {
"User tries to upload serial execution file: id='$id', filename='$filename', size=${
FormatterUtils.formatBytes(
file.size
)
}."
}
if (file.size > 100 * Constants.MB) {
log.warn("Upload file size to big: ${file.size} > 100MB")
throw IllegalArgumentException("Upload file size to big: ${file.size} > 100MB")
}
val bankAccountDO = bankAccountDao.find(id)
if (bankAccountDO == null) {
log.warn("Bank account with id #$id not found.")
throw IllegalArgumentException()
}
val bankAccount = BankAccount()
bankAccount.copyFrom(bankAccountDO)
log.info("Importing transactions for bank account #$id, iban=${bankAccount.iban}")
var importStorage: BankingImportStorage? = null
if (filename.endsWith("xls", ignoreCase = true) || filename.endsWith("xlsx", ignoreCase = true)) {
throw IllegalArgumentException("Excel not yet supported.")
} else {
importStorage = BankingImportStorage(bankAccount.importSettings, bankAccount)
// Try to import CSV
file.inputStream.use {
CsvImporter.parse(it, importStorage, importStorage.importSettings.charSet)
}
}
transactionsImporter.import(request, bankAccountDO, importStorage)
return ResponseEntity(
ResponseAction(
PagesResolver.getDynamicPageUrl(BankAccountRecordImportPageRest::class.java, absolute = true),
targetType = TargetType.REDIRECT
), HttpStatus.OK
)
}."
}
if (file.size > 100 * Constants.MB) {
log.warn("Upload file size to big: ${file.size} > 100MB")
throw IllegalArgumentException("Upload file size to big: ${file.size} > 100MB")
}
val bankAccountDO = bankAccountDao.find(id)
if (bankAccountDO == null) {
log.warn("Bank account with id #$id not found.")
throw IllegalArgumentException()
}
val bankAccount = BankAccount()
bankAccount.copyFrom(bankAccountDO)
log.info("Importing transactions for bank account #$id, iban=${bankAccount.iban}")
var importStorage: BankingImportStorage? = null
if (filename.endsWith("xls", ignoreCase = true) || filename.endsWith("xlsx", ignoreCase = true)) {
throw IllegalArgumentException("Excel not yet supported.")
} else {
importStorage = BankingImportStorage(bankAccount.importSettings, bankAccount)
// Try to import CSV
CsvImporter.parse(file.inputStream, importStorage, importStorage.importSettings.charSet)
}
transactionsImporter.import(request, bankAccountDO, importStorage)
return ResponseEntity(
ResponseAction(
PagesResolver.getDynamicPageUrl(BankAccountRecordImportPageRest::class.java, absolute = true),
targetType = TargetType.REDIRECT
), HttpStatus.OK
)
}

companion object {
const val REST_PATH = "${Rest.URL}/banking"
}
companion object {
const val REST_PATH = "${Rest.URL}/banking"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

package org.projectforge.plugins.datatransfer.rest

import jakarta.servlet.http.HttpServletResponse
import org.projectforge.framework.configuration.ApplicationContextProvider
import org.projectforge.framework.jcr.Attachment
import org.projectforge.framework.jcr.AttachmentsAccessChecker
Expand All @@ -32,48 +33,41 @@ import org.projectforge.framework.persistence.user.entities.PFUserDO
import org.projectforge.plugins.datatransfer.DataTransferAreaDO
import org.projectforge.plugins.datatransfer.DataTransferAuditDao
import org.projectforge.rest.AttachmentsRestUtils
import jakarta.servlet.http.HttpServletResponse

object DataTransferRestUtils {
/**
* @param attachments If not given, all attachments will be downloaded, otherwise only these given attachments.
*/
fun multiDownload(
response: HttpServletResponse,
attachmentsService: AttachmentsService,
attachmentsAccessChecker: AttachmentsAccessChecker,
dbObj: DataTransferAreaDO,
areaName: String?,
jcrPath: String,
id: Long,
attachments: List<Attachment>? = null,
byUser: PFUserDO? = null,
byExternalUser: String? = null,
) {
AttachmentsRestUtils.multiDownload(
response,
attachmentsService,
attachmentsAccessChecker,
areaName,
jcrPath,
id,
attachments,
)
dataTransferAuditDao.insertAudit(
if (attachments.isNullOrEmpty()) AttachmentsEventType.DOWNLOAD_ALL else AttachmentsEventType.DOWNLOAD_MULTI,
dbObj,
byUser = byUser,
byExternalUser = byExternalUser,
)
}

val dataTransferAuditDao: DataTransferAuditDao
get() {
if (_dataTransferAuditDao == null) {
_dataTransferAuditDao = ApplicationContextProvider.getApplicationContext().getBean(DataTransferAuditDao::class.java)
}
return _dataTransferAuditDao!!
}
/**
* @param attachments If not given, all attachments will be downloaded, otherwise only these given attachments.
*/
fun multiDownload(
response: HttpServletResponse,
attachmentsService: AttachmentsService,
attachmentsAccessChecker: AttachmentsAccessChecker,
dbObj: DataTransferAreaDO,
areaName: String?,
jcrPath: String,
id: Long,
attachments: List<Attachment>? = null,
byUser: PFUserDO? = null,
byExternalUser: String? = null,
) {
AttachmentsRestUtils.multiDownload(
response,
attachmentsService,
attachmentsAccessChecker,
areaName,
jcrPath,
id,
attachments,
)
dataTransferAuditDao.insertAudit(
if (attachments.isNullOrEmpty()) AttachmentsEventType.DOWNLOAD_ALL else AttachmentsEventType.DOWNLOAD_MULTI,
dbObj,
byUser = byUser,
byExternalUser = byExternalUser,
)
}

private var _dataTransferAuditDao: DataTransferAuditDao? = null
val dataTransferAuditDao: DataTransferAuditDao by lazy {
ApplicationContextProvider.getApplicationContext().getBean(DataTransferAuditDao::class.java)
}
}
Loading