Skip to content

Commit

Permalink
SAK-44379 Move file conversion service to session factory (sakaiproje…
Browse files Browse the repository at this point in the history
…ct#8637)

* SAK-44379 Move file conversion service to session factory

https://jira.sakaiproject.org/browse/SAK-44379

* remove session.close()

Co-authored-by: Earle Nietzel <[email protected]>
  • Loading branch information
adrianfish and ern authored Dec 16, 2020
1 parent 6e16ac4 commit 092ebe5
Show file tree
Hide file tree
Showing 16 changed files with 374 additions and 187 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
**********************************************************************************/

package org.sakaiproject.content.hbm;
package org.sakaiproject.content.api.persistence;

import java.time.Instant;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.sakaiproject.content.api.persistence;

import java.util.List;

import org.sakaiproject.springframework.data.SpringCrudRepository;

public interface FileConversionServiceRepository extends SpringCrudRepository<FileConversionQueueItem, Long> {

List<FileConversionQueueItem> findByStatus(FileConversionQueueItem.Status status);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.sakaiproject.hibernate;
package org.sakaiproject.springframework.data;

import java.io.Serializable;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* Copyright (c) 2003-2017 The Apereo Foundation
*
* Licensed under the Educational Community License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://opensource.org/licenses/ecl2
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.sakaiproject.springframework.data;

import java.io.Serializable;

/**
* This is modeled after Spring's CrudRepository. The idea here is using this api will
* make for an easier migration to spring-data in the future.
*/
public interface SpringCrudRepository<T, ID extends Serializable> extends Repository<T, ID> {

/**
* Returns the number of entities available.
*
* @return the number of entities
*/
long count();

/**
* Deletes a given entity.
*
* @param entity
* @throws IllegalArgumentException in case the given entity is {@literal null}.
*/
void delete(T entity);

/**
* Deletes all entities managed by the repository.
*/
void deleteAll();

/**
* Deletes the given entities.
*/
void deleteAll(Iterable<? extends T> entities);

/**
* Deletes the entity with the given id.
*
* @param id must not be {@literal null}.
* @throws IllegalArgumentException in case the given {@code id} is {@literal null}
*/
void deleteById(ID id);

/**
* Returns whether an entity with the given id exists.
*
* @param id must not be {@literal null}.
* @return true if an entity with the given id exists, {@literal false} otherwise
* @throws IllegalArgumentException if {@code id} is {@literal null}
*/
boolean existsById(ID id);

/**
* Returns all instances of the type.
*
* @return all entities
*/
Iterable<T> findAll();

/**
* Returns all instances of the type T with the given IDs.
*
* @param ids
* @return
*/
Iterable<T> findAllById(Iterable<ID> ids);

/**
* Retrieves an entity by its id.
*
* @param id must not be {@literal null}.
* @return the entity with the given id or {@literal null} if none found
* @throws IllegalArgumentException if {@code id} is {@literal null}
*/
T findById(ID id);

/**
* Saves a given entity. Use the returned instance for further operations as the save operation might have changed the
* entity instance completely.
*
* @param entity
* @return the saved entity
*/
<S extends T> S save(S entity);

/**
* Saves all given entities.
*
* @param entities
* @return the saved entities
* @throws IllegalArgumentException in case the given entity is {@literal null}.
*/
<S extends T> Iterable<S> saveAll(Iterable<S> entities);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,30 @@
import org.sakaiproject.content.api.ContentHostingService;
import org.sakaiproject.content.api.ContentResource;
import org.sakaiproject.content.api.FileConversionService;
import org.sakaiproject.content.api.repository.FileConversionQueueItemRepository;
import org.sakaiproject.content.hbm.FileConversionQueueItem;
import org.sakaiproject.content.api.persistence.FileConversionQueueItem;
import org.sakaiproject.content.api.persistence.FileConversionServiceRepository;
import org.sakaiproject.content.impl.converters.LoolFileConverter;
import org.sakaiproject.entity.api.ResourceProperties;
import org.sakaiproject.entity.api.ResourcePropertiesEdit;
import org.sakaiproject.exception.IdUnusedException;
import org.sakaiproject.exception.PermissionException;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;

@Slf4j
@Setter
public class FileConversionServiceImpl implements FileConversionService {

@Autowired private ContentHostingService contentHostingService;
@Autowired private FileConversionQueueItemRepository repository;
@Autowired private FileConversionServiceRepository repository;
@Autowired private SecurityService securityService;
@Autowired private ServerConfigurationService serverConfigurationService;
private TransactionTemplate transactionTemplate;

private String converterBaseUrl;
private boolean enabled;
Expand Down Expand Up @@ -106,11 +104,8 @@ public void startIfEnabled() {

master.scheduleWithFixedDelay(() -> {

log.debug("scheduling ...");

List<FileConversionQueueItem> items = repository.findByStatus(FileConversionQueueItem.Status.NOT_STARTED);

items.forEach(i -> log.info(i.getReference()));
List<FileConversionQueueItem> items
= repository.findByStatus(FileConversionQueueItem.Status.NOT_STARTED);

log.debug("Number of unstarted conversion items: {}", items.size());

Expand All @@ -133,7 +128,7 @@ public void startIfEnabled() {
item.setAttempts(item.getAttempts() + 1);
item.setLastAttemptStarted(Instant.now());

FileConversionQueueItem inProgressItem = transactionTemplate.execute(status -> repository.save(item));
FileConversionQueueItem inProgressItem = repository.save(item);

try {
String sourceFileName = ref.substring(ref.lastIndexOf("/") + 1);
Expand All @@ -160,19 +155,12 @@ public void startIfEnabled() {
contentHostingService.addProperty(ref, ContentHostingService.PREVIEW, previewResource.getId());

log.debug("Deleting item with ref {}. It's been successfully converted.", ref);
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
public void doInTransactionWithoutResult(TransactionStatus status) {
repository.delete(inProgressItem);
}
});

repository.deleteById(inProgressItem.getId());
} catch (Exception e) {
item.setStatus(FileConversionQueueItem.Status.NOT_STARTED);
transactionTemplate.execute(new TransactionCallbackWithoutResult() {

public void doInTransactionWithoutResult(TransactionStatus status) {
repository.save(item);
}
});
inProgressItem.setStatus(FileConversionQueueItem.Status.NOT_STARTED);
repository.save(inProgressItem);
log.error("Call to conversion service failed", e);
} finally {
securityService.popAdvisor(securityAdvisor);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.sakaiproject.content.impl.persistence;

import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import org.sakaiproject.content.api.persistence.FileConversionQueueItem;
import org.sakaiproject.content.api.persistence.FileConversionServiceRepository;
import org.sakaiproject.springframework.data.SpringCrudRepositoryImpl;

public class FileConversionServiceRepositoryImpl extends SpringCrudRepositoryImpl<FileConversionQueueItem, Long> implements FileConversionServiceRepository {

@Transactional
public List<FileConversionQueueItem> findByStatus(FileConversionQueueItem.Status status) {

Session session = sessionFactory.getCurrentSession();
List<FileConversionQueueItem> items
= session.createCriteria(FileConversionQueueItem.class)
.add(Restrictions.eq("status", status)).list();
return items;
}
}

This file was deleted.

21 changes: 12 additions & 9 deletions kernel/kernel-impl/src/main/webapp/WEB-INF/content-components.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,27 @@
class="org.sakaiproject.content.impl.BasicContentPrintService">
</bean>

<bean id="org.sakaiproject.content.api.repository.FileConversionQueueItemRepository"
class="org.sakaiproject.content.impl.repository.FileConversionQueueItemRepositoryImpl">
<constructor-arg ref="entityManagerFactory"/>
<bean id="org.sakaiproject.content.api.persistence.FileConversionServiceRepository"
class="org.sakaiproject.content.impl.persistence.FileConversionServiceRepositoryImpl">
<property name="sessionFactory" ref="org.sakaiproject.springframework.orm.hibernate.GlobalSessionFactory"/>
</bean>

<bean id="org.sakaiproject.content.api.FileConversionService"
class="org.sakaiproject.content.impl.FileConversionServiceImpl"
destroy-method="destroy"
init-method="init"
lazy-init="true">
<property name="transactionTemplate">
<bean class="org.springframework.transaction.support.TransactionTemplate">
<constructor-arg ref="jpaTransactionManager"/>
</bean>
</property>
</bean>

<bean id="fileConversionServiceHibernateMappings"
class="org.sakaiproject.springframework.orm.hibernate.impl.AdditionalHibernateMappingsImpl">
<property name="annotatedClasses">
<list>
<value>org.sakaiproject.content.api.persistence.FileConversionQueueItem</value>
</list>
</property>
</bean>

<!-- database beans -->
<bean id="org.sakaiproject.content.impl.ContentServiceSqlDefault" class="org.sakaiproject.content.impl.ContentServiceSqlDefault"/>
<bean id="org.sakaiproject.content.impl.ContentServiceSqlHSql" class="org.sakaiproject.content.impl.ContentServiceSqlHSql"/>
Expand Down Expand Up @@ -160,7 +164,6 @@
<property name="annotatedClasses">
<list>
<value>org.sakaiproject.content.hbm.Lock</value>
<value>org.sakaiproject.content.hbm.FileConversionQueueItem</value>
</list>
</property>
</bean>
Expand Down
Loading

0 comments on commit 092ebe5

Please sign in to comment.