List of usage examples for org.joda.time DateTime minusDays
public DateTime minusDays(int days)
From source file:jp.co.ntt.atrs.domain.service.b0.TicketSharedServiceImpl.java
License:Apache License
/** * {@inheritDoc}/*from w w w. j av a 2 s. co m*/ */ @Override public boolean isAvailableFareType(FareType fareType, Date depDate) { Assert.notNull(fareType); Assert.notNull(depDate); DateTime depDateMidnight = new DateTime(depDate).withTimeAtStartOfDay(); // DateTime rsrvAvailableStartDate = depDateMidnight.minusDays(fareType.getRsrvAvailableStartDayNum()); // DateTime rsrvAvailableEndDate = depDateMidnight.minusDays(fareType.getRsrvAvailableEndDayNum()); // ? DateTime sysDateMidnight = dateFactory.newDateTime().withTimeAtStartOfDay(); // ????????? return new Interval(rsrvAvailableStartDate, rsrvAvailableEndDate.plusDays(1)).contains(sysDateMidnight); }
From source file:kr.debop4j.timeperiod.tools.Times.java
License:Apache License
/** * Gets start of week./*w w w .j a va2 s .c o m*/ * * @param time the time * @param firstDayOfWeek the first day of week * @return the start of week */ public static DateTime getStartOfWeek(DateTime time, DayOfWeek firstDayOfWeek) { DateTime current = time.withTimeAtStartOfDay(); while (current.getDayOfWeek() != firstDayOfWeek.getValue()) { current = current.minusDays(1); } return current; }
From source file:kr.debop4j.timeperiod.tools.Times.java
License:Apache License
/** * ?? ?? ?? ?? .//from w w w .j a v a 2s .c om * * @param moment ?? * @param dayOfWeek ? ? */ public static DateTime previousDayOfWeek(DateTime moment, DayOfWeek dayOfWeek) { int dow = dayOfWeek.getValue(); DateTime prev = moment.minusDays(1); while (prev.getDayOfWeek() != dow) { prev = prev.minusDays(1); } return prev; }
From source file:me.vertretungsplan.additionalinfo.BaseIcalParser.java
License:Mozilla Public License
@Override public AdditionalInfo getAdditionalInfo() throws IOException { AdditionalInfo info = new AdditionalInfo(); info.setTitle(getTitle());/*w w w. j a va2s.c om*/ String rawdata = httpGet(getIcalUrl(), "UTF-8"); if (shouldStripTimezoneInfo()) { Pattern pattern = Pattern.compile("BEGIN:VTIMEZONE.*END:VTIMEZONE", Pattern.DOTALL); rawdata = pattern.matcher(rawdata).replaceAll(""); } DateTime now = DateTime.now().withTimeAtStartOfDay(); List<ICalendar> icals = Biweekly.parse(rawdata).all(); List<Event> events = new ArrayList<>(); for (ICalendar ical : icals) { for (VEvent event : ical.getEvents()) { Event item = new Event(); TimeZone timezoneStart = getTimeZoneStart(ical, event); if (event.getDescription() != null) { item.description = event.getDescription().getValue(); } if (event.getSummary() != null) { item.summary = event.getSummary().getValue(); } if (event.getDateStart() != null) { item.startDate = new DateTime(event.getDateStart().getValue()); item.startHasTime = event.getDateStart().getValue().hasTime(); } else { continue; } if (event.getDateEnd() != null) { item.endDate = new DateTime(event.getDateEnd().getValue()); item.endHasTime = event.getDateEnd().getValue().hasTime(); } if (event.getLocation() != null) { item.location = event.getLocation().getValue(); } if (event.getUrl() != null) { item.url = event.getUrl().getValue(); } if (event.getRecurrenceRule() == null && item.endDate != null && (item.endDate.compareTo(now) < 0)) { continue; } else if (event.getRecurrenceRule() == null && (item.startDate.compareTo(now) < 0)) { continue; } if (event.getRecurrenceRule() != null && event.getRecurrenceRule().getValue().getUntil() != null && event.getRecurrenceRule().getValue().getUntil().compareTo(now.toDate()) < 0) { continue; } if (event.getRecurrenceRule() != null) { Duration duration = null; if (event.getDateEnd() != null) { duration = new Duration(new DateTime(event.getDateStart().getValue()), new DateTime(event.getDateEnd().getValue())); } DateIterator iterator = event.getDateIterator(timezoneStart); while (iterator.hasNext()) { Date date = iterator.next(); Event reccitem = item.clone(); reccitem.startDate = new DateTime(date); reccitem.endDate = reccitem.startDate.plus(duration); if (item.startDate.equals(reccitem.startDate)) continue; if (item.endDate != null && (item.endDate.compareTo(now) < 0)) { continue; } else if (item.endDate == null && (item.startDate.compareTo(now) < 0)) { continue; } events.add(reccitem); } } if (item.endDate != null && (item.endDate.compareTo(now) < 0)) { continue; } else if (item.endDate == null && (item.startDate.compareTo(now) < 0)) { continue; } events.add(item); } } Collections.sort(events, new Comparator<Event>() { @Override public int compare(Event o1, Event o2) { return o1.startDate.compareTo(o2.startDate); } }); StringBuilder content = new StringBuilder(); int count = 0; DateTimeFormatter fmtDt = DateTimeFormat.shortDateTime().withLocale(Locale.GERMANY); DateTimeFormatter fmtD = DateTimeFormat.shortDate().withLocale(Locale.GERMANY); DateTimeFormatter fmtT = DateTimeFormat.shortTime().withLocale(Locale.GERMANY); for (Event item : events) { if (count >= getMaxItemsCount()) { break; } else if (count != 0) { content.append("<br><br>\n\n"); } DateTime start = item.startDate; if (item.endDate != null) { DateTime end = item.endDate; if (!item.endHasTime) { end = end.minusDays(1); } content.append((item.startHasTime ? fmtDt : fmtD).print(start)); if (!end.equals(start)) { content.append(" - "); if (item.startHasTime && item.endHasTime && end.toLocalDate().equals(start.toLocalDate())) { content.append(fmtT.print(end)); } else { content.append((item.endHasTime ? fmtDt : fmtD).print(end)); } } } else { content.append(fmtDt.print(start)); } content.append("<br>\n"); content.append("<b>"); content.append(item.summary); content.append("</b>"); count++; } info.setText(content.toString()); return info; }
From source file:mekhq.campaign.personnel.Person.java
License:Open Source License
public static Person generateInstanceFromXML(Node wn, Campaign c, Version version) { Person retVal = null;//from www .j av a2 s .com try { // Instantiate the correct child class, and call its parsing function. retVal = new Person(c); // Okay, now load Person-specific fields! NodeList nl = wn.getChildNodes(); String advantages = null; String edge = null; String implants = null; //backwards compatability String pilotName = null; String pilotNickname = null; int pilotGunnery = -1; int pilotPiloting = -1; int pilotCommandBonus = -1; int type = 0; for (int x = 0; x < nl.getLength(); x++) { Node wn2 = nl.item(x); if (wn2.getNodeName().equalsIgnoreCase("name")) { retVal.name = wn2.getTextContent(); } else if (wn2.getNodeName().equalsIgnoreCase("maidenname")) { retVal.maidenname = wn2.getTextContent(); } else if (wn2.getNodeName().equalsIgnoreCase("callsign")) { retVal.callsign = wn2.getTextContent(); } else if (wn2.getNodeName().equalsIgnoreCase("commander")) { retVal.commander = Boolean.parseBoolean(wn2.getTextContent().trim()); } else if (wn2.getNodeName().equalsIgnoreCase("dependent")) { retVal.dependent = Boolean.parseBoolean(wn2.getTextContent().trim()); } else if (wn2.getNodeName().equalsIgnoreCase("isClanTech") || wn2.getNodeName().equalsIgnoreCase("clan")) { retVal.clan = Boolean.parseBoolean(wn2.getTextContent().trim()); } else if (wn2.getNodeName().equalsIgnoreCase("phenotype")) { retVal.phenotype = Integer.parseInt(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("bloodname")) { retVal.bloodname = wn2.getTextContent(); } else if (wn2.getNodeName().equalsIgnoreCase("biography")) { retVal.biography = wn2.getTextContent(); } else if (wn2.getNodeName().equalsIgnoreCase("primaryRole")) { retVal.primaryRole = Integer.parseInt(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("secondaryRole")) { retVal.secondaryRole = Integer.parseInt(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("acquisitions")) { retVal.acquisitions = Integer.parseInt(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("primaryDesignator")) { retVal.primaryDesignator = Integer.parseInt(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("secondaryDesignator")) { retVal.secondaryDesignator = Integer.parseInt(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("daysToWaitForHealing")) { retVal.daysToWaitForHealing = Integer.parseInt(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("idleMonths")) { retVal.idleMonths = Integer.parseInt(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("id")) { if (version.getMajorVersion() == 0 && version.getMinorVersion() < 2 && version.getSnapshot() < 14) { retVal.oldId = Integer.parseInt(wn2.getTextContent()); } else { retVal.id = UUID.fromString(wn2.getTextContent()); } } else if (wn2.getNodeName().equalsIgnoreCase("ancestors")) { retVal.ancestorsID = UUID.fromString(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("spouse")) { retVal.spouse = UUID.fromString(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("duedate")) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); retVal.dueDate = (GregorianCalendar) GregorianCalendar.getInstance(); retVal.dueDate.setTime(df.parse(wn2.getTextContent().trim())); } else if (wn2.getNodeName().equalsIgnoreCase("teamId")) { retVal.teamId = Integer.parseInt(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("portraitCategory")) { retVal.setPortraitCategory(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("portraitFile")) { retVal.setPortraitFileName(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("xp")) { retVal.xp = Integer.parseInt(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("nTasks")) { retVal.nTasks = Integer.parseInt(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("hits")) { retVal.hits = Integer.parseInt(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("gender")) { retVal.gender = Integer.parseInt(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("rank")) { if (Version.versionCompare(version, "0.3.4-r1782")) { RankTranslator rt = new RankTranslator(c); try { retVal.rank = rt.getNewRank(c.getRanks().getOldRankSystem(), Integer.parseInt(wn2.getTextContent())); } catch (ArrayIndexOutOfBoundsException e) { // Do nothing } } else { retVal.rank = Integer.parseInt(wn2.getTextContent()); } } else if (wn2.getNodeName().equalsIgnoreCase("rankLevel")) { retVal.rankLevel = Integer.parseInt(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("rankSystem")) { retVal.setRankSystem(Integer.parseInt(wn2.getTextContent())); } else if (wn2.getNodeName().equalsIgnoreCase("maneiDominiRank")) { retVal.maneiDominiRank = Integer.parseInt(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("maneiDominiClass")) { retVal.maneiDominiClass = Integer.parseInt(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("doctorId")) { if (version.getMajorVersion() == 0 && version.getMinorVersion() < 2 && version.getSnapshot() < 14) { retVal.oldDoctorId = Integer.parseInt(wn2.getTextContent()); } else { if (!wn2.getTextContent().equals("null")) { retVal.doctorId = UUID.fromString(wn2.getTextContent()); } } } else if (wn2.getNodeName().equalsIgnoreCase("unitId")) { if (version.getMajorVersion() == 0 && version.getMinorVersion() < 2 && version.getSnapshot() < 14) { retVal.oldUnitId = Integer.parseInt(wn2.getTextContent()); } else { if (!wn2.getTextContent().equals("null")) { retVal.unitId = UUID.fromString(wn2.getTextContent()); } } } else if (wn2.getNodeName().equalsIgnoreCase("status")) { retVal.status = Integer.parseInt(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("prisonerstatus")) { retVal.prisonerStatus = Integer.parseInt(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("willingToDefect")) { retVal.willingToDefect = Boolean.parseBoolean(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("salary")) { retVal.salary = Integer.parseInt(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("minutesLeft")) { retVal.minutesLeft = Integer.parseInt(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("overtimeLeft")) { retVal.overtimeLeft = Integer.parseInt(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("birthday")) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); retVal.birthday = (GregorianCalendar) GregorianCalendar.getInstance(); retVal.birthday.setTime(df.parse(wn2.getTextContent().trim())); } else if (wn2.getNodeName().equalsIgnoreCase("deathday")) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); retVal.deathday = (GregorianCalendar) GregorianCalendar.getInstance(); retVal.deathday.setTime(df.parse(wn2.getTextContent().trim())); } else if (wn2.getNodeName().equalsIgnoreCase("advantages")) { advantages = wn2.getTextContent(); } else if (wn2.getNodeName().equalsIgnoreCase("edge")) { edge = wn2.getTextContent(); } else if (wn2.getNodeName().equalsIgnoreCase("implants")) { implants = wn2.getTextContent(); } else if (wn2.getNodeName().equalsIgnoreCase("toughness")) { retVal.toughness = Integer.parseInt(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("pilotGunnery")) { pilotGunnery = Integer.parseInt(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("pilotPiloting")) { pilotPiloting = Integer.parseInt(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("pilotHits")) { retVal.hits = Integer.parseInt(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("pilotCommandBonus")) { pilotCommandBonus = Integer.parseInt(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("pilotName")) { pilotName = wn2.getTextContent(); } else if (wn2.getNodeName().equalsIgnoreCase("pilotNickname")) { pilotNickname = wn2.getTextContent(); } else if (wn2.getNodeName().equalsIgnoreCase("type")) { type = Integer.parseInt(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("skill")) { Skill s = Skill.generateInstanceFromXML(wn2); if (null != s && null != s.getType()) { retVal.skills.put(s.getType().getName(), s); } } else if (wn2.getNodeName().equalsIgnoreCase("techUnitIds")) { NodeList nl2 = wn2.getChildNodes(); for (int y = 0; y < nl2.getLength(); y++) { Node wn3 = nl2.item(y); // If it's not an element node, we ignore it. if (wn3.getNodeType() != Node.ELEMENT_NODE) { continue; } if (!wn3.getNodeName().equalsIgnoreCase("id")) { // Error condition of sorts! // Errr, what should we do here? MekHQ.logMessage( "Unknown node type not loaded in techUnitIds nodes: " + wn3.getNodeName()); continue; } retVal.techUnitIds.add(UUID.fromString(wn3.getTextContent())); } } else if (wn2.getNodeName().equalsIgnoreCase("personnelLog")) { NodeList nl2 = wn2.getChildNodes(); for (int y = 0; y < nl2.getLength(); y++) { Node wn3 = nl2.item(y); // If it's not an element node, we ignore it. if (wn3.getNodeType() != Node.ELEMENT_NODE) { continue; } if (!wn3.getNodeName().equalsIgnoreCase("logEntry")) { // Error condition of sorts! // Errr, what should we do here? MekHQ.logMessage( "Unknown node type not loaded in personnel log nodes: " + wn3.getNodeName()); continue; } retVal.addLogEntry(LogEntry.generateInstanceFromXML(wn3)); } } else if (wn2.getNodeName().equalsIgnoreCase("injuries")) { NodeList nl2 = wn2.getChildNodes(); for (int y = 0; y < nl2.getLength(); y++) { Node wn3 = nl2.item(y); // If it's not an element node, we ignore it. if (wn3.getNodeType() != Node.ELEMENT_NODE) { continue; } if (!wn3.getNodeName().equalsIgnoreCase("injury")) { // Error condition of sorts! // Errr, what should we do here? MekHQ.logMessage("Unknown node type not loaded in injury nodes: " + wn3.getNodeName()); continue; } retVal.injuries.add(Injury.generateInstanceFromXML(wn3)); } DateTime now = new DateTime(c.getCalendar()); retVal.injuries.stream().filter(inj -> (null == inj.getStart())) .forEach(inj -> inj.setStart(now.minusDays(inj.getOriginalTime() - inj.getTime()))); } else if (wn2.getNodeName().equalsIgnoreCase("founder")) { retVal.founder = Boolean.parseBoolean(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("originalUnitWeight")) { retVal.originalUnitWeight = Integer.parseInt(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("originalUnitTech")) { retVal.originalUnitTech = Integer.parseInt(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("originalUnitId")) { retVal.originalUnitId = UUID.fromString(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("extraData")) { retVal.extraData = ExtraData.createFromXml(wn2); } } if (version.getMajorVersion() == 0 && version.getMinorVersion() < 2 && version.getSnapshot() < 13) { if (retVal.primaryRole > T_INFANTRY) { retVal.primaryRole += 4; } if (retVal.secondaryRole > T_INFANTRY) { retVal.secondaryRole += 4; } } if (version.getMajorVersion() == 0 && version.getMinorVersion() < 3 && version.getMinorVersion() > 1) { //adjust for conventional fighter pilots if (retVal.primaryRole >= T_CONV_PILOT) { retVal.primaryRole += 1; } if (retVal.secondaryRole >= T_CONV_PILOT) { retVal.secondaryRole += 1; } } if (version.getMajorVersion() == 0 && version.getMinorVersion() == 3 && version.getSnapshot() < 1) { //adjust for conventional fighter pilots if (retVal.primaryRole == T_CONV_PILOT && retVal.hasSkill(SkillType.S_PILOT_SPACE) && !retVal.hasSkill(SkillType.S_PILOT_JET)) { retVal.primaryRole += 1; } if (retVal.secondaryRole == T_CONV_PILOT && retVal.hasSkill(SkillType.S_PILOT_SPACE) && !retVal.hasSkill(SkillType.S_PILOT_JET)) { retVal.secondaryRole += 1; } if (retVal.primaryRole == T_AERO_PILOT && !retVal.hasSkill(SkillType.S_PILOT_SPACE) && retVal.hasSkill(SkillType.S_PILOT_JET)) { retVal.primaryRole += 8; } if (retVal.secondaryRole == T_AERO_PILOT && !retVal.hasSkill(SkillType.S_PILOT_SPACE) && retVal.hasSkill(SkillType.S_PILOT_JET)) { retVal.secondaryRole += 8; } } if ((null != advantages) && (advantages.trim().length() > 0)) { StringTokenizer st = new StringTokenizer(advantages, "::"); while (st.hasMoreTokens()) { String adv = st.nextToken(); String advName = Crew.parseAdvantageName(adv); Object value = Crew.parseAdvantageValue(adv); try { retVal.getOptions().getOption(advName).setValue(value); } catch (Exception e) { MekHQ.logMessage("Error restoring advantage: " + adv); } } } if ((null != edge) && (edge.trim().length() > 0)) { StringTokenizer st = new StringTokenizer(edge, "::"); while (st.hasMoreTokens()) { String adv = st.nextToken(); String advName = Crew.parseAdvantageName(adv); Object value = Crew.parseAdvantageValue(adv); try { retVal.getOptions().getOption(advName).setValue(value); } catch (Exception e) { MekHQ.logMessage("Error restoring edge: " + adv); } } } if ((null != implants) && (implants.trim().length() > 0)) { StringTokenizer st = new StringTokenizer(implants, "::"); while (st.hasMoreTokens()) { String adv = st.nextToken(); String advName = Crew.parseAdvantageName(adv); Object value = Crew.parseAdvantageValue(adv); try { retVal.getOptions().getOption(advName).setValue(value); } catch (Exception e) { MekHQ.logMessage("Error restoring implants: " + adv); } } } //check to see if we are dealing with a PilotPerson from 0.1.8 or earlier if (pilotGunnery != -1) { switch (type) { case 0: retVal.addSkill(SkillType.S_GUN_MECH, 7 - pilotGunnery, 0); retVal.addSkill(SkillType.S_PILOT_MECH, 8 - pilotPiloting, 0); retVal.primaryRole = T_MECHWARRIOR; break; case 1: retVal.addSkill(SkillType.S_GUN_VEE, 7 - pilotGunnery, 0); retVal.addSkill(SkillType.S_PILOT_GVEE, 8 - pilotPiloting, 0); retVal.primaryRole = T_GVEE_DRIVER; break; case 2: retVal.addSkill(SkillType.S_GUN_AERO, 7 - pilotGunnery, 0); retVal.addSkill(SkillType.S_PILOT_AERO, 8 - pilotPiloting, 0); retVal.primaryRole = T_AERO_PILOT; break; case 4: retVal.addSkill(SkillType.S_GUN_BA, 7 - pilotGunnery, 0); retVal.addSkill(SkillType.S_ANTI_MECH, 8 - pilotPiloting, 0); retVal.primaryRole = T_BA; break; } retVal.addSkill(SkillType.S_TACTICS, pilotCommandBonus, 0); } if (null != pilotName) { retVal.setName(pilotName); } if (null != pilotNickname) { retVal.setCallsign(pilotNickname); } } catch (Exception ex) { // Errrr, apparently either the class name was invalid... // Or the listed name doesn't exist. // Doh! MekHQ.logError(ex); } if (retVal.id == null) { MekHQ.logMessage("ID not pre-defined; generating person's ID.", 5); retVal.id = UUID.randomUUID(); } // Prisoner and Bondsman updating if (retVal.prisonerStatus != PRISONER_NOT && retVal.rank == 0) { if (retVal.prisonerStatus == PRISONER_BONDSMAN) { retVal.setRankNumeric(Ranks.RANK_BONDSMAN); } else { retVal.setRankNumeric(Ranks.RANK_PRISONER); } } return retVal; }
From source file:net.link.util.test.pkix.PkiTestUtils.java
License:Open Source License
public static X509Certificate generateTestSelfSignedCert(@Nullable URI ocspUri) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, IOException, OperatorCreationException, CertificateException { KeyPair keyPair = generateKeyPair(); DateTime now = new DateTime(); DateTime notBefore = now.minusDays(1); DateTime notAfter = now.plusDays(1); return generateCertificate(keyPair.getPublic(), "CN=Test", keyPair.getPrivate(), null, notBefore, notAfter, null, true, true, false, ocspUri); }
From source file:net.longfalcon.newsj.Backfill.java
License:Open Source License
public long dayToPost(NewsClient nntpClient, Group group, int days, boolean debug) throws IOException { if (debug) {/*from www . j a va 2s . co m*/ _log.info(String.format("dayToPost finding post for %s %s days back.", group.getName(), days)); } NewsgroupInfo newsgroupInfo = new NewsgroupInfo(); boolean exists = nntpClient.selectNewsgroup(group.getName(), newsgroupInfo); if (!exists) { System.out.println("Could not select group (bad name?): " + group.getName()); _log.error("Returning from dayToPost"); return 0; } DateTime goalDate = new DateTime(); goalDate = goalDate.minusDays(days); long firstArticle = newsgroupInfo.getFirstArticleLong(); long lastArticle = newsgroupInfo.getLastArticleLong(); long totalArticles = lastArticle - firstArticle; long upperBound = lastArticle; long lowerBound = firstArticle; if (debug) { _log.info(String.format("Total Articles: %d\n Upper: %d\n Lower: %d\n Goal: %s", totalArticles, upperBound, lowerBound, goalDate.toString(_dateTimeFormatter))); } DateTime firstDate = postDate(nntpClient, firstArticle, true); int offset = 1; while (firstDate == null) { firstArticle = firstArticle + (offset * 4); _log.info("Unable to locate canonical first post, trying " + firstArticle); firstDate = postDate(nntpClient, firstArticle, true); offset++; if (offset == 3) { newsgroupInfo = new NewsgroupInfo(); exists = nntpClient.selectNewsgroup(group.getName(), newsgroupInfo); if (!exists) { System.out.println("Could not select group (bad name?): " + group.getName()); _log.error("Returning from dayToPost"); return 0; } firstArticle = newsgroupInfo.getFirstArticleLong(); } } DateTime lastDate = postDate(nntpClient, lastArticle, true); if (goalDate.isBefore(firstDate)) { _log.warn("WARNING: Backfill target of " + days + " day(s) is older than the first article stored on your news server."); _log.warn("Starting from the first available article (" + _dateTimeFormatter.print(firstArticle) + " or " + daysOld(firstDate) + " days)."); return firstArticle; } else if (goalDate.isAfter(lastDate)) { _log.warn("ERROR: Backfill target of " + days + " day(s) is newer than the last article stored on your news server."); _log.warn("To backfill this group you need to set Backfill Days to at least " + daysOld(lastDate) + 1 + " days (" + _dateTimeFormatter.print(lastDate.minusDays(1)) + ")"); return 0; } if (debug) { _log.info(String.format("DEBUG: Searching for postdate \nGoaldate: %s\nFirstdate: %s\nLastdate: %s", _dateTimeFormatter.print(goalDate), _dateTimeFormatter.print(firstDate), _dateTimeFormatter.print(lastDate))); } long interval = Math.round(Math.floor((upperBound - lowerBound) * 0.5d)); DateTime dateOfNextOne; if (debug) { _log.info(String.format("Start: %s\nEnd: %s\nInterval: %s", firstArticle, lastArticle, interval)); } dateOfNextOne = lastDate; while (daysOld(dateOfNextOne) < days) { DateTime tempDate = postDate(nntpClient, (upperBound - interval), true); while (tempDate.isAfter(goalDate)) { upperBound = upperBound - interval; if (debug) { _log.info(String.format("New upperbound (%s) is %s days old.", upperBound, daysOld(tempDate))); } tempDate = postDate(nntpClient, (upperBound - interval), true); } interval = Math.round(Math.ceil(interval / 2d)); if (debug) { _log.info("Set interval to " + interval + " articles."); } if (interval == 1) { break; } dateOfNextOne = postDate(nntpClient, upperBound - 1, true); while (dateOfNextOne == null) { dateOfNextOne = postDate(nntpClient, upperBound--, true); } } _log.info("Determined to be article " + upperBound + " which is " + daysOld(dateOfNextOne) + " days old (" + _dateTimeFormatter.print(dateOfNextOne) + ")"); return upperBound; }
From source file:net.longfalcon.newsj.Releases.java
License:Open Source License
public void processReleases() { String startDateString = DateUtil.displayDateFormatter.print(System.currentTimeMillis()); _log.info(String.format("Starting release update process (%s)", startDateString)); // get site config TODO: use config service Site site = siteDAO.getDefaultSite(); int retcount = 0; Directory nzbBaseDir = fileSystemService.getDirectory("/nzbs"); checkRegexesUptoDate(site.getLatestRegexUrl(), site.getLatestRegexRevision()); // Stage 0//from w w w . j a v a2 s . c o m // this is a hack - tx is not working ATM TransactionStatus transaction = transactionManager .getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED)); // // Get all regexes for all groups which are to be applied to new binaries // in order of how they should be applied // List<ReleaseRegex> releaseRegexList = releaseRegexDAO.getRegexes(true, "-1", false); for (ReleaseRegex releaseRegex : releaseRegexList) { String releaseRegexGroupName = releaseRegex.getGroupName(); _log.info(String.format("Applying regex %d for group %s", releaseRegex.getId(), ValidatorUtil.isNull(releaseRegexGroupName) ? "all" : releaseRegexGroupName)); // compile the regex early, to test them String regex = releaseRegex.getRegex(); Pattern pattern = Pattern.compile(fixRegex(regex), Pattern.CASE_INSENSITIVE); // remove '/' and '/i' HashSet<Long> groupMatch = new LinkedHashSet<>(); // // Groups ending in * need to be like matched when getting out binaries for groups and children // Matcher matcher = _wildcardPattern.matcher(releaseRegexGroupName); if (matcher.matches()) { releaseRegexGroupName = releaseRegexGroupName.substring(0, releaseRegexGroupName.length() - 1); List<Group> groups = groupDAO.findGroupsByName(releaseRegexGroupName); for (Group group : groups) { groupMatch.add(group.getId()); } } else if (!ValidatorUtil.isNull(releaseRegexGroupName)) { Group group = groupDAO.getGroupByName(releaseRegexGroupName); if (group != null) { groupMatch.add(group.getId()); } } List<Binary> binaries = new ArrayList<>(); if (groupMatch.size() > 0) { // Get out all binaries of STAGE0 for current group binaries = binaryDAO.findByGroupIdsAndProcStat(groupMatch, Defaults.PROCSTAT_NEW); } Map<String, String> arrNoPartBinaries = new LinkedHashMap<>(); DateTime fiveHoursAgo = DateTime.now().minusHours(5); // this for loop should probably be a single transaction for (Binary binary : binaries) { String testMessage = "Test run - Binary Name " + binary.getName(); Matcher groupRegexMatcher = pattern.matcher(binary.getName()); if (groupRegexMatcher.find()) { String reqIdGroup = null; try { reqIdGroup = groupRegexMatcher.group("reqid"); } catch (IllegalArgumentException e) { _log.debug(e.toString()); } String partsGroup = null; try { partsGroup = groupRegexMatcher.group("parts"); } catch (IllegalArgumentException e) { _log.debug(e.toString()); } String nameGroup = null; try { nameGroup = groupRegexMatcher.group("name"); } catch (Exception e) { _log.debug(e.toString()); } _log.debug(testMessage + " matches with: \n reqId = " + reqIdGroup + " parts = " + partsGroup + " and name = " + nameGroup); if ((ValidatorUtil.isNotNull(reqIdGroup) && ValidatorUtil.isNumeric(reqIdGroup)) && ValidatorUtil.isNull(nameGroup)) { nameGroup = reqIdGroup; } if (ValidatorUtil.isNull(nameGroup)) { _log.warn(String.format( "regex applied which didnt return right number of capture groups - %s", regex)); _log.warn(String.format("regex matched: reqId = %s parts = %s and name = %s", reqIdGroup, partsGroup, nameGroup)); continue; } // If theres no number of files data in the subject, put it into a release if it was posted to usenet longer than five hours ago. if ((ValidatorUtil.isNull(partsGroup) && fiveHoursAgo.isAfter(binary.getDate().getTime()))) { // // Take a copy of the name of this no-part release found. This can be used // next time round the loop to find parts of this set, but which have not yet reached 3 hours. // arrNoPartBinaries.put(nameGroup, "1"); partsGroup = "01/01"; } if (ValidatorUtil.isNotNull(nameGroup) && ValidatorUtil.isNotNull(partsGroup)) { if (partsGroup.indexOf('/') == -1) { partsGroup = partsGroup.replaceFirst("(-)|(~)|(\\sof\\s)", "/"); // replace weird parts delimiters } Integer regexCategoryId = releaseRegex.getCategoryId(); Integer reqId = null; if (ValidatorUtil.isNotNull(reqIdGroup) && ValidatorUtil.isNumeric(reqIdGroup)) { reqId = Integer.parseInt(reqIdGroup); } //check if post is repost Pattern repostPattern = Pattern.compile("(repost\\d?|re\\-?up)", Pattern.CASE_INSENSITIVE); Matcher binaryNameRepostMatcher = repostPattern.matcher(binary.getName()); if (binaryNameRepostMatcher.find() && !nameGroup.toLowerCase().matches("^[\\s\\S]+(repost\\d?|re\\-?up)")) { nameGroup = nameGroup + (" " + binaryNameRepostMatcher.group(1)); } String partsStrings[] = partsGroup.split("/"); int relpart = Integer.parseInt(partsStrings[0]); int relTotalPart = Integer.parseInt(partsStrings[1]); binary.setRelName(nameGroup.replace("_", " ")); binary.setRelPart(relpart); binary.setRelTotalPart(relTotalPart); binary.setProcStat(Defaults.PROCSTAT_TITLEMATCHED); binary.setCategoryId(regexCategoryId); binary.setRegexId(releaseRegex.getId()); binary.setReqId(reqId); binaryDAO.updateBinary(binary); } } } } transactionManager.commit(transaction); // this is a hack - tx is not working ATM transaction = transactionManager .getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED)); // // Move all binaries from releases which have the correct number of files on to the next stage. // _log.info("Stage 2"); List<MatchedReleaseQuery> matchedReleaseQueries = binaryDAO .findBinariesByProcStatAndTotalParts(Defaults.PROCSTAT_TITLEMATCHED); matchedReleaseQueries = combineMatchedQueries(matchedReleaseQueries); int siteMinFilestoFormRelease = site.getMinFilesToFormRelease(); for (MatchedReleaseQuery matchedReleaseQuery : matchedReleaseQueries) { retcount++; // // Less than the site permitted number of files in a release. Dont discard it, as it may // be part of a set being uploaded. // int minFiles = siteMinFilestoFormRelease; String releaseName = matchedReleaseQuery.getReleaseName(); long matchedReleaseQueryGroup = matchedReleaseQuery.getGroup(); Long matchedReleaseQueryNumberOfBinaries = matchedReleaseQuery.getNumberOfBinaries(); int matchecReleaseTotalParts = matchedReleaseQuery.getReleaseTotalParts(); String fromName = matchedReleaseQuery.getFromName(); Integer reqId = matchedReleaseQuery.getReqId(); Group group = groupDAO.findGroupByGroupId(matchedReleaseQueryGroup); if (group != null && group.getMinFilesToFormRelease() != null) { minFiles = group.getMinFilesToFormRelease(); } if (matchedReleaseQueryNumberOfBinaries < minFiles) { _log.warn(String.format("Number of files in release %s less than site/group setting (%s/%s)", releaseName, matchedReleaseQueryNumberOfBinaries, minFiles)); binaryDAO.updateBinaryIncrementProcAttempts(releaseName, Defaults.PROCSTAT_TITLEMATCHED, matchedReleaseQueryGroup, fromName); } else if (matchedReleaseQueryNumberOfBinaries >= matchecReleaseTotalParts) { // Check that the binary is complete List<Binary> releaseBinaryList = binaryDAO.findBinariesByReleaseNameProcStatGroupIdFromName( releaseName, Defaults.PROCSTAT_TITLEMATCHED, matchedReleaseQueryGroup, fromName); boolean incomplete = false; for (Binary binary : releaseBinaryList) { long partsCount = partDAO.countPartsByBinaryId(binary.getId()); if (partsCount < binary.getTotalParts()) { float percentComplete = ((float) partsCount / (float) binary.getTotalParts()) * 100; _log.warn(String.format("binary %s from %s has missing parts = %s/%s (%s%% complete)", binary.getId(), releaseName, partsCount, binary.getTotalParts(), percentComplete)); // Allow to binary to release if posted to usenet longer than four hours ago and we still don't have all the parts DateTime fourHoursAgo = DateTime.now().minusHours(4); if (fourHoursAgo.isAfter(new DateTime(binary.getDate()))) { _log.info("allowing incomplete binary " + binary.getId()); } else { incomplete = true; } } } if (incomplete) { _log.warn(String.format("Incorrect number of parts %s-%s-%s", releaseName, matchedReleaseQueryNumberOfBinaries, matchecReleaseTotalParts)); binaryDAO.updateBinaryIncrementProcAttempts(releaseName, Defaults.PROCSTAT_TITLEMATCHED, matchedReleaseQueryGroup, fromName); } // // Right number of files, but see if the binary is a allfilled/reqid post, in which case it needs its name looked up // TODO: Does this even work anymore? else if (ValidatorUtil.isNotNull(site.getReqIdUrl()) && ValidatorUtil.isNotNull(reqId)) { // // Try and get the name using the group // _log.info("Looking up " + reqId + " in " + group.getName() + "..."); String newTitle = getReleaseNameForReqId(site.getReqIdUrl(), group, reqId, true); // // if the feed/group wasnt supported by the scraper, then just use the release name as the title. // if (ValidatorUtil.isNull(newTitle) || newTitle.equals("no feed")) { newTitle = releaseName; _log.warn("Group not supported"); } // // Valid release with right number of files and title now, so move it on // if (ValidatorUtil.isNotNull(newTitle)) { binaryDAO.updateBinaryNameAndStatus(newTitle, Defaults.PROCSTAT_READYTORELEASE, releaseName, Defaults.PROCSTAT_TITLEMATCHED, matchedReleaseQueryGroup, fromName); } else { // // Item not found, if the binary was added to the index yages ago, then give up. // Timestamp timestamp = binaryDAO.findMaxDateAddedBinaryByReleaseNameProcStatGroupIdFromName( releaseName, Defaults.PROCSTAT_TITLEMATCHED, matchedReleaseQueryGroup, fromName); DateTime maxAddedDate = new DateTime(timestamp); DateTime twoDaysAgo = DateTime.now().minusDays(2); if (maxAddedDate.isBefore(twoDaysAgo)) { binaryDAO.updateBinaryNameAndStatus(releaseName, Defaults.PROCSTAT_NOREQIDNAMELOOKUPFOUND, releaseName, Defaults.PROCSTAT_TITLEMATCHED, matchedReleaseQueryGroup, fromName); _log.warn("Not found in 48 hours"); } } } else { binaryDAO.updateBinaryNameAndStatus(releaseName, Defaults.PROCSTAT_READYTORELEASE, releaseName, Defaults.PROCSTAT_TITLEMATCHED, matchedReleaseQueryGroup, fromName); } } else { // // Theres less than the expected number of files, so update the attempts and move on. // _log.info(String.format("Incorrect number of files for %s (%d/%d)", releaseName, matchedReleaseQueryNumberOfBinaries, matchecReleaseTotalParts)); binaryDAO.updateBinaryIncrementProcAttempts(releaseName, Defaults.PROCSTAT_TITLEMATCHED, matchedReleaseQueryGroup, fromName); } if (retcount % 10 == 0) { _log.info(String.format("-processed %d binaries stage two", retcount)); } } transactionManager.commit(transaction); retcount = 0; int nfoCount = 0; // this is a hack - tx is not working ATM transaction = transactionManager .getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED)); // // Get out all distinct relname, group from binaries of STAGE2 // _log.info("Stage 3"); List<MatchedReleaseQuery> readyReleaseQueries = binaryDAO .findBinariesByProcStatAndTotalParts(Defaults.PROCSTAT_READYTORELEASE); readyReleaseQueries = combineMatchedQueries(readyReleaseQueries); for (MatchedReleaseQuery readyReleaseQuery : readyReleaseQueries) { retcount++; String releaseName = readyReleaseQuery.getReleaseName(); int numParts = readyReleaseQuery.getReleaseTotalParts(); long binaryCount = readyReleaseQuery.getNumberOfBinaries(); long groupId = readyReleaseQuery.getGroup(); // // Get the last post date and the poster name from the binary // String fromName = readyReleaseQuery.getFromName(); Timestamp timestamp = binaryDAO.findMaxDateAddedBinaryByReleaseNameProcStatGroupIdFromName(releaseName, Defaults.PROCSTAT_READYTORELEASE, groupId, fromName); DateTime addedDate = new DateTime(timestamp); // // Get all releases with the same name with a usenet posted date in a +1-1 date range. // Date oneDayBefore = addedDate.minusDays(1).toDate(); Date oneDayAfter = addedDate.plusDays(1).toDate(); List<Release> relDupes = releaseDAO.findReleasesByNameAndDateRange(releaseName, oneDayBefore, oneDayAfter); if (!relDupes.isEmpty()) { binaryDAO.updateBinaryNameAndStatus(releaseName, Defaults.PROCSTAT_DUPLICATE, releaseName, Defaults.PROCSTAT_READYTORELEASE, groupId, fromName); continue; } // // Get total size of this release // Done in a big OR statement, not an IN as the mysql binaryID index on parts table // was not being used. // // SM: TODO this should be revisited, using hb mappings long totalSize = 0; int regexAppliedCategoryId = 0; long regexIdUsed = 0; int reqIdUsed = 0; int relTotalParts = 0; float relCompletion; List<Binary> binariesForSize = binaryDAO.findBinariesByReleaseNameProcStatGroupIdFromName(releaseName, Defaults.PROCSTAT_READYTORELEASE, groupId, fromName); long relParts = 0; for (Binary binary : binariesForSize) { if (ValidatorUtil.isNotNull(binary.getCategoryId()) && regexAppliedCategoryId == 0) { regexAppliedCategoryId = binary.getCategoryId(); } if (ValidatorUtil.isNotNull(binary.getRegexId()) && regexIdUsed == 0) { regexIdUsed = binary.getRegexId(); } if (ValidatorUtil.isNotNull(binary.getReqId()) && reqIdUsed == 0) { reqIdUsed = binary.getReqId(); } relTotalParts += binary.getTotalParts(); relParts += partDAO.countPartsByBinaryId(binary.getId()); totalSize += partDAO.sumPartsSizeByBinaryId(binary.getId()); } relCompletion = ((float) relParts / (float) relTotalParts) * 100f; // // Insert the release // String releaseGuid = UUID.randomUUID().toString(); int categoryId; Category category = null; Long regexId; Integer reqId; if (regexAppliedCategoryId == 0) { categoryId = categoryService.determineCategory(groupId, releaseName); } else { categoryId = regexAppliedCategoryId; } if (categoryId > 0) { category = categoryService.getCategory(categoryId); } if (regexIdUsed == 0) { regexId = null; } else { regexId = regexIdUsed; } if (reqIdUsed == 0) { reqId = null; } else { reqId = reqIdUsed; } //Clean release name of '#', '@', '$', '%', '^', '', '', '', '' String cleanReleaseName = releaseName.replaceAll("[^A-Za-z0-9-_\\ \\.]+", ""); Release release = new Release(); release.setName(cleanReleaseName); release.setSearchName(cleanReleaseName); release.setTotalpart(numParts); release.setGroupId(groupId); release.setAddDate(new Date()); release.setGuid(releaseGuid); release.setCategory(category); release.setRegexId(regexId); release.setRageId((long) -1); release.setPostDate(addedDate.toDate()); release.setFromName(fromName); release.setSize(totalSize); release.setReqId(reqId); release.setPasswordStatus(site.getCheckPasswordedRar() == 1 ? -1 : 0); // magic constants release.setCompletion(relCompletion); releaseDAO.updateRelease(release); long releaseId = release.getId(); _log.info("Added release " + cleanReleaseName); // // Tag every binary for this release with its parent release id // remove the release name from the binary as its no longer required // binaryDAO.updateBinaryNameStatusReleaseID("", Defaults.PROCSTAT_RELEASED, releaseId, releaseName, Defaults.PROCSTAT_READYTORELEASE, groupId, fromName); // // Find an .nfo in the release // ReleaseNfo releaseNfo = nfo.determineReleaseNfo(release); if (releaseNfo != null) { nfo.addReleaseNfo(releaseNfo); nfoCount++; } // // Write the nzb to disk // nzb.writeNZBforReleaseId(release, nzbBaseDir, true); if (retcount % 5 == 0) { _log.info("-processed " + retcount + " releases stage three"); } } _log.info("Found " + nfoCount + " nfos in " + retcount + " releases"); // // Process nfo files // if (site.getLookupNfo() != 1) { _log.info("Site config (site.lookupnfo) prevented retrieving nfos"); } else { nfo.processNfoFiles(site.getLookupImdb(), site.getLookupTvRage()); } // // Lookup imdb if enabled // if (site.getLookupImdb() == 1) { movieService.processMovieReleases(); } // // Lookup music if enabled // if (site.getLookupMusic() == 1) { musicService.processMusicReleases(); } // // Lookup games if enabled // if (site.getLookupGames() == 1) { gameService.processConsoleReleases(); } // // Check for passworded releases // if (site.getCheckPasswordedRar() != 1) { _log.info("Site config (site.checkpasswordedrar) prevented checking releases are passworded"); } else { processPasswordedReleases(true); } // // Process all TV related releases which will assign their series/episode/rage data // tvRageService.processTvReleases(site.getLookupTvRage() == 1); // // Get the current datetime again, as using now() in the housekeeping queries prevents the index being used. // DateTime now = new DateTime(); // // Tidy away any binaries which have been attempted to be grouped into // a release more than x times (SM: or is it days?) // int attemtpGroupBinDays = site.getAttemtpGroupBinDays(); _log.info(String.format("Tidying away binaries which cant be grouped after %s days", attemtpGroupBinDays)); DateTime maxGroupBinDays = now.minusDays(attemtpGroupBinDays); binaryDAO.updateProcStatByProcStatAndDate(Defaults.PROCSTAT_WRONGPARTS, Defaults.PROCSTAT_NEW, maxGroupBinDays.toDate()); // // Delete any parts and binaries which are older than the site's retention days // int maxRetentionDays = site.getRawRetentionDays(); DateTime maxRetentionDate = now.minusDays(maxRetentionDays); _log.info(String.format("Deleting parts which are older than %d days", maxRetentionDays)); partDAO.deletePartByDate(maxRetentionDate.toDate()); _log.info(String.format("Deleting binaries which are older than %d days", maxRetentionDays)); binaryDAO.deleteBinaryByDate(maxRetentionDate.toDate()); // // Delete any releases which are older than site's release retention days // int releaseretentiondays = site.getReleaseRetentionDays(); if (releaseretentiondays != 0) { _log.info("Determining any releases past retention to be deleted."); DateTime maxReleaseRetentionDate = DateTime.now().minusDays(releaseretentiondays); List<Release> releasesToDelete = releaseDAO.findReleasesBeforeDate(maxReleaseRetentionDate.toDate()); for (Iterator<Release> iterator = releasesToDelete.iterator(); iterator.hasNext();) { Release release = iterator.next(); releaseDAO.deleteRelease(release); } } transaction.flush(); // may be unneeded transactionManager.commit(transaction); _log.info(String.format("Processed %d releases", retcount)); if (!transaction.isCompleted()) { throw new IllegalStateException("Transaction is not completed or rolled back."); } //return retcount; }
From source file:net.longfalcon.newsj.service.UserInviteService.java
License:Open Source License
@Transactional public UserInvite getInvite(String inviteToken) { // TODO: move this to a daily job DateTime expireDate = new DateTime(); expireDate.minusDays(UserService.DEFAULT_INVITE_EXPIRY_DAYS); userInviteDAO.cleanOldInvites(expireDate.toDate()); return userInviteDAO.getInviteByGuid(inviteToken); }
From source file:net.lshift.diffa.config.DailyPeriodUnit.java
License:Apache License
@Override protected DateTime subtractOneUnit(DateTime from) { return from.minusDays(1); }