use of org.apache.commons.lang3.tuple.Triple in project alf.io by alfio-event.
the class ReservationApiController method validateEUVat.
@RequestMapping(value = "/event/{eventName}/reservation/{reservationId}/vat-validation", method = RequestMethod.POST)
@Transactional
public ResponseEntity<VatDetail> validateEUVat(@PathVariable("eventName") String eventName, @PathVariable("reservationId") String reservationId, PaymentForm paymentForm, Locale locale, HttpServletRequest request) {
String country = paymentForm.getVatCountryCode();
Optional<Triple<Event, TicketReservation, VatDetail>> vatDetail = eventRepository.findOptionalByShortName(eventName).flatMap(e -> ticketReservationRepository.findOptionalReservationById(reservationId).map(r -> Pair.of(e, r))).filter(e -> EnumSet.of(INCLUDED, NOT_INCLUDED).contains(e.getKey().getVatStatus())).filter(e -> vatChecker.isVatCheckingEnabledFor(e.getKey().getOrganizationId())).flatMap(e -> vatChecker.checkVat(paymentForm.getVatNr(), country, e.getKey().getOrganizationId()).map(vd -> Triple.of(e.getLeft(), e.getRight(), vd)));
vatDetail.filter(t -> t.getRight().isValid()).ifPresent(t -> {
VatDetail vd = t.getRight();
String billingAddress = vd.getName() + "\n" + vd.getAddress();
PriceContainer.VatStatus vatStatus = determineVatStatus(t.getLeft().getVatStatus(), t.getRight().isVatExempt());
ticketReservationRepository.updateBillingData(vatStatus, vd.getVatNr(), country, paymentForm.isInvoiceRequested(), reservationId);
OrderSummary orderSummary = ticketReservationManager.orderSummaryForReservationId(reservationId, t.getLeft(), Locale.forLanguageTag(t.getMiddle().getUserLanguage()));
ticketReservationRepository.addReservationInvoiceOrReceiptModel(reservationId, Json.toJson(orderSummary));
ticketReservationRepository.updateTicketReservation(reservationId, t.getMiddle().getStatus().name(), paymentForm.getEmail(), paymentForm.getFullName(), paymentForm.getFirstName(), paymentForm.getLastName(), locale.getLanguage(), billingAddress, null, Optional.ofNullable(paymentForm.getPaymentMethod()).map(PaymentProxy::name).orElse(null));
paymentForm.getTickets().forEach((ticketId, owner) -> {
if (isNotEmpty(owner.getEmail()) && ((isNotEmpty(owner.getFirstName()) && isNotEmpty(owner.getLastName())) || isNotEmpty(owner.getFullName()))) {
ticketHelper.preAssignTicket(eventName, reservationId, ticketId, owner, Optional.empty(), request, (tr) -> {
}, Optional.empty());
}
});
});
return vatDetail.map(Triple::getRight).map(vd -> {
if (vd.isValid()) {
return ResponseEntity.ok(vd);
} else {
return new ResponseEntity<VatDetail>(HttpStatus.BAD_REQUEST);
}
}).orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
}
use of org.apache.commons.lang3.tuple.Triple in project EnderIO by SleepyTrousers.
the class ContainerWiredCharger method getValidPair.
public static NNList<Triple<ItemStack, ItemStack, Integer>> getValidPair(List<ItemStack> validItems) {
NNList<Triple<ItemStack, ItemStack, Integer>> result = new NNList<>();
for (ItemStack stack : validItems) {
if (PowerHandlerUtil.getCapability(stack, null) != null) {
ItemStack copy = stack.copy();
IEnergyStorage emptyCap = PowerHandlerUtil.getCapability(copy, null);
if (emptyCap != null) {
int extracted = 1, maxloop = 200;
while (extracted > 0 && emptyCap.canExtract() && maxloop-- > 0) {
extracted = emptyCap.extractEnergy(Integer.MAX_VALUE, false);
}
if (emptyCap.canReceive() && emptyCap.getEnergyStored() < emptyCap.getMaxEnergyStored()) {
ItemStack empty = copy.copy();
int added = emptyCap.receiveEnergy(Integer.MAX_VALUE, false);
int power = added;
maxloop = 200;
while (added > 0 && maxloop-- > 0) {
power += added = emptyCap.receiveEnergy(Integer.MAX_VALUE, false);
}
result.add(Triple.of(empty, copy, power));
}
}
}
}
return result;
}
use of org.apache.commons.lang3.tuple.Triple in project EnderIO by SleepyTrousers.
the class RecipeLoader method addRecipes.
public static void addRecipes() {
final RecipeFactory recipeFactory = new RecipeFactory(Config.getConfigDirectory(), EnderIO.DOMAIN);
recipeFactory.createFolder("recipes");
recipeFactory.createFolder("recipes/user");
recipeFactory.createFolder("recipes/examples");
recipeFactory.placeXSD("recipes");
recipeFactory.placeXSD("recipes/user");
recipeFactory.placeXSD("recipes/examples");
recipeFactory.createFileUser("recipes/user/user_recipes.xml");
NNList<Triple<Integer, RecipeFactory, String>> recipeFiles = new NNList<>();
for (ModContainer modContainer : Loader.instance().getModList()) {
Object mod = modContainer.getMod();
if (mod instanceof IEnderIOAddon) {
recipeFiles.addAll(((IEnderIOAddon) mod).getRecipeFiles());
for (String filename : ((IEnderIOAddon) mod).getExampleFiles()) {
recipeFactory.copyCore("recipes/examples/" + filename + ".xml");
}
}
}
Collections.sort(recipeFiles, new Comparator<Triple<Integer, RecipeFactory, String>>() {
@Override
public int compare(Triple<Integer, RecipeFactory, String> o1, Triple<Integer, RecipeFactory, String> o2) {
return o1.getLeft().compareTo(o2.getLeft());
}
});
Set<File> userfiles = new HashSet<>(recipeFactory.listXMLFiles("recipes/user"));
for (Triple<Integer, RecipeFactory, String> triple : recipeFiles) {
RecipeFactory factory = triple.getMiddle();
if (factory != null) {
userfiles.addAll(factory.listXMLFiles("recipes/user"));
}
}
/*
* Note that we load the recipes in core-imc-user order but merge them in reverse order. The loading order allows aliases to be added in the expected order,
* while the reverse merging allows user recipes to replace imc recipes to replace core recipes.
*/
Recipes config = new Recipes();
if (RecipeConfig.loadCoreRecipes.get()) {
try {
for (Triple<Integer, RecipeFactory, String> triple : recipeFiles) {
config = readCoreFile(NullHelper.first(triple.getMiddle(), recipeFactory), "recipes/" + triple.getRight()).addRecipes(config, false);
}
} catch (InvalidRecipeConfigException e) {
recipeError(NullHelper.first(e.getFilename(), "Core Recipes"), e.getMessage());
}
}
if (imcRecipes != null) {
config = handleIMCRecipes(config);
imcRecipes = null;
}
for (File file : userfiles) {
final Recipes userFile = readUserFile(recipeFactory, file.getName(), file);
if (userFile != null) {
try {
config = userFile.addRecipes(config, true);
} catch (InvalidRecipeConfigException e) {
recipeError(NullHelper.first(e.getFilename(), file.getName()), e.getMessage());
}
}
}
config.register("");
for (ModContainer modContainer : Loader.instance().getModList()) {
Object mod = modContainer.getMod();
if (mod instanceof IEnderIOAddon) {
((IEnderIOAddon) mod).postRecipeRegistration();
}
}
}
use of org.apache.commons.lang3.tuple.Triple in project syncope by apache.
the class ResourceLogic method listConnObjects.
@PreAuthorize("hasRole('" + StandardEntitlement.RESOURCE_LIST_CONNOBJECT + "')")
@Transactional(readOnly = true)
public Pair<SearchResult, List<ConnObjectTO>> listConnObjects(final String key, final String anyTypeKey, final int size, final String pagedResultsCookie, final List<OrderByClause> orderBy) {
ExternalResource resource;
ObjectClass objectClass;
OperationOptions options;
if (SyncopeConstants.REALM_ANYTYPE.equals(anyTypeKey)) {
resource = resourceDAO.authFind(key);
if (resource == null) {
throw new NotFoundException("Resource '" + key + "'");
}
if (resource.getOrgUnit() == null) {
throw new NotFoundException("Realm provisioning for resource '" + key + "'");
}
objectClass = resource.getOrgUnit().getObjectClass();
options = MappingUtils.buildOperationOptions(MappingUtils.getPropagationItems(resource.getOrgUnit().getItems()).iterator());
} else {
Triple<ExternalResource, AnyType, Provision> init = connObjectInit(key, anyTypeKey);
resource = init.getLeft();
objectClass = init.getRight().getObjectClass();
init.getRight().getMapping().getItems();
Set<MappingItem> linkinMappingItems = virSchemaDAO.findByProvision(init.getRight()).stream().map(virSchema -> virSchema.asLinkingMappingItem()).collect(Collectors.toSet());
Iterator<MappingItem> mapItems = new IteratorChain<>(init.getRight().getMapping().getItems().iterator(), linkinMappingItems.iterator());
options = MappingUtils.buildOperationOptions(mapItems);
}
final List<ConnObjectTO> connObjects = new ArrayList<>();
SearchResult searchResult = connFactory.getConnector(resource).search(objectClass, null, new ResultsHandler() {
private int count;
@Override
public boolean handle(final ConnectorObject connectorObject) {
connObjects.add(ConnObjectUtils.getConnObjectTO(connectorObject));
// safety protection against uncontrolled result size
count++;
return count < size;
}
}, size, pagedResultsCookie, orderBy, options);
return ImmutablePair.of(searchResult, connObjects);
}
use of org.apache.commons.lang3.tuple.Triple in project syncope by apache.
the class ResourceLogic method readConnObject.
@PreAuthorize("hasRole('" + StandardEntitlement.RESOURCE_GET_CONNOBJECT + "')")
@Transactional(readOnly = true)
public ConnObjectTO readConnObject(final String key, final String anyTypeKey, final String anyKey) {
Triple<ExternalResource, AnyType, Provision> init = connObjectInit(key, anyTypeKey);
// 1. find any
Any<?> any = init.getMiddle().getKind() == AnyTypeKind.USER ? userDAO.find(anyKey) : init.getMiddle().getKind() == AnyTypeKind.ANY_OBJECT ? anyObjectDAO.find(anyKey) : groupDAO.find(anyKey);
if (any == null) {
throw new NotFoundException(init.getMiddle() + " " + anyKey);
}
// 2. build connObjectKeyItem
Optional<MappingItem> connObjectKeyItem = MappingUtils.getConnObjectKeyItem(init.getRight());
if (!connObjectKeyItem.isPresent()) {
throw new NotFoundException("ConnObjectKey mapping for " + init.getMiddle() + " " + anyKey + " on resource '" + key + "'");
}
Optional<String> connObjectKeyValue = mappingManager.getConnObjectKeyValue(any, init.getRight());
// 3. determine attributes to query
Set<MappingItem> linkinMappingItems = virSchemaDAO.findByProvision(init.getRight()).stream().map(virSchema -> virSchema.asLinkingMappingItem()).collect(Collectors.toSet());
Iterator<MappingItem> mapItems = new IteratorChain<>(init.getRight().getMapping().getItems().iterator(), linkinMappingItems.iterator());
// 4. read from the underlying connector
Connector connector = connFactory.getConnector(init.getLeft());
ConnectorObject connectorObject = connector.getObject(init.getRight().getObjectClass(), AttributeBuilder.build(connObjectKeyItem.get().getExtAttrName(), connObjectKeyValue.get()), MappingUtils.buildOperationOptions(mapItems));
if (connectorObject == null) {
throw new NotFoundException("Object " + connObjectKeyValue.get() + " with class " + init.getRight().getObjectClass() + " not found on resource " + key);
}
// 5. build result
Set<Attribute> attributes = connectorObject.getAttributes();
if (AttributeUtil.find(Uid.NAME, attributes) == null) {
attributes.add(connectorObject.getUid());
}
if (AttributeUtil.find(Name.NAME, attributes) == null) {
attributes.add(connectorObject.getName());
}
return ConnObjectUtils.getConnObjectTO(connectorObject);
}
Aggregations