Search in sources :

Example 1 with SkatKeyCompositeId

use of cz.mzk.recordmanager.server.model.SkatKey.SkatKeyCompositeId in project RecordManager2 by moravianlibrary.

the class GenerateSkatKeysProcessor method process.

@Override
public Set<SkatKey> process(Long item) throws Exception {
    Set<SkatKey> parsedKeys = new HashSet<>();
    HarvestedRecord hr = harvestedRecordDao.get(item);
    if (hr.getRawRecord() == null) {
        return parsedKeys;
    }
    MarcRecord marc = null;
    InputStream is = new ByteArrayInputStream(hr.getRawRecord());
    try {
        marc = marcXmlParser.parseRecord(is);
    } catch (Exception e) {
        return parsedKeys;
    }
    for (DataField df : marc.getDataFields("996")) {
        if (df.getSubfield('e') == null) {
            continue;
        }
        if (df.getSubfield('w') == null) {
            continue;
        }
        String sigla = df.getSubfield('e').getData();
        String recordId = df.getSubfield('w').getData();
        if (recordId.length() > 100 || sigla.length() > 20) {
            // ignore garbage
            continue;
        }
        SkatKey key = new SkatKey(new SkatKeyCompositeId(hr.getId(), sigla, recordId));
        parsedKeys.add(key);
    }
    // ignore records having not enough information
    if (parsedKeys.size() < 2) {
        return Collections.emptySet();
    }
    // find already existing keys
    Set<SkatKey> existingKeys = new HashSet<>(skatKeyDao.getSkatKeysForRecord(item));
    Set<SkatKey> newKeys = new HashSet<>();
    for (SkatKey current : parsedKeys) {
        if (existingKeys.contains(current)) {
            continue;
        }
        newKeys.add(current);
    }
    return newKeys;
}
Also used : DataField(org.marc4j.marc.DataField) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SkatKeyCompositeId(cz.mzk.recordmanager.server.model.SkatKey.SkatKeyCompositeId) MarcRecord(cz.mzk.recordmanager.server.marc.MarcRecord) SkatKey(cz.mzk.recordmanager.server.model.SkatKey) HashSet(java.util.HashSet) HarvestedRecord(cz.mzk.recordmanager.server.model.HarvestedRecord)

Example 2 with SkatKeyCompositeId

use of cz.mzk.recordmanager.server.model.SkatKey.SkatKeyCompositeId in project RecordManager2 by moravianlibrary.

the class SkatKeyDAOHibernate method getSkatKeysForRecord.

@SuppressWarnings({ "unchecked", "serial" })
@Override
public List<SkatKey> getSkatKeysForRecord(Long skatRecordId) {
    Session session = sessionFactory.getCurrentSession();
    return (List<SkatKey>) session.createSQLQuery("SELECT skat_record_id,sigla,local_record_id,manually_merged" + " FROM skat_keys" + " WHERE skat_record_id = ?").setResultTransformer(new ResultTransformer() {

        @Override
        public Object transformTuple(Object[] tuple, String[] aliases) {
            SkatKeyCompositeId compositeId = new SkatKeyCompositeId();
            boolean manuallyMerged = false;
            for (int i = 0; i < tuple.length; i++) {
                switch(aliases[i]) {
                    case "skat_record_id":
                        compositeId.setSkatHarvestedRecordId(((BigDecimal) tuple[i]).longValue());
                        break;
                    case "sigla":
                        compositeId.setSigla(((String) tuple[i]));
                        break;
                    case "local_record_id":
                        compositeId.setRecordId((String) tuple[i]);
                        break;
                    case "manually_merged":
                        manuallyMerged = (boolean) tuple[i];
                        break;
                }
            }
            SkatKey key = new SkatKey(compositeId);
            key.setManuallyMerged(manuallyMerged);
            return key;
        }

        @SuppressWarnings("rawtypes")
        @Override
        public List transformList(List collection) {
            return collection;
        }
    }).setParameter(0, skatRecordId).list();
}
Also used : SkatKeyCompositeId(cz.mzk.recordmanager.server.model.SkatKey.SkatKeyCompositeId) ResultTransformer(org.hibernate.transform.ResultTransformer) List(java.util.List) SkatKey(cz.mzk.recordmanager.server.model.SkatKey) Session(org.hibernate.Session)

Aggregations

SkatKey (cz.mzk.recordmanager.server.model.SkatKey)2 SkatKeyCompositeId (cz.mzk.recordmanager.server.model.SkatKey.SkatKeyCompositeId)2 MarcRecord (cz.mzk.recordmanager.server.marc.MarcRecord)1 HarvestedRecord (cz.mzk.recordmanager.server.model.HarvestedRecord)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Session (org.hibernate.Session)1 ResultTransformer (org.hibernate.transform.ResultTransformer)1 DataField (org.marc4j.marc.DataField)1