Java tutorial
/******************************************************************************* * Copyright 2014 Juan Diego Navarre Gonzalez * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. ******************************************************************************/ package net.navasoft.madcoin.backend.services.rest.impl; import java.lang.reflect.Array; import java.util.Collection; import java.util.List; import javax.persistence.NoResultException; import net.navasoft.madcoin.backend.model.entities.impl.BusinessLines; import net.navasoft.madcoin.backend.model.entities.impl.ServiceCategories; import net.navasoft.madcoin.backend.services.controller.exception.ControllerExceptionArgument; import net.navasoft.madcoin.backend.services.controller.exception.impl.ControllerExceptionFactory; import net.navasoft.madcoin.backend.services.controller.exception.impl.InexistentLOBException; import net.navasoft.madcoin.backend.services.controller.exception.impl.InsufficientCategoryException; import net.navasoft.madcoin.backend.services.controller.exception.impl.InsufficientLOBException; import net.navasoft.madcoin.backend.services.rest.IBusinessService; import net.navasoft.madcoin.backend.services.vo.response.BusinessSuccessResponseVO; import net.navasoft.madcoin.backend.services.vo.response.SuccessResponseVO; import net.navasoft.madcoin.backend.services.vo.response.impl.AppAllBusinessLinesSuccessResponseVO; import net.navasoft.madcoin.backend.services.vo.response.impl.AppBusinessLinesSuccessResponseVO; import net.navasoft.madcoin.backend.services.vo.response.impl.AppCategoriesSuccessResponseVO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; /** * net.navasoft.madcoin.backend.services.rest.impl Class class BusinessService. * Description: * * @author Juan Diego Navarre Gonzalez - (${authorMail}) * @version 1.0 * @since 21/08/2014 11:56:35 PM */ @Component("BusinessService") public class BusinessService implements IBusinessService { @Autowired @Qualifier("BusinessDAO") private IDataAccess<BusinessLines> dao; /** * Gets the business lines. * * @return the business lines * @since 21/08/2014, 11:56:35 PM */ @Override public SuccessResponseVO getBusinessLines() { BusinessSuccessResponseVO response = new AppBusinessLinesSuccessResponseVO(); List<BusinessLines> total = dao.getAll(); if (!total.isEmpty()) { BusinessLines[] totalLOB = (BusinessLines[]) Array.newInstance(BusinessLines.class, total.size()); response.setBusinessLines(total.toArray(totalLOB)); return response; } else { throw ControllerExceptionFactory.createException(InsufficientLOBException.class.getCanonicalName(), 1); } } /** * Gets the categories. * * @return the categories * @since 21/08/2014, 11:56:35 PM */ @Override public SuccessResponseVO getCategories(String lob) { BusinessSuccessResponseVO response = new AppCategoriesSuccessResponseVO(); try { BusinessLines requested = dao.getByLogicalId(lob); response.setBusinessLines(requested); Collection<ServiceCategories> categories = requested.getServiceCategoriesCollection(); if (!categories.isEmpty()) { response.setCategories(requested.getServiceCategoriesCollection().toArray( (ServiceCategories[]) Array.newInstance(ServiceCategories.class, categories.size()))); return response; } else { throw ControllerExceptionFactory .createException(InsufficientCategoryException.class.getCanonicalName(), 1); } } catch (NoResultException e) { throw ControllerExceptionFactory.createException(InexistentLOBException.class.getCanonicalName(), 2, new ControllerExceptionArgument(lob)); } } /** * Gets the all. * * @return the all * @since 1/09/2014, 10:52:57 PM */ @Override public SuccessResponseVO getAll() { BusinessSuccessResponseVO response = new AppAllBusinessLinesSuccessResponseVO(); List<BusinessLines> total = dao.getAll(); if (!total.isEmpty()) { BusinessLines[] totalLOB = (BusinessLines[]) Array.newInstance(BusinessLines.class, total.size()); response.setBusinessLines(total.toArray(totalLOB)); return response; } else { throw ControllerExceptionFactory.createException(InsufficientLOBException.class.getCanonicalName(), 1); } } }