All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aoindustries.website.signup.SignupCustomizeServerActionHelper Maven / Gradle / Ivy

/*
 * Copyright 2007-2009, 2015, 2016 by AO Industries, Inc.,
 * 7262 Bull Pen Cir, Mobile, Alabama, 36695, U.S.A.
 * All rights reserved.
 */
package com.aoindustries.website.signup;

import com.aoindustries.aoserv.client.AOServConnector;
import com.aoindustries.aoserv.client.PackageDefinition;
import com.aoindustries.aoserv.client.PackageDefinitionLimit;
import com.aoindustries.aoserv.client.Resource;
import com.aoindustries.encoding.ChainWriter;
import com.aoindustries.website.SiteSettings;
import static com.aoindustries.website.signup.ApplicationResources.accessor;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.validator.GenericValidator;

/**
 * Managed2Action and Dedicated2Action both use this to setup the request attributes.  This is implemented
 * here because inheritance is not possible and neither one is logically above the other.
 *
 * @author  AO Industries, Inc.
 */
final public class SignupCustomizeServerActionHelper {

	/**
	 * Make no instances.
	 */
	private SignupCustomizeServerActionHelper() {}

	public static void setRequestAttributes(
		ServletContext servletContext,
		HttpServletRequest request,
		HttpServletResponse response,
		SignupSelectPackageForm signupSelectPackageForm,
		SignupCustomizeServerForm signupCustomizeServerForm
	) throws IOException, SQLException {
		AOServConnector rootConn = SiteSettings.getInstance(servletContext).getRootAOServConnector();
		PackageDefinition packageDefinition = rootConn.getPackageDefinitions().get(signupSelectPackageForm.getPackageDefinition());
		if(packageDefinition==null) throw new SQLException("Unable to find PackageDefinition: "+signupSelectPackageForm.getPackageDefinition());
		List limits = packageDefinition.getLimits();

		// Find the cheapest resources to scale prices from
		int maxPowers = 0;
		PackageDefinitionLimit cheapestPower = null;
		int maxCPUs = 0;
		PackageDefinitionLimit cheapestCPU = null;
		int maxRAMs = 0;
		PackageDefinitionLimit cheapestRAM = null;
		int maxSataControllers = 0;
		PackageDefinitionLimit cheapestSataController = null;
		int maxScsiControllers = 0;
		PackageDefinitionLimit cheapestScsiController = null;
		int maxDisks = 0;
		PackageDefinitionLimit cheapestDisk = null;
		for(PackageDefinitionLimit limit : limits) {
			String resourceName = limit.getResource().getName();
			if(resourceName.startsWith("hardware_power_")) {
				int limitPower = limit.getHardLimit();
				if(limitPower>0) {
					if(limitPower>maxPowers) maxPowers = limitPower;
					if(cheapestPower==null) cheapestPower = limit;
					else {
						BigDecimal additionalRate = limit.getAdditionalRate();
						if(additionalRate==null) additionalRate=BigDecimal.valueOf(0, 2);
						BigDecimal cheapestRate = cheapestPower.getAdditionalRate();
						if(cheapestRate==null) cheapestRate=BigDecimal.valueOf(0, 2);
						if(additionalRate.compareTo(cheapestRate)<0) cheapestPower = limit;
					}
				}
			} else if(resourceName.startsWith("hardware_processor_")) {
				int limitCpu = limit.getHardLimit();
				if(limitCpu>0) {
					if(limitCpu>maxCPUs) maxCPUs = limitCpu;
					if(cheapestCPU==null) cheapestCPU = limit;
					else {
						BigDecimal additionalRate = limit.getAdditionalRate();
						if(additionalRate==null) additionalRate=BigDecimal.valueOf(0, 2);
						BigDecimal cheapestRate = cheapestCPU.getAdditionalRate();
						if(cheapestRate==null) cheapestRate=BigDecimal.valueOf(0, 2);
						if(additionalRate.compareTo(cheapestRate)<0) cheapestCPU = limit;
					}
				}
			} else if(resourceName.startsWith("hardware_ram_")) {
				int limitRAM = limit.getHardLimit();
				if(limitRAM>0) {
					if(limitRAM>maxRAMs) maxRAMs = limitRAM;
					if(cheapestRAM==null) cheapestRAM = limit;
					else {
						BigDecimal additionalRate = limit.getAdditionalRate();
						if(additionalRate==null) additionalRate=BigDecimal.valueOf(0, 2);
						BigDecimal cheapestRate = cheapestRAM.getAdditionalRate();
						if(cheapestRate==null) cheapestRate=BigDecimal.valueOf(0, 2);
						if(additionalRate.compareTo(cheapestRate)<0) cheapestRAM = limit;
					}
				}
			} else if(resourceName.startsWith("hardware_disk_controller_sata_")) {
				int limitSataController = limit.getHardLimit();
				if(limitSataController>0) {
					if(limitSataController>maxSataControllers) maxSataControllers = limitSataController;
					if(cheapestSataController==null) cheapestSataController = limit;
					else {
						BigDecimal additionalRate = limit.getAdditionalRate();
						if(additionalRate==null) additionalRate=BigDecimal.valueOf(0, 2);
						BigDecimal cheapestRate = cheapestSataController.getAdditionalRate();
						if(cheapestRate==null) cheapestRate=BigDecimal.valueOf(0, 2);
						if(additionalRate.compareTo(cheapestRate)<0) cheapestSataController = limit;
					}
				}
			} else if(resourceName.startsWith("hardware_disk_controller_scsi_")) {
				int limitScsiController = limit.getHardLimit();
				if(limitScsiController>0) {
					if(limitScsiController>maxScsiControllers) maxScsiControllers = limitScsiController;
					if(cheapestScsiController==null) cheapestScsiController = limit;
					else {
						BigDecimal additionalRate = limit.getAdditionalRate();
						if(additionalRate==null) additionalRate=BigDecimal.valueOf(0, 2);
						BigDecimal cheapestRate = cheapestScsiController.getAdditionalRate();
						if(cheapestRate==null) cheapestRate=BigDecimal.valueOf(0, 2);
						if(additionalRate.compareTo(cheapestRate)<0) cheapestScsiController = limit;
					}
				}
			} else if(resourceName.startsWith("hardware_disk_")) {
				int hardLimit = limit.getHardLimit();
				if(hardLimit>0) {
					if(cheapestDisk==null) cheapestDisk = limit;
					else {
						BigDecimal additionalRate = limit.getAdditionalRate();
						if(additionalRate==null) additionalRate=BigDecimal.valueOf(0, 2);
						BigDecimal cheapestRate = cheapestDisk.getAdditionalRate();
						if(cheapestRate==null) cheapestRate=BigDecimal.valueOf(0, 2);
						if(additionalRate.compareTo(cheapestRate)<0) cheapestDisk = limit;
					}
					if(hardLimit>maxDisks) maxDisks = hardLimit;
				}
			}
		}
		if(cheapestCPU==null) throw new SQLException("Unable to find cheapestCPU");
		if(cheapestRAM==null) throw new SQLException("Unable to find cheapestRAM");
		if(cheapestDisk==null) throw new SQLException("Unable to find cheapestDisk");

		// Find all the options
		List




© 2015 - 2025 Weber Informatics LLC | Privacy Policy