Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to The Apereo Foundation under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
*
* The Apereo Foundation licenses this file to you 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.txt
*
* 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.opencastproject.scheduler.impl.persistence;
import static org.opencastproject.db.Queries.namedQuery;
import org.opencastproject.db.DBSession;
import org.opencastproject.db.DBSessionFactory;
import org.opencastproject.scheduler.impl.SchedulerServiceDatabase;
import org.opencastproject.scheduler.impl.SchedulerServiceDatabaseException;
import org.opencastproject.security.api.SecurityService;
import org.opencastproject.util.NotFoundException;
import com.entwinemedia.fn.data.Opt;
import com.google.gson.Gson;
import org.apache.commons.lang3.tuple.Pair;
import org.joda.time.DateTime;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.TypedQuery;
/**
* Implements {@link SchedulerServiceDatabase}.
*/
@Component(
immediate = true,
service = SchedulerServiceDatabase.class
)
public class SchedulerServiceDatabaseImpl implements SchedulerServiceDatabase {
/** JPA persistence unit name */
public static final String PERSISTENCE_UNIT = "org.opencastproject.scheduler.impl.persistence";
/** Logging utilities */
private static final Logger logger = LoggerFactory.getLogger(SchedulerServiceDatabaseImpl.class);
/** Factory used to create {@link EntityManager}s for transactions */
private EntityManagerFactory emf;
private DBSessionFactory dbSessionFactory;
private DBSession db;
/** The security service */
private SecurityService securityService;
private static final Gson gson = new Gson();
/** OSGi DI */
@Reference(target = "(osgi.unit.name=org.opencastproject.scheduler.impl.persistence)")
public void setEntityManagerFactory(EntityManagerFactory emf) {
this.emf = emf;
}
/** OSGi DI */
@Reference
public void setSecurityService(SecurityService securityService) {
this.securityService = securityService;
}
@Reference
public void setDBSessionFactory(DBSessionFactory dbSessionFactory) {
this.dbSessionFactory = dbSessionFactory;
}
/**
* Creates {@link EntityManagerFactory} using persistence provider and properties passed via OSGi.
*
* @param cc
*/
@Activate
public void activate(ComponentContext cc) {
logger.info("Activating persistence manager for scheduler");
db = dbSessionFactory.createSession(emf);
}
/*
* We need to synchronize this method because JPA doesn't support thread-safe atomic upserts.
*/
@Override
public synchronized void touchLastEntry(String agentId) throws SchedulerServiceDatabaseException {
try {
db.execTx(em -> {
LastModifiedDto entity = em.find(LastModifiedDto.class, agentId);
if (entity == null) {
entity = new LastModifiedDto();
entity.setCaptureAgentId(agentId);
entity.setLastModifiedDate(new Date());
em.persist(entity);
} else {
entity.setLastModifiedDate(new Date());
em.merge(entity);
}
});
} catch (Exception e) {
throw new SchedulerServiceDatabaseException(e);
}
}
@Override
public Date getLastModified(String agentId) throws NotFoundException, SchedulerServiceDatabaseException {
try {
return db.exec(namedQuery.findByIdOpt(LastModifiedDto.class, agentId))
.map(LastModifiedDto::getLastModifiedDate)
.orElseThrow(() -> new NotFoundException("Agent with ID " + agentId + " does not exist"));
} catch (NotFoundException e) {
throw e;
} catch (Exception e) {
throw new SchedulerServiceDatabaseException(e);
}
}
@Override
public Map getLastModifiedDates() throws SchedulerServiceDatabaseException {
try {
return db.exec(namedQuery.findAll("LastModified.findAll", LastModifiedDto.class)).stream()
.collect(Collectors.toMap(
LastModifiedDto::getCaptureAgentId,
LastModifiedDto::getLastModifiedDate
));
} catch (Exception e) {
throw new SchedulerServiceDatabaseException(e);
}
}
@Override
public void storeEvent(String mediapackageId, String organizationId, Opt captureAgentId, Opt start,
Opt end, Opt source, Opt recordingState, Opt recordingLastHeard,
Opt presenters, Opt lastModifiedDate, Opt checksum, Opt