|  1 |     | 
     | 
  |  2 |     | 
     | 
  |  3 |     | 
     | 
  |  4 |     | 
     | 
  |  5 |     | 
     | 
  |  6 |     | 
     | 
  |  7 |     | 
     | 
  |  8 |     | 
     | 
  |  9 |     | 
     | 
  |  10 |     | 
     | 
  |  11 |     | 
     | 
  |  12 |     | 
     | 
  |  13 |     | 
     | 
  |  14 |     | 
     | 
  |  15 |     | 
     | 
  |  16 |     | 
   package org.kuali.rice.kns.maintenance;  | 
  |  17 |     | 
     | 
  |  18 |     | 
   import java.util.HashMap;  | 
  |  19 |     | 
   import java.util.List;  | 
  |  20 |     | 
   import java.util.Map;  | 
  |  21 |     | 
     | 
  |  22 |     | 
   import org.kuali.rice.kns.bo.GlobalBusinessObject;  | 
  |  23 |     | 
   import org.kuali.rice.kns.bo.GlobalBusinessObjectDetail;  | 
  |  24 |     | 
   import org.kuali.rice.kns.bo.PersistableBusinessObject;  | 
  |  25 |     | 
   import org.kuali.rice.kns.document.MaintenanceLock;  | 
  |  26 |     | 
   import org.kuali.rice.kns.service.BusinessObjectService;  | 
  |  27 |     | 
   import org.kuali.rice.kns.service.KNSServiceLocator;  | 
  |  28 |     | 
   import org.kuali.rice.kns.service.KNSServiceLocatorWeb;  | 
  |  29 |     | 
   import org.kuali.rice.kns.util.KNSPropertyConstants;  | 
  |  30 |     | 
   import org.kuali.rice.kns.util.ObjectUtils;  | 
  |  31 |     | 
     | 
  |  32 |    0 |    public abstract class KualiGlobalMaintainableImpl extends KualiMaintainableImpl { | 
  |  33 |     | 
       private static final long serialVersionUID = 4814145799502207182L;  | 
  |  34 |     | 
     | 
  |  35 |    0 |        private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KualiGlobalMaintainableImpl.class);  | 
  |  36 |     | 
     | 
  |  37 |     | 
         | 
  |  38 |     | 
     | 
  |  39 |     | 
     | 
  |  40 |     | 
       @Override  | 
  |  41 |     | 
       public void prepareForSave() { | 
  |  42 |    0 |            if (businessObject != null) { | 
  |  43 |    0 |                prepareGlobalsForSave();  | 
  |  44 |     | 
           }  | 
  |  45 |    0 |        }  | 
  |  46 |     | 
     | 
  |  47 |     | 
         | 
  |  48 |     | 
     | 
  |  49 |     | 
     | 
  |  50 |     | 
       @Override  | 
  |  51 |     | 
       public void processAfterRetrieve() { | 
  |  52 |    0 |            if (businessObject != null) { | 
  |  53 |    0 |                processGlobalsAfterRetrieve();  | 
  |  54 |     | 
           }  | 
  |  55 |    0 |        }  | 
  |  56 |     | 
     | 
  |  57 |     | 
         | 
  |  58 |     | 
     | 
  |  59 |     | 
     | 
  |  60 |     | 
     | 
  |  61 |     | 
       protected void processGlobalsAfterRetrieve() { | 
  |  62 |     | 
     | 
  |  63 |     | 
             | 
  |  64 |     | 
             | 
  |  65 |     | 
             | 
  |  66 |     | 
             | 
  |  67 |     | 
             | 
  |  68 |     | 
     | 
  |  69 |    0 |            GlobalBusinessObject gbo = (GlobalBusinessObject) businessObject;  | 
  |  70 |    0 |            Class gboClass = businessObject.getClass();  | 
  |  71 |    0 |            String finDocNumber = gbo.getDocumentNumber();  | 
  |  72 |     | 
     | 
  |  73 |     | 
             | 
  |  74 |     | 
             | 
  |  75 |     | 
     | 
  |  76 |     | 
             | 
  |  77 |     | 
             | 
  |  78 |    0 |            boolean assumptionIsWrong = false;  | 
  |  79 |     | 
             | 
  |  80 |     | 
             | 
  |  81 |    0 |            List primaryKeys = KNSServiceLocatorWeb.getBusinessObjectMetaDataService().listPrimaryKeyFieldNames(gboClass);  | 
  |  82 |    0 |            if (primaryKeys == null) { | 
  |  83 |    0 |                assumptionIsWrong = true;  | 
  |  84 |     | 
           }  | 
  |  85 |    0 |            else if (primaryKeys.isEmpty()) { | 
  |  86 |    0 |                assumptionIsWrong = true;  | 
  |  87 |     | 
           }  | 
  |  88 |    0 |            else if (primaryKeys.size() != 1) { | 
  |  89 |    0 |                assumptionIsWrong = true;  | 
  |  90 |     | 
           }  | 
  |  91 |    0 |            else if (!primaryKeys.get(0).getClass().equals(String.class)) { | 
  |  92 |    0 |                assumptionIsWrong = true;  | 
  |  93 |     | 
           }  | 
  |  94 |    0 |            else if (!KNSPropertyConstants.DOCUMENT_NUMBER.equalsIgnoreCase((String) primaryKeys.get(0))) { | 
  |  95 |    0 |                assumptionIsWrong = true;  | 
  |  96 |     | 
           }  | 
  |  97 |    0 |            if (assumptionIsWrong) { | 
  |  98 |    0 |                throw new RuntimeException("An assertion about the nature of the primary keys for this GBO has " + "failed, and processing cannot continue."); | 
  |  99 |     | 
           }  | 
  |  100 |     | 
     | 
  |  101 |     | 
             | 
  |  102 |     | 
             | 
  |  103 |     | 
             | 
  |  104 |     | 
             | 
  |  105 |     | 
             | 
  |  106 |    0 |            Map pkMap = new HashMap();  | 
  |  107 |    0 |            pkMap.put(KNSPropertyConstants.DOCUMENT_NUMBER, finDocNumber);  | 
  |  108 |    0 |            PersistableBusinessObject newBo = null;  | 
  |  109 |    0 |            newBo = (PersistableBusinessObject) KNSServiceLocator.getBusinessObjectService().findByPrimaryKey(gboClass, pkMap);  | 
  |  110 |    0 |            if (newBo == null) { | 
  |  111 |    0 |                throw new RuntimeException("The Global Business Object could not be retrieved from the DB.  " + "This should never happen under normal circumstances.  If this is a legitimate case " + "Then this exception should be removed."); | 
  |  112 |     | 
           }  | 
  |  113 |     | 
             | 
  |  114 |     | 
             | 
  |  115 |     | 
           try { | 
  |  116 |    0 |                ObjectUtils.setObjectPropertyDeep(newBo, KNSPropertyConstants.NEW_COLLECTION_RECORD, boolean.class, true, 2);  | 
  |  117 |     | 
           }  | 
  |  118 |    0 |            catch (Exception e) { | 
  |  119 |    0 |                LOG.error("unable to set newCollectionRecord property: " + e.getMessage()); | 
  |  120 |    0 |                throw new RuntimeException("unable to set newCollectionRecord property: " + e.getMessage()); | 
  |  121 |    0 |            }  | 
  |  122 |     | 
     | 
  |  123 |     | 
             | 
  |  124 |    0 |            businessObject = newBo;  | 
  |  125 |    0 |        }  | 
  |  126 |     | 
     | 
  |  127 |     | 
         | 
  |  128 |     | 
     | 
  |  129 |     | 
     | 
  |  130 |     | 
       protected void prepareGlobalsForSave() { | 
  |  131 |    0 |            GlobalBusinessObject gbo = (GlobalBusinessObject) businessObject;  | 
  |  132 |     | 
     | 
  |  133 |     | 
             | 
  |  134 |    0 |            gbo.setDocumentNumber(documentNumber);  | 
  |  135 |     | 
     | 
  |  136 |    0 |            List<? extends GlobalBusinessObjectDetail> details = gbo.getAllDetailObjects();  | 
  |  137 |    0 |            for ( GlobalBusinessObjectDetail detail : details ) { | 
  |  138 |    0 |                detail.setDocumentNumber(documentNumber);  | 
  |  139 |     | 
           }  | 
  |  140 |    0 |        }  | 
  |  141 |     | 
     | 
  |  142 |     | 
         | 
  |  143 |     | 
     | 
  |  144 |     | 
     | 
  |  145 |     | 
     | 
  |  146 |     | 
     | 
  |  147 |     | 
       @Override  | 
  |  148 |     | 
       public abstract List<MaintenanceLock> generateMaintenanceLocks();  | 
  |  149 |     | 
     | 
  |  150 |     | 
         | 
  |  151 |     | 
     | 
  |  152 |     | 
     | 
  |  153 |     | 
       @Override  | 
  |  154 |     | 
       public void saveBusinessObject() { | 
  |  155 |    0 |            BusinessObjectService boService = KNSServiceLocator.getBusinessObjectService();  | 
  |  156 |    0 |            GlobalBusinessObject gbo = (GlobalBusinessObject) businessObject;  | 
  |  157 |     | 
     | 
  |  158 |     | 
             | 
  |  159 |    0 |            List<PersistableBusinessObject> bosToDeactivate = gbo.generateDeactivationsToPersist();  | 
  |  160 |    0 |            if (bosToDeactivate != null) { | 
  |  161 |    0 |                if (!bosToDeactivate.isEmpty()) { | 
  |  162 |    0 |                    boService.save(bosToDeactivate);  | 
  |  163 |     | 
               }  | 
  |  164 |     | 
           }  | 
  |  165 |     | 
     | 
  |  166 |     | 
             | 
  |  167 |    0 |            List<PersistableBusinessObject> bosToPersist = gbo.generateGlobalChangesToPersist();  | 
  |  168 |    0 |            if (bosToPersist != null) { | 
  |  169 |    0 |                if (!bosToPersist.isEmpty()) { | 
  |  170 |    0 |                    boService.save(bosToPersist);  | 
  |  171 |     | 
               }  | 
  |  172 |     | 
           }  | 
  |  173 |     | 
     | 
  |  174 |    0 |        }  | 
  |  175 |     | 
         | 
  |  176 |     | 
       public abstract Class<? extends PersistableBusinessObject> getPrimaryEditedBusinessObjectClass();  | 
  |  177 |     | 
   }  |