class StorageUnitHelper {

    List<StorageUnit> createStorageUnitsFromEntities(Collection<StorageUnitEntity> storageUnitEntities, Boolean includeStorageUnitStatusHistory,
        Boolean excludeBusinessObjectDataStorageFiles)
    {
        List<StorageUnit> storageUnits = new ArrayList<>();

        for (StorageUnitEntity storageUnitEntity : storageUnitEntities)
        {
            StorageUnit storageUnit = new StorageUnit();
            storageUnits.add(storageUnit);

            Storage storage = new Storage();
            storageUnit.setStorage(storage);

            StorageEntity storageEntity = storageUnitEntity.getStorage();
            storage.setName(storageEntity.getName());
            storage.setStoragePlatformName(storageEntity.getStoragePlatform().getName());

            // Add the storage attributes.
            if (!CollectionUtils.isEmpty(storageEntity.getAttributes()))
            {
                List<Attribute> storageAttributes = new ArrayList<>();
                storage.setAttributes(storageAttributes);
                for (StorageAttributeEntity storageAttributeEntity : storageEntity.getAttributes())
                {
                    Attribute attribute = new Attribute();
                    storageAttributes.add(attribute);
                    attribute.setName(storageAttributeEntity.getName());
                    attribute.setValue(storageAttributeEntity.getValue());
                }
            }

            // Add the storage directory.
            if (storageUnitEntity.getDirectoryPath() != null)
            {
                StorageDirectory storageDirectory = new StorageDirectory();
                storageUnit.setStorageDirectory(storageDirectory);
                storageDirectory.setDirectoryPath(storageUnitEntity.getDirectoryPath());
            }

            // Add the storage files, is they are not excluded and present in the database.
            if (BooleanUtils.isNotTrue(excludeBusinessObjectDataStorageFiles) && CollectionUtils.isNotEmpty(storageUnitEntity.getStorageFiles()))
            {
                List<StorageFile> storageFiles = new ArrayList<>();
                storageUnit.setStorageFiles(storageFiles);

                for (StorageFileEntity storageFileEntity : storageUnitEntity.getStorageFiles())
                {
                    storageFiles.add(storageFileHelper.createStorageFileFromEntity(storageFileEntity, storageUnitEntity.getDirectoryPath()));
                }
            }

            // Set the storage unit status.
            storageUnit.setStorageUnitStatus(storageUnitEntity.getStatus().getCode());

            // If specified, add storage unit status history.
            if (BooleanUtils.isTrue(includeStorageUnitStatusHistory))
            {
                List<StorageUnitStatusChangeEvent> storageUnitStatusChangeEvents = new ArrayList<>();
                storageUnit.setStorageUnitStatusHistory(storageUnitStatusChangeEvents);
                for (StorageUnitStatusHistoryEntity storageUnitStatusHistoryEntity : storageUnitEntity.getHistoricalStatuses())
                {
                    storageUnitStatusChangeEvents.add(new StorageUnitStatusChangeEvent(storageUnitStatusHistoryEntity.getStatus().getCode(),
                        HerdDateUtils.getXMLGregorianCalendarValue(storageUnitStatusHistoryEntity.getCreatedOn()),
                        storageUnitStatusHistoryEntity.getCreatedBy()));
                }
            }

            // Set the number of failed attempts to execute a storage policy transition.
            storageUnit.setStoragePolicyTransitionFailedAttempts(storageUnitEntity.getStoragePolicyTransitionFailedAttempts());

            if (storageUnitEntity.getRestoreExpirationOn() != null)
            {
                storageUnit.setRestoreExpirationOn(HerdDateUtils.getXMLGregorianCalendarValue(storageUnitEntity.getRestoreExpirationOn()));
            }
        }

        return storageUnits;
    }

}