View Javadoc

1   package org.kuali.ole.docstore.document;
2   
3   
4   import org.kuali.ole.docstore.OleDocStoreException;
5   import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
6   
7   import javax.jcr.Session;
8   import java.util.List;
9   import java.util.regex.Matcher;
10  import java.util.regex.Pattern;
11  
12  /**
13   * Implements the DocumentManager interface for [Work-Bib-MARC] documents.
14   *
15   * @author tirumalesh.b
16   * @version %I%, %G%
17   *          Date: 28/8/12 Time: 1:10 PM
18   */
19  public class WorkBibMarcDocumentManager
20          extends WorkBibDocumentManager {
21      private static WorkBibMarcDocumentManager ourInstance = null;
22  
23      public static WorkBibMarcDocumentManager getInstance() {
24          if (null == ourInstance) {
25              ourInstance = new WorkBibMarcDocumentManager();
26          }
27          return ourInstance;
28      }
29  
30      protected WorkBibMarcDocumentManager() {
31          super();
32      }
33  
34      @Override
35      protected void modifyDocumentContent(RequestDocument doc, String nodeIdentifier, String parentNodeIdentifier) {
36          String content = doc.getContent().getContent();
37          if (content != null && content != "" && content.length() > 0) {
38              Pattern pattern = Pattern.compile("tag=\"001\">.*?</controlfield");
39              Pattern pattern2 = Pattern.compile("<controlfield.*?tag=\"001\"/>");
40              Matcher matcher = pattern.matcher(content);
41              Matcher matcher2 = pattern2.matcher(content);
42              if (matcher.find()) {
43                  doc.getContent().setContent(matcher.replaceAll("tag=\"001\">" + nodeIdentifier + "</controlfield"));
44              }
45              else if (matcher2.find()) {
46                  doc.getContent()
47                     .setContent(matcher2.replaceAll("<controlfield tag=\"001\">" + nodeIdentifier + "</controlfield>"));
48              }
49              else {
50                  int ind = content.indexOf("</leader>") + 9;
51                  if (ind == 8) {
52                      ind = content.indexOf("<leader/>") + 9;
53                      if (ind == 8) {
54                          ind = content.indexOf("record>") + 7;
55                      }
56                  }
57                  StringBuilder sb = new StringBuilder();
58                  sb.append(content.substring(0, ind));
59                  sb.append("<controlfield tag=\"001\">");
60                  sb.append(nodeIdentifier);
61                  sb.append("</controlfield>");
62                  sb.append(content.substring(ind + 1));
63                  doc.getContent().setContent(sb.toString());
64              }
65          }
66      }
67  
68      @Override
69      public void validateInput(RequestDocument requestDocument, Session session, List<String> valuesList) throws OleDocStoreException {
70          for(RequestDocument linkDoc : requestDocument.getLinkedRequestDocuments()){
71              WorkInstanceDocumentManager.getInstance().validateInput(linkDoc , session, valuesList);
72          }
73      }
74  }