You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
1.8 KiB
72 lines
1.8 KiB
2 years ago
|
package com.mousetech.gourmetj.utils;
|
||
|
|
||
|
import java.io.ByteArrayOutputStream;
|
||
|
import java.io.PrintWriter;
|
||
|
import java.util.List;
|
||
|
|
||
|
import org.apache.commons.lang3.StringUtils;
|
||
|
import org.primefaces.model.ByteArrayContent;
|
||
|
import org.primefaces.model.StreamedContent;
|
||
|
|
||
|
import com.mousetech.gourmetj.ShopIngredient;
|
||
|
|
||
|
/**
|
||
|
* Construct a Primefaces file output content for an ingredient
|
||
|
* list in YAML format.
|
||
|
*
|
||
|
* @author timh
|
||
|
* @since Jan 15, 2022
|
||
|
*/
|
||
|
|
||
|
public class YamlShoppingList {
|
||
|
|
||
|
public static StreamedContent createDownload(
|
||
|
List<ShopIngredient> ingredientList) {
|
||
|
ByteArrayOutputStream ary = new ByteArrayOutputStream();
|
||
|
PrintWriter wtr = new PrintWriter(ary);
|
||
|
wtr.println("---");
|
||
|
formatContent(wtr, ingredientList);
|
||
|
wtr.close();
|
||
|
byte[] bas = ary.toByteArray();
|
||
|
|
||
|
StreamedContent dlList = new ByteArrayContent(bas,
|
||
|
"text/text", "shopping_list.yml");
|
||
|
return dlList;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Output line items in the ingredient list with topics for
|
||
|
* each Shopping Category.
|
||
|
*
|
||
|
* @param wtr Output Writer
|
||
|
* @param ingredientList Ingredient list to output.
|
||
|
*/
|
||
|
private static void formatContent(PrintWriter wtr,
|
||
|
List<ShopIngredient> ingredientList) {
|
||
|
String oldShopcat = null;
|
||
|
for (ShopIngredient ing : ingredientList) {
|
||
|
String newShopcat = ing.getShopCat();
|
||
|
if (StringUtils.isBlank(newShopcat)) {
|
||
|
newShopcat = "Unassigned";
|
||
|
}
|
||
|
if (!StringUtils.equals(newShopcat, oldShopcat)) {
|
||
|
wtr.println(newShopcat + ":");
|
||
|
oldShopcat = newShopcat;
|
||
|
}
|
||
|
wtr.print(" - ");
|
||
|
String displa = ing.getDisplayAmount();
|
||
|
wtr.print(displa);
|
||
|
if (!displa.isBlank()) {
|
||
|
wtr.print(' ');
|
||
|
}
|
||
|
String unit = ing.getUnit();
|
||
|
if (StringUtils.isNotBlank(unit)) {
|
||
|
wtr.print(unit);
|
||
|
wtr.print(' ');
|
||
|
}
|
||
|
wtr.println(ing.getItem());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|