/******************************************************************************* | |
* Copyright (c) 2019 Contributors to the Eclipse Foundation | |
* | |
* See the NOTICE file(s) distributed with this work for additional | |
* information regarding copyright ownership. | |
* | |
* This program and the accompanying materials are made available under the | |
* terms of the Eclipse Public License v. 2.0 which is available at | |
* http://www.eclipse.org/legal/epl-2.0. | |
* | |
* SPDX-License-Identifier: EPL-2.0 | |
*******************************************************************************/ | |
package org.eclipse.mdm.businessobjects.boundary; | |
import java.util.Optional; | |
import java.util.stream.Collectors; | |
import javax.ejb.Stateless; | |
import javax.inject.Inject; | |
import org.eclipse.mdm.api.base.adapter.EntityType; | |
import org.eclipse.mdm.api.base.adapter.ModelManager; | |
import org.eclipse.mdm.api.base.query.ComparisonOperator; | |
import org.eclipse.mdm.api.base.query.Condition; | |
import org.eclipse.mdm.api.base.query.Filter; | |
import org.eclipse.mdm.api.base.query.QueryService; | |
import org.eclipse.mdm.api.base.query.Record; | |
import org.eclipse.mdm.api.base.query.Result; | |
import org.eclipse.mdm.api.dflt.EntityManager; | |
import org.eclipse.mdm.api.dflt.model.Classification; | |
import org.eclipse.mdm.api.dflt.model.Domain; | |
import org.eclipse.mdm.api.dflt.model.ProjectDomain; | |
import org.eclipse.mdm.api.dflt.model.Status; | |
import org.eclipse.mdm.businessobjects.control.MDMEntityAccessException; | |
import org.eclipse.mdm.connector.boundary.ConnectorService; | |
/** | |
* @author akn | |
* | |
*/ | |
@Stateless | |
public class ClassificationService { | |
@Inject | |
private ConnectorService connectorService; | |
public Classification getClassification(String sourceName, Status status, ProjectDomain projectDomain, | |
Domain domain) { | |
Optional<QueryService> queryService = connectorService.getContextByName(sourceName).getQueryService(); | |
Optional<ModelManager> modelManager = connectorService.getContextByName(sourceName).getModelManager(); | |
EntityType entityType = modelManager.get().getEntityType(Classification.class); | |
Condition c1 = ComparisonOperator.CASE_INSENSITIVE_EQUAL.create(entityType.getAttribute("ProjectDomain"), | |
projectDomain.getID()); | |
Condition c2 = ComparisonOperator.CASE_INSENSITIVE_EQUAL.create(entityType.getAttribute("Domain"), | |
domain.getID()); | |
Condition c3 = ComparisonOperator.CASE_INSENSITIVE_EQUAL.create(entityType.getAttribute("Status"), | |
status.getID()); | |
java.util.List<Result> fetch = queryService.get().createQuery().selectID(entityType) | |
.fetch(Filter.and().addAll(c1, c2, c3)); | |
java.util.List<String> collect = fetch.stream().map(r -> r.getRecord(entityType)).map(Record::getID) | |
.collect(Collectors.toList()); | |
EntityManager em = this.connectorService.getContextByName(sourceName).getEntityManager() | |
.orElseThrow(() -> new MDMEntityAccessException("Entity manager not present!")); | |
Classification classification = null; | |
if (!collect.isEmpty()) { | |
classification = em.load(Classification.class, collect.get(0)); | |
} | |
return classification; | |
} | |
} |