001package org.kuali.ole.docstore.document.jcr;
002
003
004import org.kuali.ole.docstore.OleDocStoreException;
005import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
006
007import javax.jcr.Session;
008import java.util.List;
009import java.util.regex.Matcher;
010import java.util.regex.Pattern;
011
012/**
013 * Implements the DocumentManager interface for [Work-Bib-MARC] documents.
014 *
015 * @author tirumalesh.b
016 * @version %I%, %G%
017 *          Date: 28/8/12 Time: 1:10 PM
018 */
019public class JcrWorkBibMarcDocumentManager
020        extends JcrWorkBibDocumentManager {
021    private static JcrWorkBibMarcDocumentManager ourInstance = null;
022
023    public static JcrWorkBibMarcDocumentManager getInstance() {
024        if (null == ourInstance) {
025            ourInstance = new JcrWorkBibMarcDocumentManager();
026        }
027        return ourInstance;
028    }
029
030    protected JcrWorkBibMarcDocumentManager() {
031        super();
032    }
033
034    @Override
035    protected void modifyDocumentContent(RequestDocument doc, String nodeIdentifier, String parentNodeIdentifier) {
036        String content = doc.getContent().getContent();
037        if (content != null && content != "" && content.length() > 0) {
038            Pattern pattern = Pattern.compile("tag=\"001\">.*?</controlfield");
039            Pattern pattern2 = Pattern.compile("<controlfield.*?tag=\"001\"/>");
040            Matcher matcher = pattern.matcher(content);
041            Matcher matcher2 = pattern2.matcher(content);
042            if (matcher.find()) {
043                doc.getContent().setContent(matcher.replaceAll("tag=\"001\">" + nodeIdentifier + "</controlfield"));
044            } else if (matcher2.find()) {
045                doc.getContent()
046                        .setContent(matcher2.replaceAll("<controlfield tag=\"001\">" + nodeIdentifier + "</controlfield>"));
047            } else {
048                int ind = content.indexOf("</leader>") + 9;
049                if (ind == 8) {
050                    ind = content.indexOf("<leader/>") + 9;
051                    if (ind == 8) {
052                        ind = content.indexOf("record>") + 7;
053                    }
054                }
055                StringBuilder sb = new StringBuilder();
056                sb.append(content.substring(0, ind));
057                sb.append("<controlfield tag=\"001\">");
058                sb.append(nodeIdentifier);
059                sb.append("</controlfield>");
060                sb.append(content.substring(ind + 1));
061                doc.getContent().setContent(sb.toString());
062            }
063        }
064    }
065
066    @Override
067    public void validateInput(RequestDocument requestDocument, Object object, List<String> valuesList) throws OleDocStoreException {
068        Session session = (Session) object;
069        for (RequestDocument linkDoc : requestDocument.getLinkedRequestDocuments()) {
070            JcrWorkInstanceDocumentManager.getInstance().validateInput(linkDoc, session, valuesList);
071        }
072    }
073}