001/*
002 * Copyright 2012 The Kuali Foundation.
003 * 
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * 
008 * http://www.opensource.org/licenses/ecl2.php
009 * 
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.ole.documenthandler;
017
018import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
019
020import java.util.regex.Matcher;
021import java.util.regex.Pattern;
022
023/**
024 * Class WorkBibMarcContentHandler to handle content of Marc Files.
025 *
026 * @author Rajesh Chowdary K
027 * @created Mar 21, 2012
028 */
029public class WorkBibMarcContentHandler {
030
031    public void doPreIngestContentManipulations(RequestDocument doc, final String fileNodeUUID) {
032        String content = doc.getContent().getContent();
033        if (content != null && content != "" && content.length() > 0) {
034            Pattern pattern = Pattern.compile("tag=\"001\">.*?</controlfield");
035            Pattern pattern2 = Pattern.compile("<controlfield.*?tag=\"001\"/>");
036            Matcher matcher = pattern.matcher(content);
037            Matcher matcher2 = pattern2.matcher(content);
038            if (matcher.find()) {
039                doc.getContent().setContent(matcher.replaceAll("tag=\"001\">" + fileNodeUUID + "</controlfield"));
040            } else if (matcher2.find()) {
041                doc.getContent()
042                        .setContent(matcher2.replaceAll("<controlfield tag=\"001\">" + fileNodeUUID + "</controlfield>"));
043            } else {
044                int ind = content.indexOf("</leader>") + 9;
045                if (ind == 8) {
046                    ind = content.indexOf("<leader/>") + 9;
047                    if (ind == 8) {
048                        ind = content.indexOf("record>") + 7;
049                    }
050                }
051                StringBuilder sb = new StringBuilder();
052                sb.append(content.substring(0, ind));
053                sb.append("<controlfield tag=\"001\">");
054                sb.append(fileNodeUUID);
055                sb.append("</controlfield>");
056                sb.append(content.substring(ind + 1));
057                doc.getContent().setContent(sb.toString());
058            }
059        }
060    }
061
062
063    public void doPreIngestContentManipulationsForTesting(RequestDocument doc, final String fileNodeUUID) {
064        String content = doc.getContent().getContent();
065        int ind = content.indexOf("</leader>") + 9;
066        if (ind == 8) {
067            ind = content.indexOf("<leader/>") + 9;
068            if (ind == 8) {
069                ind = content.indexOf("record>") + 7;
070            }
071        }
072        StringBuilder sb = new StringBuilder();
073        sb.append(content.substring(0, ind));
074        sb.append("<controlfield tag=\"001\">");
075        sb.append(fileNodeUUID);
076        sb.append("</controlfield>");
077        sb.append(content.substring(ind + 1));
078        doc.getContent().setContent(sb.toString());
079    }
080}