Skip to content

Commit

Permalink
WIP - spotless
Browse files Browse the repository at this point in the history
  • Loading branch information
luciano-fiandesio committed Dec 18, 2024
1 parent ad785bb commit 8c33952
Show file tree
Hide file tree
Showing 5 changed files with 286 additions and 231 deletions.
Original file line number Diff line number Diff line change
@@ -1,48 +1,71 @@
/*
* Copyright (c) 2004-2024, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of the HISP project nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hisp.dhis.analytics.common;


import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

public class CTEContext {
private final Map<String, String> cteDefinitions = new LinkedHashMap<>();
private final Map<String, String> columnMappings = new HashMap<>();
private final Map<String, String> cteDefinitions = new LinkedHashMap<>();
private final Map<String, String> columnMappings = new HashMap<>();

public void addCTE(String cteName, String cteDefinition) {
cteDefinitions.put(cteName, cteDefinition);
}
public void addCTE(String cteName, String cteDefinition) {
cteDefinitions.put(cteName, cteDefinition);
}

public void addColumnMapping(String originalColumn, String cteReference) {
columnMappings.put(originalColumn, cteReference);
}
public void addColumnMapping(String originalColumn, String cteReference) {
columnMappings.put(originalColumn, cteReference);
}

public String getCTEDefinition() {
if (cteDefinitions.isEmpty()) {
return "";
}

StringBuilder sb = new StringBuilder("WITH ");
boolean first = true;
for (Map.Entry<String, String> entry : cteDefinitions.entrySet()) {
if (!first) {
sb.append(", ");
}
sb.append(entry.getKey())
.append(" AS (")
.append(entry.getValue())
.append(")");
first = false;
}
return sb.toString();
public String getCTEDefinition() {
if (cteDefinitions.isEmpty()) {
return "";
}

public Set<String> getCTENames() {
return cteDefinitions.keySet();
StringBuilder sb = new StringBuilder("WITH ");
boolean first = true;
for (Map.Entry<String, String> entry : cteDefinitions.entrySet()) {
if (!first) {
sb.append(", ");
}
sb.append(entry.getKey()).append(" AS (").append(entry.getValue()).append(")");
first = false;
}
return sb.toString();
}

public String getColumnMapping(String columnId) {
return columnMappings.getOrDefault(columnId, columnId);
}
public Set<String> getCTENames() {
return cteDefinitions.keySet();
}

public String getColumnMapping(String columnId) {
return columnMappings.getOrDefault(columnId, columnId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,17 @@ String getAggregateClauseForProgramIndicator(
Date latestDate);

void contributeCTE(
ProgramIndicator programIndicator,
AnalyticsType outerSqlEntity,
Date earliestStartDate,
Date latestDate,
CTEContext cteContext);
ProgramIndicator programIndicator,
AnalyticsType outerSqlEntity,
Date earliestStartDate,
Date latestDate,
CTEContext cteContext);

void contributeCTE(
ProgramIndicator programIndicator,
RelationshipType relationshipType,
AnalyticsType outerSqlEntity,
Date earliestStartDate,
Date latestDate,
CTEContext cteContext);
ProgramIndicator programIndicator,
RelationshipType relationshipType,

Check notice

Code scanning / CodeQL

Useless parameter Note

The parameter 'relationshipType' is never used.
AnalyticsType outerSqlEntity,

Check notice

Code scanning / CodeQL

Useless parameter Note

The parameter 'outerSqlEntity' is never used.
Date earliestStartDate,
Date latestDate,
CTEContext cteContext);
}
Original file line number Diff line number Diff line change
Expand Up @@ -1411,7 +1411,7 @@ protected List<String> getSelectColumnsWithCTE(EventQueryParams params, CTEConte
} else if (ValueType.ORGANISATION_UNIT == queryItem.getValueType()) {
// Handle org units
if (params.getCoordinateFields().stream()
.anyMatch(f -> queryItem.getItem().getUid().equals(f))) {
.anyMatch(f -> queryItem.getItem().getUid().equals(f))) {
columns.add(getCoordinateColumn(queryItem, OU_GEOMETRY_COL_SUFFIX).asSql());
} else {
columns.add(getOrgUnitQueryItemColumnAndAlias(params, queryItem).asSql());
Expand All @@ -1429,8 +1429,6 @@ protected List<String> getSelectColumnsWithCTE(EventQueryParams params, CTEConte
return columns;
}



/**
* Returns a select SQL clause for the given query.
*
Expand All @@ -1439,6 +1437,7 @@ protected List<String> getSelectColumnsWithCTE(EventQueryParams params, CTEConte
protected abstract String getSelectClause(EventQueryParams params);

protected abstract String getColumnWithCte(QueryItem item, String suffix, CTEContext cteContext);

/**
* Generates the SQL for the from-clause. Generally this means which analytics table to get data
* from.
Expand Down
Loading

0 comments on commit 8c33952

Please sign in to comment.