blob: 13f000a85f9391b6348e1ebf4194dfa0e60e4398 [file] [log] [blame]
/********************************************************************************
* Copyright (c) 2015-2020 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.api.atfxadapter;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.asam.ods.AoException;
import org.asam.ods.AoSession;
import org.asam.ods.ErrorCode;
import org.asam.ods.SeverityFlag;
import org.eclipse.mdm.api.atfxadapter.filetransfer.ATFXFileService;
import org.eclipse.mdm.api.base.adapter.ModelManager;
import org.eclipse.mdm.api.base.file.FileService;
import org.eclipse.mdm.api.base.query.DataAccessException;
import org.eclipse.mdm.api.base.query.QueryService;
import org.eclipse.mdm.api.dflt.ApplicationContext;
import org.eclipse.mdm.api.dflt.EntityManager;
import org.eclipse.mdm.api.dflt.model.EntityFactory;
import org.eclipse.mdm.api.dflt.model.ExtSystemAttribute;
import org.eclipse.mdm.api.odsadapter.query.ODSEntityFactory;
import org.eclipse.mdm.api.odsadapter.query.ODSModelManager;
import org.eclipse.mdm.api.odsadapter.query.ODSQueryService;
import org.omg.CORBA.ORB;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* ATFXContext encapsulates a session to the ASAM ODS CORBA API of openATFX
*
*/
public class ATFXContext implements ApplicationContext {
private static final Logger LOGGER = LoggerFactory.getLogger(ATFXContext.class);
private Map<String, String> parameters;
private ODSModelManager modelManager;
private ATFXEntityManager entityManager;
private File atfxFile;
private static final String INCLUDE_CATALOG = "includeCatalog";
/**
* Creates a new ATFX application context.
*
* @param orb the CORBA ORB used to connect to the ODS API
* @param aoSession
* @param parameters
* @param atfxFile
* @param extSystemAttrList
* @throws AoException
*/
public ATFXContext(ORB orb, AoSession aoSession, Map<String, String> parameters,
List<ExtSystemAttribute> extSystemAttrList, File atfxFile) throws AoException {
this.parameters = parameters;
boolean includeCatalog = Boolean.valueOf(parameters.getOrDefault(INCLUDE_CATALOG, "false"));
this.modelManager = new ODSModelManager(orb, aoSession, new ATFXEntityConfigRepositoryLoader(includeCatalog));
this.entityManager = new ATFXEntityManager(this, extSystemAttrList);
this.atfxFile = atfxFile;
LOGGER.debug("ATFXContext initialized.");
}
/**
* {@inheritDoc}
*/
@Override
public Optional<EntityManager> getEntityManager() {
return Optional.of(entityManager);
}
/**
* {@inheritDoc}
*/
@Override
public Optional<ModelManager> getModelManager() {
return Optional.of(modelManager);
}
@Override
public Optional<EntityFactory> getEntityFactory() {
try {
return Optional.of(new ODSEntityFactory(modelManager, entityManager.loadLoggedOnUser()));
} catch (DataAccessException e) {
throw new IllegalStateException("Unable to load instance of the logged in user.");
}
}
/**
* {@inheritDoc}
*/
@Override
public Optional<QueryService> getQueryService() {
return Optional.of(new ODSQueryService(modelManager));
}
/**
* {@inheritDoc}
*/
@Override
public Map<String, String> getParameters() {
return parameters;
}
/**
* @returns the string "atfx"
*/
@Override
public String getAdapterType() {
return "atfx";
}
/**
* {@inheritDoc}
*/
@Override
public void close() {
try {
modelManager.close();
} catch (AoException e) {
LOGGER.warn("Unable to close sesssion due to: " + e.reason, e);
}
}
/**
* Returns the {@link ATFXModelManager} used for this context.
*
* @return the {@link ATFXModelManager}
*/
public ODSModelManager getODSModelManager() {
return modelManager;
}
/**
* Returns the {@link AoSession} used for this context.
*
* @return {@link AoSession} used for this context.
*/
public AoSession getAoSession() {
return modelManager.getAoSession();
}
/**
* Returns the ORB used for this context
*
* @return ORB used for this context
*/
public ORB getORB() {
return modelManager.getORB();
}
/**
* Returns a new {@link ATFXContext} with a new ODS co-session.
*
* @return The created {@code ODSContext} is returned.
* @throws AoException Thrown on errors.
*/
public ATFXContext newContext() throws AoException {
throw new AoException(ErrorCode.AO_NOT_IMPLEMENTED, SeverityFlag.ERROR, 0,
"Method 'createCoSession' not implemented in openATFX");
}
/*
* (non-Javadoc)
*
* @see org.eclipse.mdm.api.base.BaseApplicationContext#getFileService()
*/
@Override
public Optional<FileService> getFileService() {
return Optional.of(new ATFXFileService(this.atfxFile.getParentFile()));
}
}