Commit f729df22 authored by Eduardo Santos's avatar Eduardo Santos
Browse files

Now, the updateService method adds the NSLCM info (primitives output) to an...

Now, the updateService method adds the NSLCM info (primitives output) to an array, instead of replacing each time
parent 877437b2
Loading
Loading
Loading
Loading
+70 −4
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.hibernate5.jakarta.Hibernate5JakartaModule;
import org.apache.commons.logging.Log;
@@ -449,6 +450,7 @@ public class ServiceRepoService {

		boolean serviceCharacteristicChanged = false;
		boolean serviceCharacteristicChangedContainsPrimitive = false;
        boolean serviceCharacteristicChangedContainsNSLCM = false;
		
		String charChangedForNotes = "";
		List<Characteristic> childCharacteristicsChanged = new ArrayList<>();
@@ -475,12 +477,24 @@ public class ServiceRepoService {
									serviceCharacteristicChangedContainsPrimitive = true;
								}

							}
								// Check if the name contains "NSLCM"
								if (n.getName().toUpperCase().contains("NSLCM")) {
									// Flag to indicate that service characteristic with NSLCM is present
									serviceCharacteristicChangedContainsNSLCM = true;

									// Update the NSLCM Characteristic
									updateNSLCMCharacteristic(service, n);
								}
								
							}
						}
						 // As the NSLCM Characteristic was already updated, skip that one
						 if (!serviceCharacteristicChangedContainsNSLCM) {
							service.getServiceCharacteristicByName( n.getName() ).setValue( 
									new Any( n.getValue().getValue(), n.getValue().getAlias()  )
									);
						 }

					} else {
						service.addServiceCharacteristicItem(n);
						serviceCharacteristicChanged = true;	
@@ -679,6 +693,58 @@ public class ServiceRepoService {
		return service;
	}

	/**
     * Updates the NSLCM characteristic within a given service.
     *
     * @param service The service object containing the characteristics.
     * @param n       The characteristic object to be checked and potentially updated.
     */
    public void updateNSLCMCharacteristic(Service service, Characteristic n) {
        // Create an object mapper for JSON serialization/deserialization
        ObjectMapper primitivesObjectMapper = new ObjectMapper();

        // Retrieve the service characteristic based on the name
        Characteristic NSLCMCharacteristic = service.getServiceCharacteristicByName(n.getName());

        // Retrieve the current value of the characteristic
        Any NSLCMCharacteristicValue = NSLCMCharacteristic.getValue();

        // Retrieve the current value as a string directly from the service characteristic
        String ServiceNSLCMValue = service.getServiceCharacteristicByName(n.getName()).getValue().getValue();

        // Check if the current service characteristic value is null or explicitly "null" and initialize if needed
        if (ServiceNSLCMValue == null || "null".equals(ServiceNSLCMValue)) {
            service.getServiceCharacteristicByName(n.getName()).getValue().setValue("[]");
        }

        // Check if the current characteristic value is not null and not explicitly "null"
        if (NSLCMCharacteristicValue.getValue() != null || !"null".equals(NSLCMCharacteristicValue.getValue())) {
            // Convert the current characteristic value to a string
            String NSLCMCharacteristicValueString = NSLCMCharacteristicValue.getValue();

            ArrayList<String> arrayList = null;

            // Deserialize the current value back to an array list
            try {
                arrayList = primitivesObjectMapper.readValue(NSLCMCharacteristicValueString, new TypeReference<ArrayList<String>>() {});
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }

            // Add the new value to the list if it's not already present and is not null
            if (!arrayList.contains(n.getValue().getValue()) && n.getValue().getValue() != null) {
                arrayList.add(n.getValue().getValue());
            }

            // Update the characteristic with the newly modified list
            try {
                NSLCMCharacteristic.setValue(new Any(primitivesObjectMapper.writeValueAsString(arrayList), n.getValue().getAlias()));
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
    }

	/**
	 * @param service
	 * @param parentService