Fix Bug due to user management changes
[clamp.git] / src / main / java / org / onap / clamp / loop / template / PolicyModelsService.java
index ad9ce70..eb83c66 100644 (file)
@@ -32,6 +32,8 @@ import org.onap.clamp.clds.tosca.ToscaYamlToJsonConvertor;
 import org.onap.clamp.util.SemanticVersioning;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Propagation;
+import org.springframework.transaction.annotation.Transactional;
 
 @Service
 public class PolicyModelsService {
@@ -122,4 +124,17 @@ public class PolicyModelsService {
         return policyModelsRepository.findById(new PolicyModelId(type, version)).orElse(new PolicyModel())
                 .getPolicyModelTosca();
     }
+
+    /**
+     * This method creates an PolicyModel in Db if it does not exist.
+     *
+     * @param policyModel The policyModel to save
+     */
+    @Transactional(propagation = Propagation.REQUIRES_NEW)
+    public void createPolicyInDbIfNeeded(PolicyModel policyModel) {
+        if (!policyModelsRepository
+                .existsById(new PolicyModelId(policyModel.getPolicyModelType(), policyModel.getVersion()))) {
+            policyModelsRepository.save(policyModel);
+        }
+    }
 }