Commit cc973b0a authored by Brian Long's avatar Brian Long
Browse files

refactored sorters

parent 99d76df7
Loading
Loading
Loading
Loading
+16 −9
Original line number Diff line number Diff line
@@ -24,7 +24,7 @@ import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.inteligr8.maven.aps.modeling.util.ApsModelArrayNodeSorter;
import com.inteligr8.maven.aps.modeling.util.ArrayNodeObjectSorter;
import com.inteligr8.maven.aps.modeling.util.ModelUtil;

/**
@@ -66,20 +66,27 @@ public class ApsAppJsonNormalizer implements ApsFileNormalizer {
		// remove system-level and non-functional fields from models
		for (JsonNode _jsonModel : jsonModels) {
			ObjectNode jsonModel = (ObjectNode)_jsonModel;
			
			int fields = jsonModel.size();
			jsonModel.remove(Arrays.asList("lastUpdated"));
//			jsonModel.remove(Arrays.asList("createdBy", "createdByFullName", "lastUpdatedBy", "lastUpdatedByFullName", "lastUpdated"));
			if (jsonModel.size() < fields)
				changed = true;
			changed = this.transformModel(jsonModel) || changed;
		}
		
		// sort the models for better 'diff' support
		if (this.enableSorting)
			changed = ApsModelArrayNodeSorter.getInstance().sort(jsonModels, "name") || changed;
		if (this.enableSorting) {
			boolean sorted = ArrayNodeObjectSorter.getInstance().sort(jsonModels, "name");
			this.logger.trace("Sorted App models: {}: {}", modelName, sorted);
			changed = sorted || changed;
		}
		
		if (changed)
			ModelUtil.getInstance().writeJson(descriptor, file, this.enableSorting);
	}

	private boolean transformModel(ObjectNode jsonModel) {
		this.logger.trace("Removing excess App model meta-data: {}", jsonModel.get("name"));

		int fields = jsonModel.size();
		jsonModel.remove(Arrays.asList("lastUpdated"));
//		jsonModel.remove(Arrays.asList("createdBy", "createdByFullName", "lastUpdatedBy", "lastUpdatedByFullName", "lastUpdated"));
		return jsonModel.size() < fields;
	}

}
+1 −1
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@ import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.inteligr8.maven.aps.modeling.util.ApsModelArrayNodeSorter;
import com.inteligr8.maven.aps.modeling.util.ArrayNodeObjectSorter;
import com.inteligr8.maven.aps.modeling.util.ModelUtil;

/**
+0 −22
Original line number Diff line number Diff line
package com.inteligr8.maven.aps.modeling.util;

import com.fasterxml.jackson.databind.node.ArrayNode;

public class ApsModelArrayNodeSorter extends ArrayNodeObjectSorter {
	
	private static ApsModelArrayNodeSorter INSTANCE = new ApsModelArrayNodeSorter();
	
	public static ApsModelArrayNodeSorter getInstance() {
		return INSTANCE;
	}
	
	
	
	protected ApsModelArrayNodeSorter() {
	}
	
	public boolean sort(ArrayNode arrayNode, String jsonObjectFieldName) {
		return this.sort(arrayNode, new ObjectNodeComparator(jsonObjectFieldName));
	}

}
+0 −23
Original line number Diff line number Diff line
package com.inteligr8.maven.aps.modeling.util;

import java.util.List;
import java.util.Map;

public class ApsModelListMapSorter extends ListMapSorter<String, Object> {
	
	private static ApsModelListMapSorter INSTANCE = new ApsModelListMapSorter();
	
	public static ApsModelListMapSorter getInstance() {
		return INSTANCE;
	}
	
	
	
	protected ApsModelListMapSorter() {
	}
	
	public boolean sort(List<Map<String, Object>> jsonArrayAsMap, String jsonObjectFieldName) {
		return this.sort(jsonArrayAsMap, new MapComparator<String, Object>(jsonObjectFieldName));
	}

}
+47 −0
Original line number Diff line number Diff line
/*
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.inteligr8.maven.aps.modeling.util;

import java.util.Comparator;
@@ -5,10 +19,18 @@ import java.util.Comparator;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;

/**
 * A singleton class that provides JSON array sorting methods.
 * 
 * @author brian@inteligr8.com
 */
public class ArrayNodeObjectSorter {
	
	private static ArrayNodeObjectSorter INSTANCE = new ArrayNodeObjectSorter();
	
	/**
	 * @return A singleton instance of this class.
	 */
	public static ArrayNodeObjectSorter getInstance() {
		return INSTANCE;
	}
@@ -18,6 +40,31 @@ public class ArrayNodeObjectSorter {
	protected ArrayNodeObjectSorter() {
	}

	/**
	 * This method sorts the JSON object elements of the JSON array by the
	 * value of the specified field in the JSON objects.
	 * 
	 * If a JSON object has the specified field, its value is subject to the
	 * standard comparison.  If it doesn't have a value, then it will sort to
	 * the end of the array.
	 * 
	 * @param arrayNode A JSON array of JSON objects.
	 * @param jsonObjectFieldName A field in the JSON object elements.
	 * @return true if any elements were moved/sorted; false if unchanged.
	 * @see com.inteligr8.maven.aps.modeling.util.ObjectNodeComparator
	 */
	public boolean sort(ArrayNode arrayNode, String jsonObjectFieldName) {
		return this.sort(arrayNode, new ObjectNodeComparator(jsonObjectFieldName));
	}
	
	/**
	 * This method sorts the JSON node elements of the JSON array using the
	 * specified comparator.
	 * 
	 * @param arrayNode A JSON array of JSON objects.
	 * @param comparator A JSON node comparator.
	 * @return true if any elements were moved/sorted; false if unchanged.
	 */
	public boolean sort(ArrayNode arrayNode, Comparator<JsonNode> comparator) {
		if (arrayNode.isEmpty())
			return false;
Loading