Search in sources :

Example 66 with EnumMap

use of java.util.EnumMap in project BibleMultiConverter by schierlm.

the class OnLineBible method doExport.

@Override
public void doExport(Bible bible, String... exportArgs) throws Exception {
    String outFile = exportArgs[0], namesFile = null;
    ;
    boolean includeStrongs = false;
    if (exportArgs.length > 1) {
        if (exportArgs[1].equals("IncludeStrongs")) {
            includeStrongs = true;
        } else {
            namesFile = exportArgs[1];
        }
        if (exportArgs.length > 2 && exportArgs[2].equals("IncludeStrongs")) {
            includeStrongs = true;
        }
    }
    Set<BookID> supportedBooks = EnumSet.noneOf(BookID.class);
    for (BookMeta bm : BOOK_META) {
        supportedBooks.add(bm.id);
    }
    Map<BookID, Book> bookMap = new EnumMap<>(BookID.class);
    for (Book book : bible.getBooks()) {
        if (supportedBooks.contains(book.getId()))
            bookMap.put(book.getId(), book);
        else
            System.out.println("WARNING: Skipping book " + book.getAbbr());
    }
    if (namesFile != null) {
        try (BufferedWriter bw = new BufferedWriter(new FileWriter(namesFile))) {
            for (BookMeta bm : BOOK_META) {
                Book bk = bookMap.get(bm.id);
                if (bk != null) {
                    bw.write(bk.getShortName() + " " + bm.abbr);
                    bw.newLine();
                }
            }
        }
    }
    try (BufferedWriter bw = new BufferedWriter(new FileWriter(outFile))) {
        for (BookMeta bm : BOOK_META) {
            String prefix = "";
            if (bm.id == BookID.BOOK_Matt && includeStrongs) {
                prefix = "0 ";
            }
            Book bk = bookMap.remove(bm.id);
            int[] verseCount = StandardVersification.KJV.getVerseCount(bm.id);
            for (int i = 0; i < verseCount.length; i++) {
                Chapter ch = bk != null && i < bk.getChapters().size() ? bk.getChapters().get(i) : null;
                int maxVerse = verseCount[i];
                BitSet allowedNumbers = new BitSet(maxVerse + 1);
                allowedNumbers.set(1, maxVerse + 1);
                List<VirtualVerse> vvs = ch == null ? null : ch.createVirtualVerses(null);
                for (int vnum = 1; vnum <= verseCount[i]; vnum++) {
                    bw.write("$$$ " + bm.abbr + " " + (i + 1) + ":" + vnum + " ");
                    bw.newLine();
                    StringBuilder text = new StringBuilder(prefix);
                    if (vvs != null) {
                        for (VirtualVerse vv : vvs) {
                            if (vv.getNumber() == vnum) {
                                for (Headline h : vv.getHeadlines()) {
                                    text.append(" {\\$");
                                    h.accept(new OnLineBibleVisitor(text, includeStrongs));
                                    text.append("\\$} ");
                                }
                                for (Verse v : vv.getVerses()) {
                                    if (!v.getNumber().equals("" + vv.getNumber())) {
                                        text.append("\\\\(" + v.getNumber() + ")\\\\ ");
                                    }
                                    v.accept(new OnLineBibleVisitor(text, includeStrongs));
                                }
                            }
                        }
                    }
                    if (text.length() > 0) {
                        bw.write(text.toString().replaceAll("  +", " "));
                        bw.newLine();
                    }
                    prefix = "";
                }
            }
        }
    }
    if (!bookMap.isEmpty())
        throw new IllegalStateException("Remaining books: " + bookMap.keySet());
}
Also used : VirtualVerse(biblemulticonverter.data.VirtualVerse) FileWriter(java.io.FileWriter) Chapter(biblemulticonverter.data.Chapter) BitSet(java.util.BitSet) BufferedWriter(java.io.BufferedWriter) BookID(biblemulticonverter.data.BookID) Book(biblemulticonverter.data.Book) Headline(biblemulticonverter.data.FormattedText.Headline) EnumMap(java.util.EnumMap) VirtualVerse(biblemulticonverter.data.VirtualVerse) Verse(biblemulticonverter.data.Verse)

Example 67 with EnumMap

use of java.util.EnumMap in project cu-kfs by CU-CommunityApps.

the class TaxProcessingDaoJdbc method doTaxProcessing.

@Override
public void doTaxProcessing(String taxType, int reportYear, java.sql.Date startDate, java.sql.Date endDate, boolean vendorForeign, java.util.Date processingStartDate) {
    List<EnumMap<TaxStatType, Integer>> stats;
    TaxProcessingService taxProcessingService = SpringContext.getBean(TaxProcessingService.class);
    TaxOutputDefinition tempDefinition;
    if (CUTaxConstants.TAX_TYPE_1099.equals(taxType)) {
        // Perform 1099 tax processing.
        Transaction1099Summary summary = new Transaction1099Summary(reportYear, startDate, endDate, vendorForeign, taxProcessingService.getDataDefinition(CUTaxKeyConstants.TAX_TABLE_1099_PREFIX, reportYear).getDataRowsAsMap());
        // Delete any previously-generated rows for the current year and tax type.
        getJdbcTemplate().update(TaxSqlUtils.getTransactionDetailDeleteSql(taxType, summary.transactionDetailRow), Integer.valueOf(reportYear));
        // Create new transaction rows, retrieving summary statistics as needed.
        stats = createTransactionRows(summary, Arrays.<Class<? extends TransactionRowBuilder<Transaction1099Summary>>>asList(TransactionRowPdpBuilder.For1099.class, TransactionRowDvBuilder.For1099.class, TransactionRowPRNCBuilder.For1099.class));
        // Create 1099 output data from the transaction rows and send it to the file(s), retrieving summary statistics as needed.
        tempDefinition = taxProcessingService.getOutputDefinition(CUTaxKeyConstants.TAX_FORMAT_1099_PREFIX, reportYear);
        stats.add(processTransactionRows(processingStartDate, summary, TransactionRow1099Processor.class, tempDefinition));
        // Print the transaction row data to another file.
        tempDefinition = taxProcessingService.getOutputDefinition(CUTaxKeyConstants.TAX_FORMAT_1099_PREFIX + CUTaxKeyConstants.TAX_FORMAT_SUMMARY_SUFFIX, reportYear);
        processTransactionRows(processingStartDate, summary, TransactionRowPrintProcessor.For1099.class, tempDefinition);
    } else if (CUTaxConstants.TAX_TYPE_1042S.equals(taxType)) {
        // Perform 1042S tax processing.
        Transaction1042SSummary summary = new Transaction1042SSummary(reportYear, startDate, endDate, vendorForeign, taxProcessingService.getDataDefinition(CUTaxKeyConstants.TAX_TABLE_1042S_PREFIX, reportYear).getDataRowsAsMap());
        // Delete any previously-generated rows for the current year and tax type.
        getJdbcTemplate().update(TaxSqlUtils.getTransactionDetailDeleteSql(taxType, summary.transactionDetailRow), Integer.valueOf(reportYear));
        // Create new transaction rows, retrieving summary statistics as needed.
        stats = createTransactionRows(summary, Arrays.<Class<? extends TransactionRowBuilder<Transaction1042SSummary>>>asList(TransactionRowPdpBuilder.For1042S.class, TransactionRowDvBuilder.For1042S.class, TransactionRowPRNCBuilder.For1042S.class));
        // Create 1042S output data from the transaction rows and send it to the file(s), retrieving summary statistics as needed.
        tempDefinition = taxProcessingService.getOutputDefinition(CUTaxKeyConstants.TAX_FORMAT_1042S_PREFIX, reportYear);
        stats.add(processTransactionRows(processingStartDate, summary, TransactionRow1042SProcessor.class, tempDefinition));
        // Print the transaction row data to another file.
        tempDefinition = taxProcessingService.getOutputDefinition(CUTaxKeyConstants.TAX_FORMAT_1042S_PREFIX + CUTaxKeyConstants.TAX_FORMAT_SUMMARY_SUFFIX, reportYear);
        processTransactionRows(processingStartDate, summary, TransactionRowPrintProcessor.For1042S.class, tempDefinition);
    } else {
        // Invalid tax processing type was given.
        throw new IllegalArgumentException("Unrecognized tax type");
    }
    // Print the statistics.
    printStatistics(stats);
}
Also used : TaxProcessingService(edu.cornell.kfs.tax.service.TaxProcessingService) TaxOutputDefinition(edu.cornell.kfs.tax.batch.TaxOutputDefinition) EnumMap(java.util.EnumMap)

Example 68 with EnumMap

use of java.util.EnumMap in project cu-kfs by CU-CommunityApps.

the class TaxProcessingDaoJdbc method processTransactionRows.

/*
     * Helper method that retrieves the generated transaction detail rows and uses them
     * to print tax rows to the output file(s) accordingly. Returns an EnumMap containing
     * numeric statistics pertaining to the transaction row processing.
     */
private <T extends TransactionDetailSummary> EnumMap<TaxStatType, Integer> processTransactionRows(final java.util.Date processingStartDate, final T summary, final Class<? extends TransactionRowProcessor<T>> processorClazz, final TaxOutputDefinition outputDefinition) {
    // Create the object that will handle the processing of the transaction row data.
    final TransactionRowProcessor<T> processor = TransactionRowProcessorBuilder.createBuilder().buildNewProcessor(processorClazz, outputDefinition, summary);
    processor.setReportsDirectory(reportsDirectory);
    // Use a ConnectionCallback via a JdbcTemplate to simplify the batch processing and transaction management.
    return getJdbcTemplate().execute(new ConnectionCallback<EnumMap<TaxStatType, Integer>>() {

        @Override
        public EnumMap<TaxStatType, Integer> doInConnection(Connection con) throws SQLException {
            PreparedStatement selectStatement = null;
            ResultSet rs = null;
            // Once tempStatement or tempWriter get passed to the processor successfully, we don't need to hold onto their references here.
            PreparedStatement tempStatement = null;
            Writer tempWriter = null;
            try {
                String[] tempValues;
                // Prepare the selection SQL.
                selectStatement = con.prepareStatement(processor.getSqlForSelect(summary), ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
                setParameters(selectStatement, processor.getParameterValuesForSelect(summary));
                // Prepare any other statements needed by the tax processing.
                tempValues = processor.getSqlForExtraStatements(summary);
                for (int i = 0; i < tempValues.length; i++) {
                    tempStatement = con.prepareStatement(tempValues[i]);
                    Object[][] defaultArgs = processor.getDefaultParameterValuesForExtraStatement(i, summary);
                    if (defaultArgs != null) {
                        setParameters(tempStatement, defaultArgs);
                    }
                    processor.setExtraStatement(tempStatement, i);
                }
                tempStatement = null;
                // Prepare any Writer instances needed by the tax processing.
                tempValues = processor.getFilePathsForWriters(summary, processingStartDate);
                for (int i = 0; i < tempValues.length; i++) {
                    tempWriter = new BufferedWriter(new PrintWriter(new File(tempValues[i])));
                    processor.setWriter(tempWriter, i);
                }
                tempWriter = null;
                // Get the transaction detail rows.
                rs = selectStatement.executeQuery();
                // Perform the actual processing.
                processor.processTaxRows(rs, summary);
                // Return the collected statistics.
                return processor.getStatistics();
            } catch (IOException e) {
                throw new RuntimeException(e);
            } finally {
                // Close resources, handling exceptions as needed.
                processor.closeForFinallyBlock();
                if (tempWriter != null) {
                    // If an error occurred passing a Writer to the processor, then close it here.
                    try {
                        tempWriter.close();
                    } catch (IOException e) {
                        LOG.error("Could not close file writer");
                    }
                }
                if (tempStatement != null) {
                    // If an error occurred passing a PreparedStatement to the processor, then close it here.
                    try {
                        tempStatement.close();
                    } catch (SQLException e) {
                        LOG.error("Could not close extra tax processing statement");
                    }
                }
                if (rs != null) {
                    try {
                        rs.close();
                    } catch (SQLException e) {
                        LOG.error("Could not close transaction row ResultSet");
                    }
                }
                if (selectStatement != null) {
                    try {
                        selectStatement.close();
                    } catch (SQLException e) {
                        LOG.error("Could not close transaction row selection statement");
                    }
                }
                processor.clearArraysAndReferences();
            }
        }
    });
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) ResultSet(java.sql.ResultSet) EnumMap(java.util.EnumMap) File(java.io.File) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) PrintWriter(java.io.PrintWriter)

Example 69 with EnumMap

use of java.util.EnumMap in project BibleMultiConverter by schierlm.

the class SWORD method doImport.

protected Bible doImport(Book book) throws Exception {
    OSISHelper helper = new OSISHelper();
    Bible result = new Bible(book.getName());
    TransformerHandler th = ((SAXTransformerFactory) SAXTransformerFactory.newInstance()).newTransformerHandler();
    Map<BookID, biblemulticonverter.data.Book> parsedBooks = new EnumMap<>(BookID.class);
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    List<Verse> allVerses = new ArrayList<>();
    Verse nextCandidate = null;
    for (Iterator<?> iter = book.getGlobalKeyList().iterator(); iter.hasNext(); ) {
        Verse v = (Verse) iter.next();
        while (nextCandidate != null && !nextCandidate.equals(v)) {
            if (book.contains(nextCandidate)) {
                System.out.println("WARNING: Verse (after) skipped by iterator: " + nextCandidate);
                allVerses.add(nextCandidate);
            }
            nextCandidate = nextCandidate.getVersification().add(nextCandidate, 1);
        }
        Verse prevCandidate = v.getVersification().subtract(v, 1);
        List<Verse> versesSkippedBefore = new ArrayList<>();
        while (prevCandidate != null && !allVerses.contains(prevCandidate) && !versesSkippedBefore.contains(prevCandidate)) {
            versesSkippedBefore.add(0, prevCandidate);
            prevCandidate = prevCandidate.getVersification().subtract(prevCandidate, 1);
        }
        for (Verse vv : versesSkippedBefore) {
            if (book.contains(vv)) {
                System.out.println("WARNING: Verse (before) skipped by iterator: " + vv);
                allVerses.add(vv);
            }
        }
        allVerses.add(v);
        nextCandidate = v.getVersification().add(v, 1);
    }
    while (nextCandidate != null) {
        if (book.contains(nextCandidate)) {
            System.out.println("WARNING: Verse (at end) skipped by iterator: " + nextCandidate);
            allVerses.add(nextCandidate);
        }
        Verse nextNextCandidate = nextCandidate.getVersification().add(nextCandidate, 1);
        if (nextCandidate.equals(nextNextCandidate))
            break;
        nextCandidate = nextNextCandidate;
    }
    for (Verse v : allVerses) {
        BookID bkid = biblemulticonverter.sword.BookMapping.MAPPING.get(v.getBook());
        biblemulticonverter.data.Book bk = parsedBooks.get(bkid);
        if (!parsedBooks.containsKey(bkid)) {
            bk = new biblemulticonverter.data.Book(bkid.getOsisID().replace("x-Intr", "Intr"), bkid, bkid.getEnglishName(), bkid.getEnglishName());
            parsedBooks.put(bkid, bk);
            bk.getChapters().add(new Chapter());
            result.getBooks().add(bk);
        }
        int chapterNum = v.getChapter(), verseNum = v.getVerse();
        while (bk.getChapters().size() < chapterNum) bk.getChapters().add(new Chapter());
        Chapter chapter = bk.getChapters().get(chapterNum == 0 ? 0 : chapterNum - 1);
        FormattedText verse;
        if (verseNum == 0) {
            verse = new FormattedText();
            if (chapter.getProlog() != null) {
                chapter.getProlog().accept(verse.getAppendVisitor());
            }
            chapter.setProlog(verse);
        } else {
            if (chapterNum == 0)
                throw new IllegalStateException("Verse " + verseNum + " in chapter 0 is invalid");
            verse = new biblemulticonverter.data.Verse("" + verseNum);
            chapter.getVerses().add((biblemulticonverter.data.Verse) verse);
        }
        Element root = doc.createElement("verse");
        th.setResult(new DOMResult(root));
        new BookData(book, v).getSAXEventProvider().provideSAXEvents(th);
        if (root.getChildNodes().getLength() == 1 && root.getFirstChild() instanceof Element && root.getFirstChild().getNodeName().equals("div") && root.getFirstChild().getChildNodes().getLength() >= 1 && root.getFirstChild().getFirstChild().getNodeName().equals("title")) {
            Element div = (Element) root.getFirstChild();
            root.removeChild(div);
            div.removeChild(div.getFirstChild());
            while (div.getFirstChild() != null) {
                Node child = div.getFirstChild();
                div.removeChild(child);
                root.appendChild(child);
            }
        } else {
            throw new RuntimeException("Unexpected OSIS structure!");
        }
        helper.handleVerse(root, verse);
        if (verse.getElementTypes(1).length() == 0) {
            System.out.println("WARNING: Empty verse " + bk.getAbbr() + " " + chapterNum + ":" + verseNum);
            if (verse instanceof biblemulticonverter.data.Verse)
                chapter.getVerses().remove(verse);
            else
                chapter.setProlog(null);
        }
    }
    for (biblemulticonverter.data.Book bk : parsedBooks.values()) {
        while (!bk.getChapters().isEmpty()) {
            Chapter ch = bk.getChapters().get(bk.getChapters().size() - 1);
            if (ch.getProlog() == null && ch.getVerses().isEmpty()) {
                bk.getChapters().remove(ch);
            } else {
                break;
            }
        }
        if (bk.getChapters().isEmpty()) {
            result.getBooks().remove(bk);
        }
    }
    return result;
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) DOMResult(javax.xml.transform.dom.DOMResult) Bible(biblemulticonverter.data.Bible) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) BookID(biblemulticonverter.data.BookID) Book(org.crosswire.jsword.book.Book) BookData(org.crosswire.jsword.book.BookData) EnumMap(java.util.EnumMap) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) Chapter(biblemulticonverter.data.Chapter) FormattedText(biblemulticonverter.data.FormattedText) Verse(org.crosswire.jsword.passage.Verse)

Example 70 with EnumMap

use of java.util.EnumMap in project BibleMultiConverter by schierlm.

the class Versification method toNewVersificationScheme.

public VersificationScheme toNewVersificationScheme() {
    Map<BookID, BitSet[]> coveredBooks = new EnumMap<BookID, BitSet[]>(BookID.class);
    for (Map.Entry<BookID, VerseSet[]> entry : verseSets.entrySet()) {
        BitSet[] value = new BitSet[entry.getValue().length];
        for (int i = 0; i < value.length; i++) {
            value[i] = new BitSet();
            VerseSet vs = entry.getValue()[i];
            if (vs == null)
                continue;
            if (vs.verses != null)
                value[i].or(vs.verses);
            if (vs.lastRangeTo >= vs.lastRangeFrom)
                value[i].set(vs.lastRangeFrom, vs.lastRangeTo + 1);
        }
        coveredBooks.put(entry.getKey(), value);
    }
    return new VersificationScheme(name, coveredBooks);
}
Also used : BitSet(java.util.BitSet) VersificationScheme(biblemulticonverter.tools.AbstractVersificationDetector.VersificationScheme) EnumMap(java.util.EnumMap) EnumMap(java.util.EnumMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Aggregations

EnumMap (java.util.EnumMap)389 Map (java.util.Map)73 ArrayList (java.util.ArrayList)70 List (java.util.List)61 HashMap (java.util.HashMap)60 Test (org.junit.Test)46 IOException (java.io.IOException)38 Collection (java.util.Collection)35 DecodeHintType (com.google.zxing.DecodeHintType)30 HashSet (java.util.HashSet)26 Set (java.util.Set)26 EncodeHintType (com.google.zxing.EncodeHintType)17 File (java.io.File)17 BitMatrix (com.google.zxing.common.BitMatrix)15 BookID (biblemulticonverter.data.BookID)14 Iterator (java.util.Iterator)14 URL (java.net.URL)12 TreeMap (java.util.TreeMap)12 Header (com.jsql.model.bean.util.Header)10 Request (com.jsql.model.bean.util.Request)10