diff --git a/src/main/java/org/olat/core/commons/services/vfs/manager/AsyncIncrementFileDownloadEvent.java b/src/main/java/org/olat/core/commons/services/vfs/manager/AsyncIncrementFileDownloadEvent.java new file mode 100644 index 0000000000000000000000000000000000000000..9418089280ab9947fd7c23886780d250cc9f6d5d --- /dev/null +++ b/src/main/java/org/olat/core/commons/services/vfs/manager/AsyncIncrementFileDownloadEvent.java @@ -0,0 +1,51 @@ +/** + * <a href="http://www.openolat.org"> + * OpenOLAT - Online Learning and Training</a><br> + * <p> + * Licensed under the Apache License, Version 2.0 (the "License"); <br> + * you may not use this file except in compliance with the License.<br> + * You may obtain a copy of the License at the + * <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a> + * <p> + * Unless required by applicable law or agreed to in writing,<br> + * software distributed under the License is distributed on an "AS IS" BASIS, <br> + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br> + * See the License for the specific language governing permissions and <br> + * limitations under the License. + * <p> + * Initial code contributed and copyrighted by<br> + * frentix GmbH, http://www.frentix.com + * <p> + */ +package org.olat.core.commons.services.vfs.manager; + +import org.olat.core.util.event.MultiUserEvent; + +/** + * + * Initial date: 15 mars 2019<br> + * @author srosse, stephane.rosse@frentix.com, http://www.frentix.com + * + */ +public class AsyncIncrementFileDownloadEvent extends MultiUserEvent { + + private static final long serialVersionUID = 5439824954399187557L; + public static final String ASYNC_FILE_SIZE = "async-file-size-updates"; + + private String filename; + private String relativePath; + + public AsyncIncrementFileDownloadEvent(String relativePath, String filename) { + super(ASYNC_FILE_SIZE); + this.filename = filename; + this.relativePath = relativePath; + } + + public String getFilename() { + return filename; + } + + public String getRelativePath() { + return relativePath; + } +} diff --git a/src/main/java/org/olat/core/commons/services/vfs/manager/VFSMetadataDAO.java b/src/main/java/org/olat/core/commons/services/vfs/manager/VFSMetadataDAO.java index a5e1e2f3f1b8ff60a3a4e1e184e40e3ffe5bf0a0..50c94678b08a86dae57bce2de7f0577c14c7495c 100644 --- a/src/main/java/org/olat/core/commons/services/vfs/manager/VFSMetadataDAO.java +++ b/src/main/java/org/olat/core/commons/services/vfs/manager/VFSMetadataDAO.java @@ -113,8 +113,7 @@ public class VFSMetadataDAO { .append(" left join fetch author.user as authorUser") .append(" left join fetch metadata.licenseType as licenseType") .append(" where metadata.uuid=:uuid"); - - + List<VFSMetadata> metadata = dbInstance.getCurrentEntityManager() .createQuery(sb.toString(), VFSMetadata.class) .setParameter("uuid", uuid) @@ -276,6 +275,7 @@ public class VFSMetadataDAO { .createQuery(updateQuery) .setParameter("filename", filename) .setParameter("relativePath", relativePath) + .setHint("javax.persistence.query.timeout", 1000) .executeUpdate(); } diff --git a/src/main/java/org/olat/core/commons/services/vfs/manager/VFSRepositoryServiceImpl.java b/src/main/java/org/olat/core/commons/services/vfs/manager/VFSRepositoryServiceImpl.java index fce8cf124373abc68c6a9818fcdeebd30498d24a..0aa5c96e59e59ce9649ed08d473a2561b7912153 100644 --- a/src/main/java/org/olat/core/commons/services/vfs/manager/VFSRepositoryServiceImpl.java +++ b/src/main/java/org/olat/core/commons/services/vfs/manager/VFSRepositoryServiceImpl.java @@ -106,6 +106,7 @@ public class VFSRepositoryServiceImpl implements VFSRepositoryService, GenericEv private static final Logger log = Tracing.createLoggerFor(VFSRepositoryServiceImpl.class); private final OLATResourceable fileSizeSubscription = OresHelper.createOLATResourceableType("UpdateFileSizeAsync"); + private final OLATResourceable incrementFileDownload = OresHelper.createOLATResourceableType("IncrementFileDownloadAsync"); private static final String CANONICAL_ROOT_REL_PATH = "/"; @Autowired @@ -138,13 +139,16 @@ public class VFSRepositoryServiceImpl implements VFSRepositoryService, GenericEv @Override public void afterPropertiesSet() throws Exception { coordinatorManager.getCoordinator().getEventBus().registerFor(this, null, fileSizeSubscription); + coordinatorManager.getCoordinator().getEventBus().registerFor(this, null, incrementFileDownload); } @Override public void event(Event event) { if(event instanceof AsyncFileSizeUpdateEvent) { processFileSizeUpdateEvent((AsyncFileSizeUpdateEvent)event); - } + } else if(event instanceof AsyncIncrementFileDownloadEvent) { + processIncrementFileDownnload((AsyncIncrementFileDownloadEvent)event); + } } private void processFileSizeUpdateEvent(AsyncFileSizeUpdateEvent event) { @@ -160,6 +164,15 @@ public class VFSRepositoryServiceImpl implements VFSRepositoryService, GenericEv } } + private void processIncrementFileDownnload(AsyncIncrementFileDownloadEvent event) { + try { + metadataDao.increaseDownloadCount(event.getRelativePath(), event.getFilename()); + dbInstance.commit(); + } catch (Exception e) { + log.error("Cannot increment file downloads of: " + event.getRelativePath() + " " + event.getFilename(), e); + } + } + @Override public VFSMetadata getMetadataByUUID(String uuid) { if(StringHelper.containsNonWhitespace(uuid)) { @@ -492,7 +505,8 @@ public class VFSRepositoryServiceImpl implements VFSRepositoryService, GenericEv public void increaseDownloadCount(VFSLeaf item) { String relPath = getContainerRelativePath(item); if(StringHelper.containsNonWhitespace(relPath)) { - metadataDao.increaseDownloadCount(relPath, item.getName()); + AsyncIncrementFileDownloadEvent event = new AsyncIncrementFileDownloadEvent(relPath, item.getName()); + coordinatorManager.getCoordinator().getEventBus().fireEventToListenersOf(event, incrementFileDownload); } }