def _normalize_image_location_for_db(image_data):
    """
    This function takes the legacy locations field and the newly added
    location_data field from the image_data values dictionary which flows
    over the wire between the registry and API servers and converts it
    into the location_data format only which is then consumable by the
    Image object.

    :param image_data: a dict of values representing information in the image
    :returns: a new image data dict
    """
    if 'locations' not in image_data and 'location_data' not in image_data:
        image_data['locations'] = None
        return image_data
    locations = image_data.pop('locations', [])
    location_data = image_data.pop('location_data', [])
    location_data_dict = {}
    for l in locations:
        location_data_dict[l] = {}
    for l in location_data:
        location_data_dict[l['url']] = {'metadata': l['metadata'], 'status':
            l['status'], 'id': l['id'] if 'id' in l else None}
    ordered_keys = locations[:]
    for ld in location_data:
        if ld['url'] not in ordered_keys:
            ordered_keys.append(ld['url'])
    location_data = []
    for loc in ordered_keys:
        data = location_data_dict[loc]
        if data:
            location_data.append({'url': loc, 'metadata': data['metadata'],
                'status': data['status'], 'id': data['id']})
        else:
            location_data.append({'url': loc, 'metadata': {}, 'status':
                'active', 'id': None})
    image_data['locations'] = location_data
    return image_data
