[507] | 1 | package gov.hhs.fha.nhinc.repository.service;
|
---|
| 2 |
|
---|
| 3 | import gov.hhs.fha.nhinc.repository.model.Document;
|
---|
| 4 | import gov.hhs.fha.nhinc.repository.dao.DocumentDao;
|
---|
| 5 | import gov.hhs.fha.nhinc.repository.dao.EventCodeDao;
|
---|
| 6 | import gov.hhs.fha.nhinc.repository.model.DocumentQueryParams;
|
---|
| 7 | import gov.hhs.fha.nhinc.repository.model.EventCode;
|
---|
| 8 | import gov.hhs.fha.nhinc.util.hash.SHA1HashCode;
|
---|
| 9 | import java.util.ArrayList;
|
---|
| 10 | import java.util.List;
|
---|
| 11 | import java.util.Set;
|
---|
| 12 | import org.apache.commons.logging.Log;
|
---|
| 13 | import org.apache.commons.logging.LogFactory;
|
---|
| 14 |
|
---|
| 15 | /**
|
---|
| 16 | * Persistence service for Document records
|
---|
| 17 | *
|
---|
| 18 | * @author Neil Webb
|
---|
| 19 | */
|
---|
| 20 | public class DocumentService
|
---|
| 21 | {
|
---|
| 22 | private static Log log = LogFactory.getLog(DocumentService.class);
|
---|
| 23 |
|
---|
| 24 | /**
|
---|
| 25 | * Save a document record.
|
---|
| 26 | *
|
---|
| 27 | * @param document Document object to save.
|
---|
| 28 | */
|
---|
| 29 | public void saveDocument(Document document)
|
---|
| 30 | {
|
---|
| 31 | log.debug("Saving a document");
|
---|
| 32 | if (document != null)
|
---|
| 33 | {
|
---|
| 34 | if (document.getDocumentid() != null)
|
---|
| 35 | {
|
---|
| 36 | if(log.isDebugEnabled())
|
---|
| 37 | {
|
---|
| 38 | log.debug("Performing an update for document: " + document.getDocumentid().longValue());
|
---|
| 39 | }
|
---|
| 40 | Document ecDoc = getDocument(document.getDocumentid());
|
---|
| 41 | if (ecDoc != null)
|
---|
| 42 | {
|
---|
| 43 | // Delete existing event codes
|
---|
| 44 | Set<EventCode> eventCodes = ecDoc.getEventCodes();
|
---|
| 45 | if ((eventCodes != null) && !eventCodes.isEmpty())
|
---|
| 46 | {
|
---|
| 47 | EventCodeDao eventCodeDao = new EventCodeDao();
|
---|
| 48 | for (EventCode eventCode : eventCodes)
|
---|
| 49 | {
|
---|
| 50 | eventCodeDao.delete(eventCode);
|
---|
| 51 | eventCode.setEventCodeId(null);
|
---|
| 52 | }
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | // Reset event code identifiers
|
---|
| 56 | eventCodes = document.getEventCodes();
|
---|
| 57 | if ((eventCodes != null) && !eventCodes.isEmpty())
|
---|
| 58 | {
|
---|
| 59 | for (EventCode eventCode : eventCodes)
|
---|
| 60 | {
|
---|
| 61 | if (eventCode.getEventCodeId() != null)
|
---|
| 62 | {
|
---|
| 63 | eventCode.setEventCodeId(null);
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 | else
|
---|
| 70 | {
|
---|
| 71 | log.debug("Performing an insert");
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | // Calculate the hash code.
|
---|
| 75 | //-------------------------
|
---|
| 76 | if (document.getRawData() != null)
|
---|
| 77 | {
|
---|
| 78 | try
|
---|
| 79 | {
|
---|
| 80 | String sHash = "";
|
---|
| 81 | sHash = SHA1HashCode.calculateSHA1(new String(document.getRawData()));
|
---|
| 82 | if (log.isDebugEnabled())
|
---|
| 83 | {
|
---|
| 84 | log.debug("Created Hash Code: " + sHash + " for string: " +
|
---|
| 85 | new String(document.getRawData()));
|
---|
| 86 | }
|
---|
| 87 | document.setHash(sHash);
|
---|
| 88 | }
|
---|
| 89 | catch (Throwable t)
|
---|
| 90 | {
|
---|
| 91 | String sError = "Failed to create SHA-1 Hash code. Error: " + t.getMessage() +
|
---|
| 92 | "Data Text: " + new String(document.getRawData());
|
---|
| 93 | log.error(sError, t);
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 | else
|
---|
| 97 | {
|
---|
| 98 | document.setHash("");
|
---|
| 99 | log.warn("No SHA-1 Hash Code created because document was null.");
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | DocumentDao dao = new DocumentDao();
|
---|
| 104 | dao.save(document);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | /**
|
---|
| 108 | * Delete a document
|
---|
| 109 | *
|
---|
| 110 | * @param document Document to delete
|
---|
| 111 | * @throws DocumentServiceException
|
---|
| 112 | */
|
---|
| 113 | public void deleteDocument(Document document) throws DocumentServiceException
|
---|
| 114 | {
|
---|
| 115 | log.debug("Deleting a document");
|
---|
| 116 | DocumentDao dao = new DocumentDao();
|
---|
| 117 |
|
---|
| 118 | if ((document != null) && (document.getDocumentid() == null) && (document.getDocumentUniqueId() != null))
|
---|
| 119 | {
|
---|
| 120 | // Query by unique id and delete if only one exists.
|
---|
| 121 | DocumentQueryParams params = new DocumentQueryParams();
|
---|
| 122 | List<String> uniqueIds = new ArrayList<String>();
|
---|
| 123 | uniqueIds.add(document.getDocumentUniqueId());
|
---|
| 124 |
|
---|
| 125 | List<Document> docs = documentQuery(params);
|
---|
| 126 | if ((docs != null) && (docs.size() == 1))
|
---|
| 127 | {
|
---|
| 128 | document = docs.get(0);
|
---|
| 129 | }
|
---|
| 130 | else
|
---|
| 131 | {
|
---|
| 132 | throw new DocumentServiceException("Single document match not found for document unique id: " + document.getDocumentUniqueId());
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 | else
|
---|
| 136 | {
|
---|
| 137 | if (document == null)
|
---|
| 138 | {
|
---|
| 139 | throw new DocumentServiceException("Document to delete was null");
|
---|
| 140 | }
|
---|
| 141 | else if (document.getDocumentUniqueId() == null)
|
---|
| 142 | {
|
---|
| 143 | throw new DocumentServiceException("Document unique id was null");
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | dao.delete(document);
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | /**
|
---|
| 151 | * Retrieve a document by identifier
|
---|
| 152 | *
|
---|
| 153 | * @param documentId Document identifier
|
---|
| 154 | * @return Retrieved document
|
---|
| 155 | */
|
---|
| 156 | public Document getDocument(Long documentId)
|
---|
| 157 | {
|
---|
| 158 | DocumentDao dao = new DocumentDao();
|
---|
| 159 | return dao.findById(documentId);
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | /**
|
---|
| 163 | * Retrieves all documents
|
---|
| 164 | *
|
---|
| 165 | * @return All document records
|
---|
| 166 | */
|
---|
| 167 | public List<Document> getAllDocuments()
|
---|
| 168 | {
|
---|
| 169 | DocumentDao dao = new DocumentDao();
|
---|
| 170 | return dao.findAll();
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | /**
|
---|
| 174 | * Document query
|
---|
| 175 | *
|
---|
| 176 | * @param params Document query parameters
|
---|
| 177 | * @return Query results
|
---|
| 178 | */
|
---|
| 179 | public List<Document> documentQuery(DocumentQueryParams params)
|
---|
| 180 | {
|
---|
| 181 | DocumentDao dao = new DocumentDao();
|
---|
| 182 | return dao.findDocuments(params);
|
---|
| 183 | }
|
---|
| 184 | }
|
---|