Skip to content

Commit

Permalink
Merge pull request #91 from urbancamo/develop
Browse files Browse the repository at this point in the history
 - fix dxcc entity identification (prefer dxcc in the input ADIF file…
  • Loading branch information
urbancamo authored Jun 2, 2024
2 parents 65583ec + 40b0c8b commit d1436c6
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 31 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<groupId>uk.m0nom</groupId>
<artifactId>adif-processor</artifactId>
<version>1.3.8</version>
<version>1.3.9</version>

<name>ADIF Processor</name>
<url>https://github.com/urbancamo/adif-processor</url>
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/uk/m0nom/FileProcessorApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class FileProcessorApplication implements CommandLineRunner, ProgressFeed
private final Adif3FileReader reader;
private final Adif3FileWriter writer;

private final ActivityDatabaseService summits;
private final ActivityDatabaseService activityDatabaseService;

private final Adif3PrintFormatter formatter;
private final KmlWriter kmlWriter;
Expand All @@ -73,12 +73,12 @@ public class FileProcessorApplication implements CommandLineRunner, ProgressFeed
}

public FileProcessorApplication(Adif3Transformer transformer, Adif3FileReader reader, Adif3FileWriter writer,
ActivityDatabaseService summits, Adif3PrintFormatter formatter, KmlWriter kmlWriter,
ActivityDatabaseService activityDatabaseService, Adif3PrintFormatter formatter, KmlWriter kmlWriter,
CachingQrzXmlService qrzXmlService) {
this.transformer = transformer;
this.reader = reader;
this.writer = writer;
this.summits = summits;
this.activityDatabaseService = activityDatabaseService;
this.formatter = formatter;
this.kmlWriter = kmlWriter;
this.qrzXmlService = qrzXmlService;
Expand Down Expand Up @@ -120,7 +120,7 @@ public void run(String... args) {
String markdown = String.format("%s%s-%s.%s", outPath, inBasename, "fta", "md");
logger.info(String.format("Running from: %s", new File(".").getAbsolutePath()));
try {
summits.loadData();
activityDatabaseService.loadData();
JsonDxccEntities jsonDxccEntities = new DxccJsonReader().read();
DxccEntities dxccEntities = new DxccEntities();
try {
Expand All @@ -145,13 +145,13 @@ public void run(String... args) {
qsos = transformer.transform(log, control, results, this, null);
logger.info(String.format("Writing output file %s with encoding %s", out, control.getEncoding()));
if (control.getGenerateKml()) {
kmlWriter.write(control, kml, inBasename, summits, qsos, results);
kmlWriter.write(control, kml, inBasename, activityDatabaseService, qsos, results);
if (StringUtils.isNotEmpty(results.getError())) {
logger.severe(results.getError());
}
}
// Contest Calculations
log.getHeader().setPreamble(new ContestResultsCalculator(summits).calculateResults(log));
log.getHeader().setPreamble(new ContestResultsCalculator(activityDatabaseService).calculateResults(log));

writer.write(out, control.getEncoding(), log);
if (control.isFormattedOutput()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,8 @@ public void transform(TransformControl control, TransformResults results, Qsos q
// Set DXCC Entities
ZonedDateTime date = qso.getRecord().getQsoDate();
if (date != null) {
qso.getFrom().setDxccEntity(control.getDxccEntities().findDxccEntityFromCallsign(qso.getFrom().getCallsign(), qso.getRecord().getQsoDate()));
qso.getTo().setDxccEntity(control.getDxccEntities().findDxccEntityFromCallsign(qso.getTo().getCallsign(), qso.getRecord().getQsoDate()));
control.getDxccEntities().setFromDxccEntity(qso, control);
control.getDxccEntities().setToDxccEntity(qso, control);
} else {
results.addWarning(String.format("Record %d with station %s has no date", index, qso.getTo().getCallsign()));
}
Expand Down
48 changes: 33 additions & 15 deletions src/main/java/uk/m0nom/adifproc/dxcc/DxccEntities.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.marsik.ham.adif.Adif3Record;
import uk.m0nom.adifproc.adif3.contacts.Qso;
import uk.m0nom.adifproc.adif3.control.TransformControl;
import uk.m0nom.adifproc.callsign.CallsignUtils;

import java.text.ParseException;
Expand Down Expand Up @@ -47,13 +50,11 @@ public Collection<DxccEntity> findEntitiesFromPrefix(String prefix, ZonedDateTim
private DxccEntity tryFindEntitiesForSuffix(String callsign, ZonedDateTime qsoDate) {
String swappedCallsign = CallsignUtils.swapSuffixToPrefix(callsign);
Collection<DxccEntity> matches = findEntitiesFromPrefix(swappedCallsign, qsoDate);
switch (matches.size()) {
case 1:
return matches.iterator().next();
case 2:
return bestMatch(swappedCallsign, matches);
}
return null;
return switch (matches.size()) {
case 1 -> matches.iterator().next();
case 2 -> bestMatch(swappedCallsign, matches);
default -> null;
};
}

public DxccEntity findDxccEntityFromCallsign(String callsign, ZonedDateTime qsoDate) {
Expand All @@ -64,14 +65,11 @@ public DxccEntity findDxccEntityFromCallsign(String callsign, ZonedDateTime qsoD
matches = findEntitiesFromPrefix(callsign, qsoDate);
}

switch (matches.size()) {
case 0:
return null;
case 1:
return matches.iterator().next();
default:
return bestMatch(callsign, matches);
}
return switch (matches.size()) {
case 0 -> null;
case 1 -> matches.iterator().next();
default -> bestMatch(callsign, matches);
};
}

public Collection<String> getPrefixesForDxccEntity(DxccEntity entity) {
Expand Down Expand Up @@ -160,4 +158,24 @@ private DxccEntity checkForLongestMatchingPrefix(String callsign, Map<String, Dx
}
return prefixesToCheck.get(longestPrefix);
}

public void setFromDxccEntity(Qso qso, TransformControl control) {
Adif3Record rec = qso.getRecord();
if (rec.getDxcc() != null) {
qso.getFrom().setDxccEntity(control.getDxccEntities().getDxccEntity(rec.getMyDxcc()));
} else {
qso.getFrom().setDxccEntity(control.getDxccEntities().findDxccEntityFromCallsign(qso.getFrom().getCallsign(), qso.getRecord().getQsoDate()));
}
}

public void setToDxccEntity(Qso qso, TransformControl control) {
Adif3Record rec = qso.getRecord();
if (rec.getMyDxcc() != null) {
qso.getTo().setDxccEntity(control.getDxccEntities().getDxccEntity(rec.getDxcc()));
} else {
qso.getTo().setDxccEntity(control.getDxccEntities().findDxccEntityFromCallsign(qso.getTo().getCallsign(), qso.getRecord().getQsoDate()));
}
}


}
3 changes: 1 addition & 2 deletions src/main/java/uk/m0nom/adifproc/dxcc/DxccPermutations.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package uk.m0nom.adifproc.dxcc;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -17,7 +16,7 @@ public static List<String> generate(String begin, String end) {
i--;
}
int suffixStart = i+1;
String suffix = begin.substring(suffixStart, begin.length());
String suffix = begin.substring(suffixStart);

// String suffix off begin and end and generate variants
List<String> variants = generateInternal(begin.substring(0, suffixStart), end.substring(0, suffixStart));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package uk.m0nom.adifproc.adif3;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.marsik.ham.adif.Adif3;
import org.marsik.ham.adif.Adif3Record;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import uk.m0nom.adifproc.FileProcessorApplicationConfig;
import uk.m0nom.adifproc.activity.ActivityDatabaseService;
import uk.m0nom.adifproc.adif3.contacts.Qso;
import uk.m0nom.adifproc.adif3.contacts.Station;
import uk.m0nom.adifproc.adif3.io.Adif3FileReader;
import uk.m0nom.adifproc.adif3.io.Adif3FileWriter;
import uk.m0nom.adifproc.adif3.print.Adif3PrintFormatter;
import uk.m0nom.adifproc.dxcc.DxccEntities;
import uk.m0nom.adifproc.dxcc.DxccJsonReader;
import uk.m0nom.adifproc.dxcc.JsonDxccEntities;
import uk.m0nom.adifproc.kml.KmlWriter;
import uk.m0nom.adifproc.qrz.CachingQrzXmlService;
import uk.m0nom.adifproc.adif3.control.TransformControl;
import java.text.ParseException;

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = FileProcessorApplicationConfig.class)
public class PreferAdifDxccEntityOverCalculatedDxccEntityFromCallsignTest {
@Autowired
Adif3Transformer transformer;

@Autowired
Adif3FileReader reader;

@Autowired
Adif3FileWriter writer;

@Autowired
ActivityDatabaseService summits;

@Autowired
Adif3PrintFormatter formatter;

@Autowired
KmlWriter kmlWriter;

@Autowired
CachingQrzXmlService qrzXmlService;

TransformControl transformControl;

@Test
public void testCountryOverDxcc() {
try {
transformControl = new TransformControl();
JsonDxccEntities jsonDxccEntities = new DxccJsonReader().read();
DxccEntities dxccEntities = new DxccEntities();
try {
dxccEntities.setup(jsonDxccEntities);
} catch (ParseException p) {
Assertions.fail(p);
}

transformControl.setDxccEntities(dxccEntities);
Adif3 input = reader.read("./target/test-classes/adif/LU7HF.adi", "windows-1251", false);
assert input != null;
assert input.getRecords().size() == 1;
Adif3Record record = input.getRecords().get(0);
assert record.getCall().equals("LU7HF");
assert record.getCountry().equals("Argentina");
assert record.getDxcc().equals(100);

Qso qso = new Qso(record, 0);
Station from = new Station(record.getStationCallsign(), qso);
Station to = new Station(record.getStationCallsign(), qso);
to.setDxccEntity(dxccEntities.getDxccEntity(100));

qso.setTo(to);
qso.setFrom(from);

dxccEntities.setToDxccEntity(qso, transformControl);
assert(qso.getTo().getDxccEntity().getEntityCode() == 100);

} catch (Exception e) {
Assertions.fail("testApp() threw exception while setting up the test. ", e);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
* Test the QrzXmlService is able to look up data on a callsign
* NOTE: you will need to plug in your username and password for this to work correctly
* (which is why be default this test is ignored)
* (which is why the default this test is ignored)
*/
public class QrzXmlServiceIntegrationTest {
private final static String USERNAME = "";
Expand Down
53 changes: 53 additions & 0 deletions src/test/resources/adif/LU7HF.adi
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
### QLog ADIF Export
<ADIF_VER:5>3.1.4
<PROGRAMID:4>QLog
<PROGRAMVERSION:6>0.34.0
<CREATED_TIMESTAMP:15>20240515 003039
<EOH>

<call:5>LU7HF
<rst_sent:3>599
<rst_rcvd:3>599
<freq:6:N>24.895
<band:3>12m
<mode:2>CW
<name:15>MARCELO GABRIEL
<qth:23>SAN FRANCISCO - CORDOBA
<gridsquare:6>FF88XS
<dxcc:3>100
<country:9>Argentina
<cont:2>SA
<cqz:2>13
<ituz:2>14
<pfx:3>LU7
<state:2>Se
<qsl_rcvd:1>N
<qsl_sent:1>Q
<lotw_qsl_rcvd:1>N
<lotw_qsl_sent:1>Y
<lotw_qslsdate:8>20240510
<tx_pwr:3>100
<a_index:1>7
<band_rx:3>12m
<distance:17>7002.559901983147
<eqsl_qslsdate:8>20240511
<eqsl_qsl_rcvd:1>N
<eqsl_qsl_sent:1>Y
<freq_rx:6>24.895
<k_index:1>2
<my_antenna:7>doublet
<my_city:11>Bushnell FL
<my_country:13>United States
<my_cq_zone:1>5
<my_dxcc:3>291
<my_gridsquare:4>EL88
<my_itu_zone:1>8
<my_name:4>Dave
<my_rig:5>FT710
<sfi:3>233
<station_callsign:4>KZ1O
<qso_date:8:D>20240509
<time_on:6:T>213531
<qso_date_off:8:D>20240509
<time_off:6:T>213639
<eor>

0 comments on commit d1436c6

Please sign in to comment.