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.util.ArrayList; import java.util.Collection; import java.util.Iterator; import net.navasoft.madcoin.backend.model.entities.impl.ServiceProviders; import net.navasoft.madcoin.backend.model.entities.impl.WorkRequests; import net.navasoft.madcoin.backend.services.rest.Scope; import net.navasoft.madcoin.backend.services.rest.Transformer; import net.navasoft.madcoin.backend.services.rest.WorkingLoad; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.stereotype.Component; import org.springframework.web.context.support.SpringBeanAutowiringSupport; /** * net.navasoft.madcoin.backend.services.rest.impl Class class ProviderScope. * Description: * * @author Juan Diego Navarre Gonzalez - (${authorMail}) * @version 1.0 * @param <T> * the generic type * @since 8/09/2014 01:45:11 AM */ @Component("alcance") @org.springframework.context.annotation.Scope(BeanDefinition.SCOPE_PROTOTYPE) public final class ProviderScope<T extends WorkingLoad> implements Scope<T> { /** * Constant journal. * * @since 8/09/2014, 01:45:11 AM */ private static final Logger journal = Logger.getLogger(ProviderScope.class); /** * dao. * * @since 8/09/2014, 01:45:11 AM */ @Autowired @Qualifier("dataAccessProvider") private IDataAccess<ServiceProviders> dao; /** * translator. * * @since 8/09/2014, 01:45:11 AM */ @Autowired @Qualifier("providerTransformer") private Transformer<MasterLimiter, T> translator; /** * Gets the translator. * * @return the translator * @since 8/09/2014, 01:45:11 AM */ public Transformer<MasterLimiter, T> getTranslator() { return translator; } /** * Sets the translator. * * @param translator * the translator * @since 8/09/2014, 01:45:11 AM */ public void setTranslator(Transformer<MasterLimiter, T> translator) { this.translator = translator; } /** * originator. * * @since 8/09/2014, 01:45:11 AM */ private WorkRequests originator; /** * Instantiates a new provider scope. * * @param order * the order * @since 8/09/2014, 01:45:11 AM */ public ProviderScope(final WorkRequests order) { SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this); originator = order; } /** * Define scope. * * @return the collection * @since 8/09/2014, 01:45:11 AM */ @Override public Collection<T> defineScope() { Collection<ServiceProviders> workers = dao.getAll(); Iterator<ServiceProviders> it = workers.iterator(); Collection<T> scoped = new ArrayList<T>(workers.size()); while (it.hasNext()) { MasterLimiter worker = (MasterLimiter) it.next(); translator.transform(worker); scoped.add(translator.getTarget()); } return scoped; } /** * Validate scope. * * @param total * the total * @return the collection * @throws Exception * the exception * @since 8/09/2014, 01:45:11 AM */ @Override public Collection<T> validateScope(Collection<T> total) throws Exception { boolean valid = true; int na = 0; int arbitrary = total.size(); Collection<WorkingLoad> filtered = new ArrayList<WorkingLoad>(); for (T individual : total) { individual.setEvaluator(originator); boolean removals = individual.applyPolicies(); if (!removals) { filtered.add(individual); na++; } valid &= removals; } total.removeAll(filtered); journal.info("Cantidad de Elementos de alcance: " + total.size()); if (valid || (na != arbitrary)) { return total; } else { throw new Exception("Alcance totalmente invlido"); } } }