Java tutorial
/** * Product : Hiperium Project * Architect: Andres Solorzano. * Created : 08-05-2009 - 23:30:00 * * The contents of this file are copyrighted by Andres Solorzano * and it is protected by the license: "GPL V3." You can find a copy of this * license at: http://www.hiperium.com/about/licence.html * * Copyright 2014 Andres Solorzano. All rights reserved. * */ package com.hiperium.bo.control.impl; import java.util.List; import java.util.Locale; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.ejb.EJB; import javax.ejb.Remote; import javax.ejb.Stateless; import javax.inject.Inject; import javax.interceptor.AroundInvoke; import javax.interceptor.ExcludeDefaultInterceptors; import javax.interceptor.InvocationContext; import javax.jms.JMSException; import javax.naming.NamingException; import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; import org.apache.commons.lang.StringUtils; import com.hiperium.bo.audit.UserDeviceAuditBO; import com.hiperium.bo.control.DeviceBO; import com.hiperium.bo.control.DeviceSocketBO; import com.hiperium.bo.generic.GenericBusinessObject; import com.hiperium.bo.manager.exception.ExceptionManager; import com.hiperium.bo.manager.security.SessionManager; import com.hiperium.bo.utils.EnumCloudDestination; import com.hiperium.bo.utils.Resources; import com.hiperium.commons.EnumHiperiumTier; import com.hiperium.commons.EnumI18N; import com.hiperium.commons.HiperiumTier; import com.hiperium.commons.dto.DeviceDTO; import com.hiperium.commons.dto.SelectionDTO; import com.hiperium.commons.exception.EnumInformationException; import com.hiperium.commons.exception.InformationException; import com.hiperium.commons.jms.CloudMessageSender; import com.hiperium.commons.log.HiperiumLogger; import com.hiperium.model.control.Device; /** * This service is the implementation of the interface DeviceLocal and manages * all actions needed for device management. * * @author Andres Solorzano * */ @Stateless @Remote(DeviceBO.class) public class DeviceBOImpl extends GenericBusinessObject implements DeviceBO { /** The property log. */ @Inject @HiperiumTier(EnumHiperiumTier.BUSINESS) private HiperiumLogger log; /** The property sessionManager. */ @Inject private SessionManager sessionManager; /** The property deviceSocketBO. */ @EJB private DeviceSocketBO deviceSocketBO; /** The property userDeviceAuditBO. */ @EJB private UserDeviceAuditBO userDeviceAuditBO; /** The property exceptionManager. */ @EJB private ExceptionManager exceptionManager; /** The property sender. */ private CloudMessageSender sender; /** * Component initialization. */ @PostConstruct public void init() { try { this.sender = new CloudMessageSender().setProperties(Resources.PROPERTIES) .setDestination(EnumCloudDestination.DEVICE_CLOUD_TOPIC.getValue()).build(); } catch (NamingException | JMSException e) { this.log.error(e.getMessage(), e); } } /** * {@inheritDoc} */ @Override public void update(@NotNull SelectionDTO register, @NotNull String sessionId) throws InformationException { Device device = super.getDaoFactory().getDeviceDAO().findById(register.getId(), false, true); device.setName(register.getName()); super.getDaoFactory().getDeviceDAO().update(device); } /** * {@inheritDoc} */ @Override public SelectionDTO findSelectionById(@NotNull @Min(value = 1L) Long deviceId, @NotNull String sessionId) throws InformationException { return super.getDaoFactory().getDeviceDAO().findSelectionById(deviceId); } /** * {@inheritDoc} */ @Override public List<DeviceDTO> findByZoneId(@NotNull @Min(value = 1L) Long zoneId, @NotNull String sessionId) throws InformationException { return super.getDaoFactory().getDeviceDAO().findByZoneId(zoneId); } /** * {@inheritDoc} */ @Override public List<Device> findByProfileId(@NotNull @Min(value = 1L) Long profileId, @NotNull String sessionId) throws InformationException { return super.getDaoFactory().getDeviceDAO().findByProfileId(profileId); } /** * * @param context * @return * @throws Exception */ @AroundInvoke private Object validateMethod(InvocationContext context) throws Exception, InformationException { this.log.debug("validateMethod() - BEGIN: " + context.getMethod().getName()); String methodName = context.getMethod().getName(); Object result = null; // INTERCEPTS ONLY DEVICE OPERATION METHODS if ("userOperation".equals(methodName) || "homeOperation".equals(methodName)) { Object[] params = context.getParameters(); String sessionId = (String) params[1]; if (StringUtils.isBlank(sessionId) || !this.sessionManager.isUserLoggedIn(sessionId)) { throw InformationException.generate(EnumI18N.COMMON, EnumInformationException.ACCESS_NOT_ALLOWED, Locale.getDefault()); } // PROCEED WITH METHOD CALL try { DeviceDTO deviceDTO = (DeviceDTO) params[0]; super.getDaoFactory().getDeviceDAO().updateDeviceState(deviceDTO); result = context.proceed(); this.userDeviceAuditBO.create(deviceDTO, sessionId); } catch (Exception e) { InformationException infoException = null; if (e.getCause() instanceof InformationException) { infoException = (InformationException) e; throw infoException; } infoException = this.exceptionManager.createMessageException(e, this.sessionManager.findUserLocale(sessionId)); throw infoException; } } else { result = context.proceed(); } this.log.debug("validateMethod() - END: " + context.getMethod().getName()); return result; } /** * {@inheritDoc} */ @Override @ExcludeDefaultInterceptors // ADDS METHOD INTERCEPTOR public void userOperation(@NotNull DeviceDTO deviceDTO, @NotNull String sessionId) { this.log.debug("userOperation - BEGIN"); this.sender.setDeviceDTO(deviceDTO).setHomeId(this.sessionManager.findUserHomeId(sessionId)).sendMessage(); this.log.debug("userOperation - END"); } /** * {@inheritDoc} */ @Override @ExcludeDefaultInterceptors // ADDS METHOD INTERCEPTOR public void homeOperation(@NotNull DeviceDTO deviceDTO, @NotNull String sessionId) { this.log.debug("homeOperation - BEGIN"); this.deviceSocketBO.sendSocketMessage(deviceDTO.getHomeId(), deviceDTO); this.log.debug("homeOperation - END"); } /** * Destroys the component. */ @PreDestroy public void destroy() { if (this.sender != null) { this.sender.destroy(); } } }