Skip to content

Commit

Permalink
Meta attributes handling, pretty print methods sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-lizner committed Aug 21, 2015
1 parent a96efd5 commit 245d11c
Showing 1 changed file with 121 additions and 110 deletions.
Expand Up @@ -15,6 +15,8 @@
*/
package com.evolveum.midpoint.report.impl;

import com.evolveum.midpoint.prism.path.ItemPathSegment;
import com.evolveum.midpoint.prism.path.NameItemPathSegment;
import java.io.File;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -43,28 +45,26 @@
import com.evolveum.prism.xml.ns._public.types_3.ObjectDeltaType;
import com.evolveum.prism.xml.ns._public.types_3.ProtectedStringType;
import com.evolveum.prism.xml.ns._public.types_3.RawType;
import java.lang.reflect.Method;
import java.util.MissingResourceException;

/**
* Utility methods for report. Mostly pretty print functions. Do not use any
* "prism" object and anything related to them. Methods has to work with both,
* common schema types and extended schema types (prism)
*
*
* @author Katarina Valalikova
* @author Martin Lizner
*
*
*/

public class ReportUtils {

private static String MIDPOINT_HOME = System.getProperty("midpoint.home");
private static String EXPORT_DIR = MIDPOINT_HOME + "export/";


private static final Trace LOGGER = TraceManager
.getTrace(ReportUtils.class);


public static Timestamp convertDateTime(XMLGregorianCalendar dateTime) {
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
try {
Expand All @@ -76,7 +76,6 @@ public static Timestamp convertDateTime(XMLGregorianCalendar dateTime) {
return timestamp;
}


public static String getDateTime() {
Date createDate = new Date(System.currentTimeMillis());
SimpleDateFormat formatDate = new SimpleDateFormat("dd-MM-yyyy hh-mm-ss");
Expand Down Expand Up @@ -141,8 +140,6 @@ public static String getReportOutputFilePath(ReportType reportType) {
return output;
}



public static String getPropertyString(String key) {
return getPropertyString(key, null);
}
Expand Down Expand Up @@ -173,7 +170,6 @@ public static String prettyPrintForReport(ProtectedStringType pst) {
return "*****";
}


public static String prettyPrintForReport(OperationResultType ort) {
StringBuilder sb = new StringBuilder();
if (ort.getOperation() != null) {
Expand Down Expand Up @@ -216,102 +212,121 @@ public static String prettyPrintForReport(Collection prismValueList) {
- Credit goes to Evolveum
*/
public static String prettyPrintForReport(Object value) {
if (value == null){
return "";
}


if (value instanceof MetadataType){
return "";
}

//special handling for byte[], some problems with jasper when printing
if (byte[].class.equals(value.getClass())){
return prettyPrintForReport((byte[]) value);
}

String str = PrettyPrinter.prettyPrint(value);
if (str.length() > 1000){
return str.substring(0, 1000);
}
return str;
if (value == null) {
return "";
}

if (value instanceof MetadataType) {
return "";
}

//special handling for byte[], some problems with jasper when printing
if (byte[].class.equals(value.getClass())) {
return prettyPrintForReport((byte[]) value);
}

// 1. Try to find prettyPrintForReport in this class first
for (Method method : ReportUtils.class.getMethods()) {
if (method.getName().equals("prettyPrintForReport")) {
Class<?>[] parameterTypes = method.getParameterTypes();
if (parameterTypes.length == 1 && parameterTypes[0].equals(value.getClass())) {
try {
return (String) method.invoke(null, value);
} catch (Throwable e) {
return "###INTERNAL#ERROR### " + e.getClass().getName() + ": " + e.getMessage() + "; prettyPrintForReport method for value " + value;
}
}
}
}

// 2. Default to PrettyPrinter.prettyPrint
String str = PrettyPrinter.prettyPrint(value);
if (str.length() > 1000) {
return str.substring(0, 1000);
}
return str;

}
private static String printItemDeltaValues(ItemDeltaType itemDelta){
List values = itemDelta.getValue();
StringBuilder sb = new StringBuilder();
for (Object value : values){
String v = printItemDeltaValue(itemDelta.getPath(), value);
if (StringUtils.isNotBlank(v)){
sb.append(v);
sb.append(", ");
}
}
sb.setLength(Math.max(sb.length() - 2, 0)); // delete last delimiter
return sb.toString();

private static String printItemDeltaValues(ItemDeltaType itemDelta) {
List values = itemDelta.getValue();
StringBuilder sb = new StringBuilder();
for (Object value : values) {
String v = printItemDeltaValue(itemDelta.getPath(), value);
if (StringUtils.isNotBlank(v)) {
sb.append(v);
sb.append(", ");
}
}
sb.setLength(Math.max(sb.length() - 2, 0)); // delete last delimiter
return sb.toString();
}

private static String printItemDeltaValue(ItemPathType itemPath, Object value){
if (value instanceof MetadataType){
return "";
} else if (value instanceof RawType){
try {

if (isMetadata(itemPath)){
return "";
}
return prettyPrintForReport(((RawType) value).getParsedRealValue(null, itemPath.getItemPath()));
} catch (SchemaException e) {
return "###INTERNAL#ERROR### " + e.getClass().getName() + ": " + e.getMessage() + "; prettyPrintForReport method for value " + value;
} catch (RuntimeException e){
return "###INTERNAL#ERROR### " + e.getClass().getName() + ": " + e.getMessage() + "; prettyPrintForReport method for value " + value;
} catch (Exception e){
return "###INTERNAL#ERROR### " + e.getClass().getName() + ": " + e.getMessage() + "; prettyPrintForReport method for value " + value;
}
} else {
return prettyPrintForReport(value);
}


private static String printItemDeltaValue(ItemPathType itemPath, Object value) {
if (value instanceof MetadataType) {
return "";
} else if (value instanceof RawType) {
try {

if (isMetadata(itemPath)) {
return "";
}
return prettyPrintForReport(((RawType) value).getParsedRealValue(null, itemPath.getItemPath()));
} catch (SchemaException e) {
return "###INTERNAL#ERROR### " + e.getClass().getName() + ": " + e.getMessage() + "; prettyPrintForReport method for value " + value;
} catch (RuntimeException e) {
return "###INTERNAL#ERROR### " + e.getClass().getName() + ": " + e.getMessage() + "; prettyPrintForReport method for value " + value;
} catch (Exception e) {
return "###INTERNAL#ERROR### " + e.getClass().getName() + ": " + e.getMessage() + "; prettyPrintForReport method for value " + value;
}
} else {
return prettyPrintForReport(value);
}
}
private static String printItemDeltaOldValues(ItemPathType itemPath, List values){
StringBuilder sb = new StringBuilder();
for (Object value : values){
String v = printItemDeltaValue(itemPath, value);
if (StringUtils.isNotBlank(v)){
sb.append(v);
sb.append(", ");
}
}
sb.setLength(Math.max(sb.length() - 2, 0)); // delete last delimiter
return sb.toString();

private static String printItemDeltaOldValues(ItemPathType itemPath, List values) {
StringBuilder sb = new StringBuilder();
for (Object value : values) {
String v = printItemDeltaValue(itemPath, value);
if (StringUtils.isNotBlank(v)) {
sb.append(v);
sb.append(", ");
}
}
sb.setLength(Math.max(sb.length() - 2, 0)); // delete last delimiter
return sb.toString();

}
private static boolean isMetadata(ItemDeltaType itemDelta){
List values = itemDelta.getValue();
for (Object v : values){
if (v instanceof MetadataType){
return true;
} else if (v instanceof RawType){
return isMetadata(itemDelta.getPath());
}
}
return false;

private static boolean isMetadata(ItemDeltaType itemDelta) {
List values = itemDelta.getValue();
for (Object v : values) {
if (v instanceof MetadataType) {
return true;
} else if (v instanceof RawType) {
return isMetadata(itemDelta.getPath());
}
}

return false;
}

private static boolean isMetadata(ItemPathType itemPath){
return com.evolveum.midpoint.prism.path.ItemPath.getFirstName(itemPath.getItemPath()).getLocalPart().equals("metadata");

private static boolean isMetadata(ItemPathType itemPath) {
boolean retMeta = false;
for (ItemPathSegment ips : itemPath.getItemPath().getSegments()) {
if (ips instanceof NameItemPathSegment && "metadata".equals(((NameItemPathSegment) ips).getName().getLocalPart())) {
return true;
}
}
return retMeta;
}

public static String prettyPrintForReport(ItemDeltaType itemDelta) {
StringBuilder sb = new StringBuilder();
boolean displayNA = false;
if (isMetadata(itemDelta)){
return sb.toString();

if (isMetadata(itemDelta)) {
return sb.toString();
}

sb.append(">>> ");
Expand All @@ -326,8 +341,8 @@ public static String prettyPrintForReport(ItemDeltaType itemDelta) {
sb.append(", ");
displayNA = true;
}
if (itemDelta.getModificationType() == ModificationTypeType.REPLACE){

if (itemDelta.getModificationType() == ModificationTypeType.REPLACE) {
sb.append("Replace: ");
sb.append("{");
sb.append(printItemDeltaValues(itemDelta));
Expand Down Expand Up @@ -364,12 +379,10 @@ public static String prettyPrintForReport(ItemDeltaType itemDelta) {
return sb.toString();
}


public static String getBusinessDisplayName(ObjectReferenceType ort){
return ort.getDescription();
public static String getBusinessDisplayName(ObjectReferenceType ort) {
return ort.getDescription();
}


private static String printChangeType(ObjectDeltaType delta, String opName) {
StringBuilder sb = new StringBuilder();
sb.append(opName);
Expand All @@ -383,19 +396,17 @@ private static String printChangeType(ObjectDeltaType delta, String opName) {
return sb.toString();
}


public static String printDelta(List<ObjectDeltaType> delta) {
StringBuilder sb = new StringBuilder();
for (ObjectDeltaType d : delta){
sb.append(printDelta(d));
sb.append("\n");
}
return sb.toString();
StringBuilder sb = new StringBuilder();
for (ObjectDeltaType d : delta) {
sb.append(printDelta(d));
sb.append("\n");
}
return sb.toString();
}

public static String printDelta(ObjectDeltaType delta) {
StringBuilder sb = new StringBuilder();
Boolean isMeta;

switch (delta.getChangeType()) {
case MODIFY:
Expand Down

0 comments on commit 245d11c

Please sign in to comment.