argos/dmp-backend/src/main/java/eu/eudat/managers/DatasetProfileManager.java

56 lines
3.2 KiB
Java

package eu.eudat.managers;
import eu.eudat.builders.model.models.DataTableDataBuilder;
import eu.eudat.dao.entities.DatasetProfileDao;
import eu.eudat.entities.DatasetProfile;
import eu.eudat.entities.xmlmodels.datasetprofiledefinition.Field;
import eu.eudat.models.datasetprofile.DatasetProfileAutocompleteItem;
import eu.eudat.models.datasetprofile.DatasetProfileAutocompleteRequest;
import eu.eudat.models.datasetprofile.DatasetProfileListingModel;
import eu.eudat.models.datasetprofile.DatasetProfileTableRequestItem;
import eu.eudat.models.helpers.common.DataTableData;
import eu.eudat.queryable.QueryableList;
import eu.eudat.services.ApiContext;
import eu.eudat.utilities.builders.XmlBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.xml.xpath.*;
import java.util.List;
public class DatasetProfileManager {
public static List<DatasetProfileAutocompleteItem> getWithCriteria(DatasetProfileDao datasetProfileRepository, DatasetProfileAutocompleteRequest datasetProfileAutocompleteRequest) throws IllegalAccessException, InstantiationException {
QueryableList<DatasetProfile> items = datasetProfileRepository.getWithCriteria(datasetProfileAutocompleteRequest.getCriteria());
List<DatasetProfileAutocompleteItem> datasetProfiles = items.select(item -> new DatasetProfileAutocompleteItem().fromDataModel(item));
return datasetProfiles;
}
public static DataTableData<DatasetProfileListingModel> getPaged(ApiContext apiContext, DatasetProfileTableRequestItem datasetProfileTableRequestItem) throws Exception {
QueryableList<DatasetProfile> items = apiContext.getOperationsContext().getDatabaseRepository().getDatasetProfileDao().getWithCriteria(datasetProfileTableRequestItem.getCriteria());
QueryableList<DatasetProfile> pagedItems = PaginationManager.applyPaging(items, datasetProfileTableRequestItem);
List<DatasetProfileListingModel> datasetProfiles = pagedItems.select(item -> new DatasetProfileListingModel().fromDataModel(item));
return apiContext.getOperationsContext().getBuilderFactory().getBuilder(DataTableDataBuilder.class).data(datasetProfiles).totalCount(items.count()).build();
}
public static List<DatasetProfileListingModel> getAll(DatasetProfileDao datasetProfileRepository) throws IllegalAccessException, InstantiationException {
QueryableList<DatasetProfile> items = datasetProfileRepository.getAll();
List<DatasetProfileListingModel> datasetProfiles = items.select(item -> new DatasetProfileListingModel().fromDataModel(item));
return datasetProfiles;
}
public static eu.eudat.entities.xmlmodels.datasetprofiledefinition.Field queryForField(String xml, String fieldId) throws XPathExpressionException {
eu.eudat.entities.xmlmodels.datasetprofiledefinition.Field field = new Field();
Document document = XmlBuilder.fromXml(xml);
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
XPathExpression expr =
xpath.compile("//field[@id='" + fieldId + "']");
Element name = (Element) expr.evaluate(document, XPathConstants.NODE);
field.fromXml(name);
return field;
}
}