
org.ligoj.app.plugin.prov.AbstractProvQuoteResource Maven / Gradle / Ivy
/*
* Licensed under MIT (https://github.com/ligoj/ligoj/blob/master/LICENSE)
*/
package org.ligoj.app.plugin.prov;
import org.apache.commons.lang3.StringUtils;
import org.ligoj.app.plugin.prov.dao.BaseProvQuoteRepository;
import org.ligoj.app.plugin.prov.dao.BaseProvTypeRepository;
import org.ligoj.app.plugin.prov.model.*;
import org.ligoj.app.plugin.prov.quote.support.QuoteTagSupport;
import org.ligoj.bootstrap.core.IDescribableBean;
import org.ligoj.bootstrap.core.dao.RestRepository;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.function.Function;
/**
* The resource part of the provisioning.
*
* @param Quoted resource type.
* @param Quoted resource price type.
* @param Quoted resource price type's type.
* @param Quoted resource edition VO type.
* @since 1.8.5
*/
public abstract class AbstractProvQuoteResource, C extends AbstractQuote, E extends IDescribableBean>
extends AbstractCostedResource {
@Autowired
protected ProvTagResource tagResource;
@Autowired
protected ProvNetworkResource networkResource;
/**
* Return the repository managing the instance pricing entities.
*
* @return The repository managing the instance pricing entities.
*/
public abstract RestRepository getIpRepository();
/**
* Return the repository managing the quote entities.
*
* @return The repository managing the quote entities.
*/
public abstract BaseProvQuoteRepository getQiRepository();
/**
* Return the repository managing the instance type entities.
*
* @return The repository managing the instance type entities.
*/
public abstract BaseProvTypeRepository getItRepository();
/**
* Return the resource type managed by this service.
*
* @return The resource type managed by this service.
*/
protected abstract ResourceType getType();
/**
* Create the container inside a quote.
*
* @param vo The quote container.
* @return The created container cost details with identifier.
*/
public abstract UpdatedCost create(final E vo);
/**
* Update the container inside a quote.
*
* @param vo The quote container to update.
* @return The new cost configuration.
*/
public abstract UpdatedCost update(final E vo);
/**
* Delete all resources type from a quote. The total cost is updated.
*
* @param subscription The related subscription.
* @return The updated computed cost.
*/
protected UpdatedCost deleteAll(final int subscription) {
final var quote = resource.getQuoteFromSubscription(subscription);
final var cost = new UpdatedCost(0);
tagResource.onDeleteAll(getType(), quote.getId());
networkResource.onDeleteAll(getType(), quote.getId());
// Delete all resources
final var repository = getQiRepository();
cost.getDeleted().put(getType(), repository.findAllIdentifiers(quote));
repository.deleteAll(repository.findAllBy("configuration.subscription.id", subscription));
repository.flush();
// Update the cost. Note the effort could be reduced to a simple subtract of deleted resource costs.
resource.updateCost(quote);
return resource.refreshSupportCost(cost, quote);
}
protected void saveOrUpdate(final C entity, final QuoteTagSupport vo) {
// Add tags
tagResource.replaceTags(vo.getTags(), entity);
}
/**
* Delete a storage from a quote. The total cost is updated.
*
* @param id The {@link ProvQuoteStorage}'s identifier to delete.
* @return The updated computed cost.
*/
protected UpdatedCost delete(final int id) {
tagResource.onDelete(getType(), id);
networkResource.onDelete(getType(), id);
return resource.refreshSupportCost(new UpdatedCost(id),
deleteAndUpdateCost(getQiRepository(), id, Function.identity()::apply));
}
/**
* Return a normalized form a string.
*
* @param value The raw value.
* @return The normalized value.
*/
protected String normalize(final String value) {
return StringUtils.trimToEmpty(StringUtils.upperCase(value));
}
/**
* Return a normalized form a quote resource.
*
* @param quote The optional quote.
* @return The normalized value of the type's code.
*/
protected String normalize(final AbstractQuote> quote) {
if (quote == null) {
return "";
}
return quote.getPrice().getType().getCode();
}
/**
* Return the rate replacing the null
value by the minimal constraint
*
* @param rate The query context.
* @return The adjusted rate, never null
.
*/
protected Rate normalize(final Rate rate) {
return rate == null ? Rate.WORST : rate;
}
/**
* Return the identifier replacing the null
value by 0.
*
* @param value The query context.
* @return The adjusted identifier, never null
.
*/
protected int normalize(final Integer value) {
return value == null ? 0 : value;
}
/**
* Return the boolean replacing the null
value by 0.
*
* @param value The query context.
* @return The adjusted boolean, never null
.
*/
protected boolean normalize(final Boolean value) {
return value != null && value;
}
/**
* Return the resolved resource requirement from the resource or from the quote.
*
* @param quoteValue Quote's value.
* @param value The local requirement value.
* @return The resolved requirement, default is true
.
*/
protected boolean normalize(final Boolean quoteValue, final Boolean value) {
return quoteValue == null ? normalize(value) : quoteValue;
}
}