blob: 7fe011a10fbf265b0908d9937901098fb0d2f5cf [file] [log] [blame]
/********************************************************************************
* Copyright (c) 2015-2018 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.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.inject.Inject;
import org.eclipse.mdm.api.base.adapter.Attribute;
import org.eclipse.mdm.api.base.adapter.EntityType;
import org.eclipse.mdm.api.base.model.Channel;
import org.eclipse.mdm.api.base.model.Environment;
import org.eclipse.mdm.api.base.query.DataAccessException;
import org.eclipse.mdm.api.dflt.ApplicationContext;
import org.eclipse.mdm.api.dflt.EntityManager;
import org.eclipse.mdm.businessobjects.control.I18NActivity;
import org.eclipse.mdm.businessobjects.control.MDMEntityAccessException;
import org.eclipse.mdm.businessobjects.control.NavigationActivity;
import org.eclipse.mdm.businessobjects.control.SearchActivity;
import org.eclipse.mdm.businessobjects.utils.ServiceUtils;
import org.eclipse.mdm.connector.boundary.ConnectorService;
/**
* ChannelService Bean implementation with available {@link Channel} operations
*
* @author Sebastian Dirsch, Gigatronik Ingolstadt GmbH
*
*/
@Stateless
public class ChannelService {
@Inject
private ConnectorService connectorService;
@EJB
private I18NActivity i18nActivity;
@EJB
private NavigationActivity navigationActivity;
@EJB
private SearchActivity searchActivity;
/**
* returns the matching {@link Channel}s using the given filter or all
* {@link Channel}s if no filter is available
*
* @param sourceName name of the source (MDM {@link Environment} name)
* @param filter filter string to filter the Channel result
* @return the found {@link Channel}s
*/
public List<Channel> getChannels(String sourceName, String filter) {
try {
ApplicationContext context = this.connectorService.getContextByName(sourceName);
EntityManager em = context.getEntityManager()
.orElseThrow(() -> new MDMEntityAccessException("Entity manager not present!"));
if (filter == null || filter.trim().length() <= 0) {
return em.loadAll(Channel.class);
}
if (ServiceUtils.isParentFilter(context, filter, Channel.PARENT_TYPE_CHANNELGROUP)) {
String id = ServiceUtils.extactIdFromParentFilter(context, filter, Channel.PARENT_TYPE_CHANNELGROUP);
return this.navigationActivity.getChannels(sourceName, id);
}
return this.searchActivity.search(context, Channel.class, filter);
} catch (DataAccessException e) {
throw new MDMEntityAccessException(e.getMessage(), e);
}
}
/**
* returns a {@link Channel} identified by the given id.
*
* @param sourceName name of the source (MDM {@link Environment} name)
* @param channelId id of the {@link Channel}
* @return the matching {@link Channel}
*/
public Channel getChannel(String sourceName, String channelId) {
try {
EntityManager em = this.connectorService.getContextByName(sourceName).getEntityManager()
.orElseThrow(() -> new MDMEntityAccessException("Entity manager not present!"));
return em.load(Channel.class, channelId);
} catch (DataAccessException e) {
throw new MDMEntityAccessException(e.getMessage(), e);
}
}
/**
* returns localized {@link Channel} attributes
*
* @param sourceName name of the source (MDM {@link Environment} name)
* @return the localized {@link Channel} attributes
*/
public Map<Attribute, String> localizeAttributes(String sourceName) {
return this.i18nActivity.localizeAttributes(sourceName, Channel.class);
}
/**
* returns the localized {@link Channel} type name
*
* @param sourceName name of the source (MDM {@link Environment} name)
* @return the localized {@link Channel} type name
*/
public Map<EntityType, String> localizeType(String sourceName) {
return this.i18nActivity.localizeType(sourceName, Channel.class);
}
}