diff --git a/pom.xml b/pom.xml index 958b8a3a..c1ff1937 100644 --- a/pom.xml +++ b/pom.xml @@ -312,7 +312,7 @@ - + + + org.codehaus.mojo + flatten-maven-plugin + 1.1.0 + + true + resolveCiFriendliesOnly + + + + flatten + process-resources + + flatten + + + + flatten.clean + clean + + clean + + + org.apache.maven.plugins diff --git a/qaf-core/src/main/java/com/qmetry/qaf/automation/util/FileUtil.java b/qaf-core/src/main/java/com/qmetry/qaf/automation/util/FileUtil.java index 951b4344..ce412818 100644 --- a/qaf-core/src/main/java/com/qmetry/qaf/automation/util/FileUtil.java +++ b/qaf-core/src/main/java/com/qmetry/qaf/automation/util/FileUtil.java @@ -42,6 +42,7 @@ import org.apache.commons.codec.binary.Base64; import org.apache.commons.io.FileUtils; +import org.apache.commons.lang.LocaleUtils; public class FileUtil extends FileUtils { private static int counter = -1; /* Protected by tmpFileLock */ @@ -302,4 +303,12 @@ public static String getRelativePath(File fileOrFolder, File baseFolder) { return baseFolder.toURI().relativize(fileOrFolder.toURI()).getPath(); } + public static boolean isLocale(String name) { + String ext = getExtention(name); + try { + return StringUtil.isNotBlank(ext) && LocaleUtils.toLocale(ext).toString().equalsIgnoreCase(ext); + } catch (Exception e) { + return false; + } + } } diff --git a/qaf-tools/src/main/java/com/qmetry/qaf/automation/tools/MultiPropertiesEditorHelper.java b/qaf-tools/src/main/java/com/qmetry/qaf/automation/tools/MultiPropertiesEditorHelper.java new file mode 100644 index 00000000..bfbfe342 --- /dev/null +++ b/qaf-tools/src/main/java/com/qmetry/qaf/automation/tools/MultiPropertiesEditorHelper.java @@ -0,0 +1,133 @@ +package com.qmetry.qaf.automation.tools; + +import static com.qmetry.qaf.automation.util.FileUtil.checkCreateDir; +import static com.qmetry.qaf.automation.util.FileUtil.getExtention; +import static com.qmetry.qaf.automation.util.FileUtil.isLocale; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Path; +import java.util.Collection; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; + +import org.apache.commons.configuration.ConfigurationException; +import org.apache.commons.configuration.PropertiesConfiguration; + +import com.google.common.io.Files; +import com.qmetry.qaf.automation.util.StringUtil; + +public class MultiPropertiesEditorHelper { + + /** + * Example return value: + *
+	 * 
+	 * [
+	 * {"key":"some.prop","en_US":"valForUS","fr_FR":"valInFR"},
+	 * {"key":"another.prop","en_US":"val2ForUS","fr_FR":"val2InFR"},
+	 * ...
+	 * ]
+	 * 
+ * + * @param filePath + * @return + */ + + @SuppressWarnings("unchecked") + public static Collection> getContent(String filePath) { + File[] files = getFiles(filePath); + Map> entries = new LinkedHashMap>(); + + for(File file: files) { + try { + PropertiesConfiguration p = new PropertiesConfiguration(); + p.setEncoding(StandardCharsets.UTF_8.name()); + p.load(file); + + String colName = getExtention(file.getName()); + p.getKeys().forEachRemaining((k)->{ + Map entry = entries.get(k); + if(null == entry) { + entry = new HashMap(); + entry.put("key", (String)k); + entries.put((String)k, entry); + } + entry.put(colName, p.getString((String)k)); + }); + + } catch (Exception e) { + System.err.println("Unable to read " + file + e.getMessage()); + } + } + + return entries.values(); + } + + public static void saveContent(Collection> data, String path) { + Path filePath = Path.of(path); + String name = filePath.getFileName().toString(); + String parent = filePath.getParent().toString(); + saveContent(data, name, parent); + } + + public static void saveContent(Collection> data, String name, String parent) { + Map properties = new HashMap(); + + data.iterator().forEachRemaining(entry -> { + String key = (String) entry.remove("key"); + if (StringUtil.isNotBlank(key)) { + entry.entrySet().forEach(colEntry -> { + String colName = colEntry.getKey(); + PropertiesConfiguration p = properties.get(colName); + if (null == p) { + try { + checkCreateDir(parent); + File target = new File(parent, String.join(".", name, colName)); + target.createNewFile(); + p = new PropertiesConfiguration(); + p.setEncoding(StandardCharsets.UTF_8.toString()); + p.load(target); + p.setFile(target); + properties.put(colName, p); + } catch (IOException | ConfigurationException e) { + throw new RuntimeException(e); + } + + } + p.setProperty(key, colEntry.getValue()); + }); + } + }); + + properties.values().forEach(p->{ + try { + p.save(); + //p.save + } catch (ConfigurationException e2) { + throw new RuntimeException(e2); + } + }); + + } + + public static File[] getFiles(String name, boolean locale) { + if(!locale || !isLocale(name) ) return new File[] {}; + + File f = new File(name); + f.getParentFile().listFiles((dir,fname)->{ + return (!locale || isLocale(fname)) && Files.getNameWithoutExtension(fname).equalsIgnoreCase(Files.getNameWithoutExtension(name)); + }); + return null; + } + + public static File[] getFiles(String name) { + return getFiles(name, isLocale(name)); + } + + + + +} diff --git a/qaf-tools/src/main/java/com/qmetry/qaf/automation/tools/RepoEditor.java b/qaf-tools/src/main/java/com/qmetry/qaf/automation/tools/RepoEditor.java index f9e20bb5..661cd501 100644 --- a/qaf-tools/src/main/java/com/qmetry/qaf/automation/tools/RepoEditor.java +++ b/qaf-tools/src/main/java/com/qmetry/qaf/automation/tools/RepoEditor.java @@ -32,6 +32,7 @@ import java.net.URLDecoder; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; @@ -151,7 +152,7 @@ private static HttpServer createServer(int port) { break; case "get_content": res.setEntity(new StringEntity( - JSONUtil.toString(getContent(queryParams.get("path"))), + JSONUtil.toString(getContent(queryParams.get("path"),Boolean.getBoolean(queryParams.getOrDefault("multi","false")))), ContentType.APPLICATION_JSON)); break; case "save_wsc": @@ -163,7 +164,7 @@ private static HttpServer createServer(int port) { break; case "save_loc": String data = IOUtils.toString(req.getEntity().getContent(), StandardCharsets.UTF_8); - saveContent(JSONUtil.toObject(data, List.class), queryParams.get("path")); + saveContent(JSONUtil.toObject(data, List.class), queryParams.get("path"), Boolean.getBoolean(queryParams.getOrDefault("multi","false"))); break; case "load_resource": @@ -461,8 +462,8 @@ private static Map rename(Map queryParams) throw File newFile = new File(parent, newName); if (!FileUtil.getExtention(newName).equalsIgnoreCase(FileUtil.getExtention(old)) && (isWSC(newName) || isLOC(newName))) { try { - List> content = getContent(oldFile.getPath()); - saveContent(content, newFile.getPath()); + Collection> content = getContent(oldFile.getPath(), false); + saveContent(content, newFile.getPath(),false); oldFile.delete(); } catch (Exception e) { throw new RuntimeException("Unable to change file type: " + e.getMessage()); @@ -605,9 +606,9 @@ private static Object[] getNodes(String id) { }).toArray(); } //".wsc",".loc",".proto","json", "properties", "xml","txt", "csv" - String[] allowedTypes = QAF_CONTEXT.getStringArray("repoeditor.filetypes", ".wsc",".loc",".proto","properties",".locj",".wscj"); + String[] allowedTypes = QAF_CONTEXT.getStringArray("repoeditor.filetypes", ".wsc",".loc",".proto","properties",".locj",".wscj", ".bdd", ".bdl", ".feature"); Object[] nodes = Files.list(parent.toPath()) - .filter(p -> p.toFile().isDirectory() || StringUtil.endsWithAny(p.toString(), allowedTypes)) + .filter(p -> p.toFile().isDirectory() || StringUtil.endsWithAny(p.toString(), allowedTypes) || FileUtil.isLocale(p.toString())) .map(p -> { Map node = new HashMap(); node.put("text", p.toFile().getName()); @@ -629,9 +630,11 @@ private static Object[] getNodes(String id) { } @SuppressWarnings("unchecked") - private static List> getContent(String file) { + private static Collection> getContent(String file, boolean isMulti) { List> fileContent = new LinkedList>(); - + if(isMulti) { + return MultiPropertiesEditorHelper.getContent(file); + } if(file.endsWith(".wscj")) { Map content = JSONUtil.getJsonObjectFromFile(file, Map.class); if(null==content) @@ -692,17 +695,21 @@ private static List> getContent(String file) { return fileContent; } private static Map getWSCFile(String file){ - List> content = getContent(file); - return content.isEmpty()?JSONUtil.toMap("{}"):content.get(0); + Collection> content = getContent(file,false); + return content.isEmpty()?JSONUtil.toMap("{}"):content.iterator().next(); } - private static void saveContent(List> data, String file) throws IOException { + private static void saveContent(Collection> data, String file, boolean isMulti) throws IOException { + if(isMulti) { + File f = new File(file); + MultiPropertiesEditorHelper.saveContent(data, f.getName(), f.getParent()); + } Gson gson = new GsonBuilder().disableHtmlEscaping().serializeNulls().create(); String fileContent = null; if(file.endsWith(".wscj")) { - JSONUtil.writeJsonObjectToFile(file, data.get(0)); + JSONUtil.writeJsonObjectToFile(file, data.iterator().next()); }else if(file.endsWith(".wsc")) { - fileContent = data.get(0).entrySet().stream().map((e) -> e.getKey() +"=" + gson.toJson(e.getValue()).replace("\\", "\\\\")).collect(Collectors.joining("\n")); + fileContent = data.iterator().next().entrySet().stream().map((e) -> e.getKey() +"=" + gson.toJson(e.getValue()).replace("\\", "\\\\")).collect(Collectors.joining("\n")); } else if(file.endsWith(".loc")) { fileContent = data.stream().map(e->e.remove("key").toString()+"="+gson.toJson(e).replace("\\", "\\\\")).collect(Collectors.joining("\n")); @@ -714,15 +721,15 @@ else if(file.endsWith(".loc")) { if(null!=fileContent) { FileUtil.writeStringToFile(new File(file), fileContent, ApplicationProperties.LOCALE_CHAR_ENCODING.getStringVal("UTF-8")); + //to keep track added/updated wsc and properties + QAF_CONTEXT.load(new String[] {file}); } - //to keep track added/updated wsc and properties - QAF_CONTEXT.load(new String[] {file}); } private static void saveWSCFile(Map data,String file) throws IOException{ List> fileContent = new LinkedList>(); fileContent.add(data); - saveContent(fileContent,file); + saveContent(fileContent, file, false); } private static QAFRequestHandler handleExecuteRequest = (req, res, ctx) -> {