X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=src%2Fclub%2Fwpia%2Fgigi%2FdbObjects%2FName.java;h=ce8e7e2a1ca465eceecc4a6e84117f43fa4a18eb;hp=da7e7190665adb1dee3d6d52307b0aedbfbff720;hb=4869b9224eed6aad66ea926c808bcbcfa472012b;hpb=0984c9d455d1c37bdd8107c8ada3fca25943cb58 diff --git a/src/club/wpia/gigi/dbObjects/Name.java b/src/club/wpia/gigi/dbObjects/Name.java index da7e7190..ce8e7e2a 100644 --- a/src/club/wpia/gigi/dbObjects/Name.java +++ b/src/club/wpia/gigi/dbObjects/Name.java @@ -1,6 +1,9 @@ package club.wpia.gigi.dbObjects; import java.io.PrintWriter; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; import java.util.Map; import club.wpia.gigi.GigiApiException; @@ -11,6 +14,7 @@ import club.wpia.gigi.dbObjects.NamePart.NamePartType; import club.wpia.gigi.localisation.Language; import club.wpia.gigi.output.template.Outputable; import club.wpia.gigi.util.HTMLEncoder; +import club.wpia.gigi.util.TimeConditions; public class Name implements Outputable, IdCachable { @@ -45,6 +49,11 @@ public class Name implements Outputable, IdCachable { */ public abstract void output(PrintWriter out); + /** + * @see Name#toInitialsString() + */ + public abstract String toInitialsString(); + } private static class SingleName extends SchemedName { @@ -70,6 +79,11 @@ public class Name implements Outputable, IdCachable { return singlePart.getValue(); } + @Override + public String toInitialsString() { + return singlePart.getValue().substring(0, 1); + } + @Override public NameSchemaType getSchemeName() { return NameSchemaType.SINGLE; @@ -187,17 +201,20 @@ public class Name implements Outputable, IdCachable { @Override public void output(PrintWriter out) { - outputNameParts(out, "fname", firstNames); - outputNameParts(out, "lname", lastNames); - outputNameParts(out, "suffix", suffixes); + outputNameParts(out, "fname", firstNames, false); + outputNameParts(out, "lname", lastNames, true); + outputNameParts(out, "suffix", suffixes, true); } - private void outputNameParts(PrintWriter out, String type, NamePart[] input) { + private void outputNameParts(PrintWriter out, String type, NamePart[] input, boolean leadingSpace) { StringBuilder res; res = new StringBuilder(); appendArray(res, input); if (res.length() > 0) { res.deleteCharAt(res.length() - 1); + if (leadingSpace) { + out.print(" "); + } out.print(""); out.print(HTMLEncoder.encodeHTML(res.toString())); out.print(""); @@ -220,6 +237,14 @@ public class Name implements Outputable, IdCachable { public String toAbbreviatedString() { return firstNames[0].getValue() + " " + lastNames[0].getValue().charAt(0) + "."; } + + @Override + public String toInitialsString() { + + String initals = getInitialByNamePart(firstNames, lastNames, suffixes); + + return initals; + } } private int id; @@ -375,8 +400,8 @@ public class Name implements Outputable, IdCachable { /** * Transforms this String into a short form. This short form should not be - * unique. (For "western" names this would be - * "firstName firstCharOfLastName.".) + * unique. (For "western" names this would be "firstName + * firstCharOfLastName.".) * * @return the short form of the name */ @@ -384,6 +409,17 @@ public class Name implements Outputable, IdCachable { return scheme.toAbbreviatedString(); } + /** + * Transforms this Name object into a short form. This short form might not + * be unique. (For "western" names this would be all first letters of each + * name part) + * + * @return the short form of the name + */ + public String toInitialsString() { + return scheme.toInitialsString(); + } + public int getVerificationPoints() { try (GigiPreparedStatement query = new GigiPreparedStatement("SELECT SUM(`points`) FROM (SELECT DISTINCT ON (`from`, `method`) `points` FROM `notary` WHERE `to`=? AND `deleted` IS NULL AND (`expire` IS NULL OR `expire` > CURRENT_TIMESTAMP) ORDER BY `from`, `method`, `when` DESC) AS p")) { query.setInt(1, getId()); @@ -457,4 +493,49 @@ public class Name implements Outputable, IdCachable { } return owner; } + + private static String getInitialByNamePart(NamePart[]... npa) { + StringBuilder initals = new StringBuilder(); + for (NamePart[] np : npa) { + initals.append(getInitialByNamePart(np)); + } + return initals.toString(); + } + + private static String getInitialByNamePart(NamePart[] np) { + StringBuilder initals = new StringBuilder(); + for (NamePart p : np) { + switch (p.getValue()) { + case "-": + case "/": + break; + default: + initals.append(p.getValue().substring(0, 1).toUpperCase()); + break; + } + } + return initals.toString(); + } + + public boolean isValidVerification() { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + Calendar c = Calendar.getInstance(); + c.setTimeInMillis(System.currentTimeMillis()); + c.add(Calendar.MONTH, -TimeConditions.getInstance().getVerificationMonths()); + String date = sdf.format(new Date(c.getTimeInMillis())); + try (GigiPreparedStatement query = new GigiPreparedStatement("SELECT COUNT(id) FROM `notary` WHERE `to` = ? AND `deleted` IS NULL AND (`expire` IS NULL OR `expire` > CURRENT_TIMESTAMP) AND `date` > ?")) { + query.setInt(1, getId()); + query.setString(2, date); + GigiResultSet rs = query.executeQuery(); + + if (rs.next()) { + if (rs.getInt(1) > 0) { + return true; + } + } + + return false; + } + } + }