
org.finra.herd.dao.impl.StorageFileDaoImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of herd-dao Show documentation
Show all versions of herd-dao Show documentation
This project contains the database application layer (DAL) code. This contains code for accessing various external data sources such as
databases, AWS services, etc.
/*
* Copyright 2015 herd contributors
*
* Licensed under the Apache 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://www.apache.org/licenses/LICENSE-2.0
*
* 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.finra.herd.dao.impl;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Tuple;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.apache.commons.collections4.MultiValuedMap;
import org.apache.commons.collections4.multimap.ArrayListValuedHashMap;
import org.springframework.stereotype.Repository;
import org.finra.herd.dao.StorageFileDao;
import org.finra.herd.model.dto.ConfigurationValue;
import org.finra.herd.model.jpa.StorageEntity;
import org.finra.herd.model.jpa.StorageEntity_;
import org.finra.herd.model.jpa.StorageFileEntity;
import org.finra.herd.model.jpa.StorageFileEntity_;
import org.finra.herd.model.jpa.StorageUnitEntity;
import org.finra.herd.model.jpa.StorageUnitEntity_;
@Repository
public class StorageFileDaoImpl extends AbstractHerdDao implements StorageFileDao
{
@Override
public StorageFileEntity getStorageFileByStorageNameAndFilePath(String storageName, String filePath)
{
// Create the criteria builder and the criteria.
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery criteria = builder.createQuery(StorageFileEntity.class);
// The criteria root is the storage files.
Root storageFileEntity = criteria.from(StorageFileEntity.class);
// Join to the other tables we can filter on.
Join storageUnitEntity = storageFileEntity.join(StorageFileEntity_.storageUnit);
Join storageEntity = storageUnitEntity.join(StorageUnitEntity_.storage);
// Create the standard restrictions (i.e. the standard where clauses).
Predicate filePathRestriction = builder.equal(storageFileEntity.get(StorageFileEntity_.path), filePath);
Predicate storageNameRestriction = builder.equal(builder.upper(storageEntity.get(StorageEntity_.name)), storageName.toUpperCase());
criteria.select(storageFileEntity).where(builder.and(filePathRestriction, storageNameRestriction));
return executeSingleResultQuery(criteria,
String.format("Found more than one storage file with parameters {storageName=\"%s\"," + " filePath=\"%s\"}.", storageName, filePath));
}
@Override
public Long getStorageFileCount(String storageName, String filePathPrefix)
{
// Create the criteria builder and the criteria.
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery criteria = builder.createQuery(Long.class);
// The criteria root is the storage files.
Root storageFileEntity = criteria.from(StorageFileEntity.class);
// Join to the other tables we can filter on.
Join storageUnitEntity = storageFileEntity.join(StorageFileEntity_.storageUnit);
Join storageEntity = storageUnitEntity.join(StorageUnitEntity_.storage);
// Create path.
Expression storageFileCount = builder.count(storageFileEntity.get(StorageFileEntity_.id));
// Create the standard restrictions (i.e. the standard where clauses).
Predicate storageNameRestriction = builder.equal(builder.upper(storageEntity.get(StorageEntity_.name)), storageName.toUpperCase());
Predicate filePathRestriction = builder.like(storageFileEntity.get(StorageFileEntity_.path), String.format("%s%%", filePathPrefix));
// Add the clauses for the query.
criteria.select(storageFileCount).where(builder.and(storageNameRestriction, filePathRestriction));
return entityManager.createQuery(criteria).getSingleResult();
}
@Override
public List getStorageFilesByStorageAndFilePathPrefix(String storageName, String filePathPrefix)
{
// Create the criteria builder and the criteria.
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery criteria = builder.createQuery(StorageFileEntity.class);
// The criteria root is the storage files.
Root storageFileEntity = criteria.from(StorageFileEntity.class);
// Join to the other tables we can filter on.
Join storageUnitEntity = storageFileEntity.join(StorageFileEntity_.storageUnit);
Join storageEntity = storageUnitEntity.join(StorageUnitEntity_.storage);
// Create the standard restrictions (i.e. the standard where clauses).
Predicate filePathRestriction = builder.like(storageFileEntity.get(StorageFileEntity_.path), String.format("%s%%", filePathPrefix));
Predicate storageNameRestriction = builder.equal(builder.upper(storageEntity.get(StorageEntity_.name)), storageName.toUpperCase());
// Order the results by file path.
Order orderByFilePath = builder.asc(storageFileEntity.get(StorageFileEntity_.path));
criteria.select(storageFileEntity).where(builder.and(filePathRestriction, storageNameRestriction)).orderBy(orderByFilePath);
// Retrieve the storage files.
List storageFileEntities = entityManager.createQuery(criteria).getResultList();
// Build the result list.
List storageFilePaths = new ArrayList<>();
for (StorageFileEntity storageFile : storageFileEntities)
{
storageFilePaths.add(storageFile.getPath());
}
return storageFilePaths;
}
@Override
public MultiValuedMap getStorageFilePathsByStorageUnits(List storageUnitEntities)
{
// Create a map that can hold a collection of values against each key.
MultiValuedMap result = new ArrayListValuedHashMap<>();
// Retrieve the pagination size for the storage file paths query configured in the system.
Integer paginationSize = configurationHelper.getProperty(ConfigurationValue.STORAGE_FILE_PATHS_QUERY_PAGINATION_SIZE, Integer.class);
// Create the criteria builder and the criteria.
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery criteria = builder.createTupleQuery();
// The criteria root is the storage file.
Root storageFileEntity = criteria.from(StorageFileEntity.class);
// Join to the other tables we can filter on.
Join storageUnitEntity = storageFileEntity.join(StorageFileEntity_.storageUnit);
// Get the columns.
Path storageUnitIdColumn = storageUnitEntity.get(StorageUnitEntity_.id);
Path storageFilePathColumn = storageFileEntity.get(StorageFileEntity_.path);
// Create the standard restrictions (i.e. the standard where clauses).
Predicate queryRestriction = getPredicateForInClause(builder, storageUnitEntity, storageUnitEntities);
// Add the select clause.
criteria.multiselect(storageUnitIdColumn, storageFilePathColumn);
// Add the where clause.
criteria.where(queryRestriction);
// Execute the query using pagination and populate the result map.
int startPosition = 0;
while (true)
{
// Run the query to get a list of tuples back.
List tuples = entityManager.createQuery(criteria).setFirstResult(startPosition).setMaxResults(paginationSize).getResultList();
// Populate the result map from the returned tuples (i.e. 1 tuple for each row).
for (Tuple tuple : tuples)
{
// Extract the tuple values.
Integer storageUnitId = tuple.get(storageUnitIdColumn);
String storageFilePath = tuple.get(storageFilePathColumn);
// Update the result map.
result.put(storageUnitId, storageFilePath);
}
// Break out of the while loop if we got less results than the pagination size.
if (tuples.size() < paginationSize)
{
break;
}
// Increment the start position.
startPosition += paginationSize;
}
return result;
}
}