/** * Prints a link. * * @param link resource link * @param more flag to indicate if there will be more arguments */ protected void printLink(ResourceLink link, boolean more, String format) { if (!link.getTag().isEmpty() && !link.getUrl().isEmpty()) { println("{"); levelUp(); printLinkArg("accessURL", link.getUrl(), true); printLinkArg("format", format, false); levelDown(); println("}" + (more ? "," : "")); } }
/** * Finds metadata url from resource links * * @param links the resource links * @return the metadata url */ private String getIdentifierUrl(ResourceLinks links) { String identifierUrl = ""; for (int j = 0; j < links.size(); j++) { ResourceLink link = links.get(j); if (link.getTag().equals(ResourceLink.TAG_METADATA)) { identifierUrl = link.getUrl(); return identifierUrl; } } return identifierUrl; }
/** * Prints all links. * * @param links collection of resource links * @param before flag to indicate if there will be more arguments */ protected void printLinks(ResourceLinks links, boolean more, boolean before) { if (before) { print(false, ","); print(false, "\r\n"); } println("\"distribution\"" + sp() + ":" + sp() + "["); levelUp(); boolean moreLinks = false; for (int j = 0; j < links.size(); j++) { boolean bPrintLink = false; String format = ""; ResourceLink link = links.get(j); /*if (link.getTag().equals(ResourceLink.TAG_OPEN)) { defaultValues.put("webService", link.getUrl()); }*/ if (link.getTag().equals(ResourceLink.TAG_METADATA)) { bPrintLink = true; if (!moreLinks) { moreLinks = true; } else { moreLinks = false; } format = "text/xml"; } if (link.getTag().equals(ResourceLink.TAG_DETAILS)) { bPrintLink = true; if (!moreLinks) { moreLinks = true; } else { moreLinks = false; } format = "text/html"; } if (bPrintLink) { printLink(link, moreLinks, format); } } levelDown(); println("]" + (more ? "," : "")); }
/** * Writes fields value to response using field mappings. * * @param index the lucene index records * @param fieldName the lucene field * @param jsonKey the dcat field * @param delimiter the delimiter in lucene field * @param before the indentation flag * @param isDate true if date field * @return always <code>true</code> */ private boolean writeField( IFeedRecord r, Map<String, IFeedAttribute> index, DcatField dcatField, String delimiter, boolean before) { String indexFieldName = dcatField.getIndex(); String dcatFieldName = dcatField.getName(); String fieldType = dcatField.getType(); String fldValues = ""; String[] flds = indexFieldName.split(","); for (String fld : flds) { IFeedAttribute indexValue = index.get(fld); String val = ""; if (indexValue == null) { if (dcatField.isRequired()) { if (dcatFieldName.equalsIgnoreCase("accessURL")) { ResourceLinks links = r.getResourceLinks(); for (int j = 0; j < links.size(); j++) { ResourceLink link = links.get(j); if (link.getTag().equals(ResourceLink.TAG_METADATA)) { val = link.getUrl(); break; } } } else { val = defaultValues.get(dcatFieldName); } } else { continue; } } else { val = "" + indexValue; if (dcatFieldName.equalsIgnoreCase("format") && val.equalsIgnoreCase("[\"unknown\"]")) { val = defaultValues.get(dcatFieldName); } } String cleanedVal = cleanValue(val, fieldType, dcatFieldName, dcatField); if (dcatFieldName.equalsIgnoreCase("dataDictionary") && !(cleanedVal.startsWith("http://") || cleanedVal.startsWith("https://"))) { continue; } if (!fldValues.contains(cleanedVal)) { if (fldValues.length() > 0) { StringBuilder sb = new StringBuilder(); if (fieldType.equalsIgnoreCase("array")) { if (!cleanedVal.equalsIgnoreCase(defaultValues.get(dcatFieldName))) { if (fldValues.startsWith("[") && fldValues.endsWith("]")) { fldValues = fldValues.replace("[", "").replace("]", ""); } if (cleanedVal.startsWith("[") && cleanedVal.endsWith("]")) { cleanedVal = cleanedVal.replace("[", "").replace("]", ""); } sb.append("[").append(fldValues).append(delimiter).append(cleanedVal).append("]"); fldValues = sb.toString(); } } else { sb.append(fldValues).append(delimiter).append(cleanedVal); fldValues = sb.toString(); } } else { fldValues += cleanedVal; } } } if (fldValues.length() == 0) { fldValues = defaultValues.get(dcatFieldName); if (fldValues == null) { fldValues = ""; } } if (fldValues.length() > 0) { fldValues = fldValues.replaceAll(",", ", "); if (before) { print(false, ","); print(false, "\r\n"); } if (!fldValues.startsWith("\"") && !fldValues.startsWith("[") && !fldValues.endsWith("\"") && !fldValues.endsWith("]")) { fldValues = "\"" + fldValues + "\""; } print(before, "\"" + dcatFieldName + "\"" + sp() + ":" + sp() + fldValues); before = true; } return before; }