text:
import demistomock as demisto
from CommonServerPython import *
from CommonServerUserPython import *

''' IMPORTS '''

import json
import requests
import traceback
from datetime import datetime, timedelta
import os

# Disable insecure warnings
requests.packages.urllib3.disable_warnings()

''' GLOBALS/PARAMS '''
FETCHES_INCIDENTS = ''
FETCH_TIME = ''
FETCH_ATTACHMENTS = ''
OBJECTS_TO_FETCH = ''
MAX_RESULT = ''
USERNAME = ''
PASSWORD = ''
SERVER = ''
SECURED = False
CLIENT_ID = ''
QUERY_STRING = ''
DATE_FORMAT = ''
BASE_URL = ''

HTTP_CODES = {
    'unauthorized': 401,
    'internal_server_error': 500,
    'success': 200
}

HEADERS = {
    'Content-Type': "application/json",
    'Accept': "application/json"
}

QUERY_OPERATORS = ['eq', 'gt', 'lt', 'contains', 'startswith']

ONE_STEP_ACTION_HEADERS = ['name', 'displayName', 'description', 'id', 'association', 'standInKey']

#######################################################################################################################

''' HELPER FUNCTIONS '''

def parse_response(response, error_operation, file_content=False, is_fetch=False):
    try:
        response.raise_for_status()
        if not response.content:
            return
        if file_content:
            return response.content
        else:
            return response.json()
    except requests.exceptions.HTTPError:
        try:
            res_json = response.json()
            err_msg = res_json.get('errorMessage') or res_json.get('error_description') or res_json.get('Message')
        except Exception:
            err_msg = response.content.decode('utf-8')
        raise_or_return_error(error_operation + ": " + str(err_msg), is_fetch)
    except Exception as error:
        raise_or_return_error(f'Could not parse response {error}', is_fetch)

def cherwell_dict_parser(key, value, item_list):
    new_dict = {}
    for item in item_list:
        field_key = item.get(key)
        new_dict[field_key] = item.get(value)

    return new_dict

def parse_fields_from_business_object(field_list):
    new_business_obj = cherwell_dict_parser('name', 'value', field_list)

    return new_business_obj

def parse_fields_from_business_object_list(response):
    object_list = []
    if not response.get('businessObjects'):
        return []
    for business_obj in response.get('businessObjects'):
        new_business_obj = parse_fields_from_business_object(business_obj.get('fields'))
        new_business_obj['BusinessObjectId'] = business_obj.get('busObId')
        new_business_obj['PublicId'] = business_obj.get('busObPublicId')
        new_business_obj['RecordId'] = business_obj.get('busObRecId')
        object_list.append(new_business_obj)

    return object_list

def build_fields_for_business_object(data_dict, ids_dict):
    fields = []
    for key, value in data_dict.items():
        new_field = {
            "dirty": "true",
            "fieldId": ids_dict.get(key),
            "name": key,
            "value": value
        }
        fields.append(new_field)
    return fields

def http_request(method, url, payload, token=None, custom_headers=None, is_fetch=False):
    headers = build_headers(token, custom_headers)
    try:
        response = requests.request(method, url, data=payload, headers=headers, verify=SECURED)
    except requests.exceptions.ConnectionError as e:
        err_message = f'Error connecting to server. Check your URL/Proxy/Certificate settings: {e}'
        raise_or_return_error(err_message, is_fetch)
    return response

def request_new_access_token(using_refresh):
    url = BASE_URL + "token"
    refresh_token = demisto.getIntegrationContext().get('refresh_token')

    if using_refresh:
        payload = f'client_id={CLIENT_ID}&grant_type=refresh_token&refresh_token={refresh_token}'
    else:
        payload = f'client_id={CLIENT_ID}&grant_type=password&username={USERNAME}&password={PASSWORD}'

    headers = {
        'Accept': "application/json",
        'Content-Type': "application/x-www-form-urlencoded",
    }

    response = http_request('POST', url, payload, custom_headers=headers)
    return response

def get_new_access_token(is_fetch=False):
    response = request_new_access_token(True)
    if not response.status_code == HTTP_CODES['success']:
        response = request_new_access_token(False)
    res_json = parse_response(response,
                              "Could not get token. Check your credentials (user/password/client id) and try again",
                              is_fetch=is_fetch)
    token_expiration_time = int(date_to_timestamp(res_json.get('.expires'), '%a, %d %b %Y %H:%M:%S GMT'))
    demisto.setIntegrationContext({
        'refresh_token': res_json.get('refresh_token'),
        'token_expiration_time': token_expiration_time,
        'access_token': res_json.get('access_token')
    })
    return res_json.get('access_token')

def get_access_token(new_token, is_fetch=False):
    integration_context = demisto.getIntegrationContext()
    token_expiration_time = integration_context.get('token_expiration_time')
    current_time = date_to_timestamp(datetime.utcnow())
    if new_token or not token_expiration_time or token_expiration_time < current_time:
        token = get_new_access_token(is_fetch=is_fetch)
        return token
    else:
        return integration_context.get('access_token')

def build_headers(token, headers=None):
    headers = headers if headers else HEADERS
    headers['Authorization'] = f'Bearer {token}'
    return headers

def make_request(method, url, payload=None, headers=None, is_fetch=False):
    token = get_access_token(False, is_fetch=is_fetch)
    response = http_request(method, url, payload, token, custom_headers=headers, is_fetch=is_fetch)
    if response.status_code == HTTP_CODES['unauthorized']:
        token = get_access_token(True, is_fetch=is_fetch)
        response = http_request(method, url, payload, token, custom_headers=headers, is_fetch=is_fetch)
    return response

def get_business_object_summary_by_name(name, is_fetch=False):
    url = BASE_URL + f'api/V1/getbusinessobjectsummary/busobname/{name}'
    response = make_request('GET', url, is_fetch=is_fetch)
    return parse_response(response, "Could not get business object summary", is_fetch=is_fetch)

def get_business_object_summary_by_id(_id, is_fetch=False):
    url = BASE_URL + f'api/V1/getbusinessobjectsummary/busobid/{_id}'
    response = make_request('GET', url, is_fetch=is_fetch)
    return parse_response(response, "Could not get business object summary", is_fetch=is_fetch)

def resolve_business_object_id_by_name(name, is_fetch=False):
    res = get_business_object_summary_by_name(name, is_fetch)
    if not res:
        err_message = f'Could not retrieve "{name}" business object id. Make sure "{name}" is a valid business object.'
        raise_or_return_error(err_message, is_fetch)
    return res[0].get('busObId')

def save_business_object(payload):
    url = BASE_URL + "api/V1/savebusinessobject"
    response = make_request("POST", url, json.dumps(payload))
    return parse_response(response, "Could not save business object")

def get_business_object_record(business_object_id, object_id, id_type):
    id_type_str = 'publicid' if id_type == 'public_id' else 'busobrecid'
    url = BASE_URL + f'api/V1/getbusinessobject/busobid/{business_object_id}/{id_type_str}/{object_id}'
    response = make_request("GET", url)
    return parse_response(response, "Could not get business objects")

def delete_business_object_record(business_object_id, object_id, id_type):
    id_type_str = 'publicid' if id_type == 'public_id' else 'busobrecid'
    url = BASE_URL + f'api/V1/deletebusinessobject/busobid/{business_object_id}/{id_type_str}/{object_id}'
    response = make_request("DELETE", url)
    return parse_response(response, "Could not delete business object")

def get_search_results(payload, is_fetch=False):
    url = BASE_URL + "api/V1/getsearchresults"
    response = make_request("POST", url, json.dumps(payload))
    return parse_response(response, "Could not search for business objects", is_fetch=is_fetch)

def get_business_object_template(business_object_id, include_all=True, field_names=None, fields_ids=None,
                                 is_fetch=False):
    url = BASE_URL + "api/V1/getbusinessobjecttemplate"
    payload = {
        "busObId": business_object_id,
        "includeAll": include_all
    }

    if field_names:
        payload['fieldNames'] = field_names
    if fields_ids:
        payload['fieldIds'] = fields_ids
    response = make_request("POST", url, json.dumps(payload), is_fetch=is_fetch)
    return parse_response(response, "Could not get business object template", is_fetch=is_fetch)

def build_business_object_json(simple_json, business_object_id, object_id=None, id_type=None):
    business_object_ids_dict = get_key_value_dict_from_template('name', 'fieldId', business_object_id)
    fields_for_business_object = build_fields_for_business_object(simple_json, business_object_ids_dict)
    business_object_json = {
        'busObId': business_object_id,
        "fields": fields_for_business_object
    }
    if object_id:
        id_key = 'busObPublicId' if id_type == 'public_id' else 'busObRecId'
        business_object_json[id_key] = object_id
    return business_object_json

def create_business_object(name, data_json):
    business_object_id = resolve_business_object_id_by_name(name)
    business_object_json = build_business_object_json(data_json, business_object_id)
    return save_business_object(business_object_json)

def update_business_object(name, data_json, object_id, id_type):
    business_object_id = resolve_business_object_id_by_name(name)
    business_object_json = build_business_object_json(data_json, business_object_id, object_id, id_type)
    return save_business_object(business_object_json)

def get_business_object(name, object_id, id_type):
    business_object_id = resolve_business_object_id_by_name(name)
    results = get_business_object_record(business_object_id, object_id, id_type)
    parsed_business_object = parse_fields_from_business_object(results.get('fields'))
    parsed_business_object['PublicId'] = results.get('busObPublicId')
    parsed_business_object['RecordId'] = results.get('busObRecId')
    return parsed_business_object, results

def delete_business_object(name, object_id, id_type):
    business_object_id = resolve_business_object_id_by_name(name)
    return delete_business_object_record(business_object_id, object_id, id_type)

def download_attachment_from_business_object(attachment, is_fetch):
    attachment_id = attachment.get('attachmentId')
    business_object_id = attachment.get('busObId')
    business_record_id = attachment.get('busObRecId')
    url = BASE_URL + f'api/V1/getbusinessobjectattachment' \
        f'/attachmentid/{attachment_id}/busobid/{business_object_id}/busobrecid/{business_record_id}'
    response = make_request('GET', url, is_fetch=is_fetch)
    return parse_response(response, f'Unable to get content of attachment {attachment_id}', file_content=True,
                          is_fetch=is_fetch)

def get_attachments_content(attachments_to_download, is_fetch):
    attachments = []
    for attachment in attachments_to_download:
        new_attachment = {
            'FileName': attachment.get('displayText'),
            'CreatedAt': attachment.get('created'),
            'Content': download_attachment_from_business_object(attachment, is_fetch=is_fetch)
        }
        attachments.append(new_attachment)
    return attachments

def get_attachments_details(id_type, object_id, object_type_name, object_type_id, type, attachment_type,
                            is_fetch=False):
    id_type_str = 'publicid' if id_type == 'public_id' else 'busobrecid'
    business_object_type_str = 'busobid' if object_type_id else 'busobname'
    object_type = object_type_id if object_type_id else object_type_name
    url = BASE_URL + f'api/V1/getbusinessobjectattachments/' \
        f'{business_object_type_str}/{object_type}/' \
        f'{id_type_str}/{object_id}' \
        f'/type/{type}' \
        f'/attachmenttype/{attachment_type}'
    response = make_request('GET', url, is_fetch=is_fetch)
    return parse_response(response, f'Unable to get attachments for {object_type} {object_id}', is_fetch=is_fetch)

def download_attachments(id_type, object_id, business_object_type_name=None, business_object_type_id=None,
                         is_fetch=False):
    type = 'File'
    attachment_type = 'Imported'
    result = get_attachments_details(id_type, object_id, business_object_type_name, business_object_type_id, type,
                                     attachment_type, is_fetch=is_fetch)
    attachments_to_download = result.get('attachments')
    if not attachments_to_download:
        return
    return get_attachments_content(attachments_to_download, is_fetch=is_fetch)

def get_attachments_info(id_type, object_id, attachment_type, business_object_type_name=None,
                         business_object_type_id=None):
    type = 'File'
    result = get_attachments_details(id_type, object_id, business_object_type_name, business_object_type_id, type,
                                     attachment_type)
    attachments = result.get('attachments')
    attachments_info = [{
        'AttachmentFiledId': attachment.get('attachmentFileId'),
        'FileName': attachment.get('displayText'),
        'AttachmentId': attachment.get('attachmentId'),
        'BusinessObjectType': business_object_type_name,
        f'BusinessObject{string_to_context_key(id_type)}': object_id

    } for attachment in attachments]
    return attachments_info, result

def attachment_results(attachments):
    attachments_file_results = []
    for attachment in attachments:
        attachment_content = attachment.get('Content')
        attachment_name = attachment.get('FileName')
        attachments_file_results.append(fileResult(attachment_name, attachment_content))
    return attachments_file_results

def run_query_on_business_objects(bus_id, filter_query, max_results, is_fetch):
    payload = {
        'busObId': bus_id,
        'includeAllFields': True,
        'filters': filter_query
    }
    if max_results:
        payload['pageSize'] = max_results
    return get_search_results(payload, is_fetch=is_fetch)

def get_key_value_dict_from_template(key, val, business_object_id, is_fetch=False):
    template_dict = get_business_object_template(business_object_id, is_fetch=is_fetch)
    return cherwell_dict_parser(key, val, template_dict.get('fields'))

def get_all_incidents(objects_names, last_created_time, max_results, query_string, real_fetch):
    all_incidents: list = []
    for business_object_name in objects_names:
        business_object_id = resolve_business_object_id_by_name(business_object_name, is_fetch=real_fetch)
        query_list = [['CreatedDateTime', 'gt', last_created_time]]
        if query_string:
            additional_query_list = validate_query_for_fetch_incidents(objects_names, query_string, real_fetch)
            query_list += additional_query_list
        incidents, _ = query_business_object(query_list, business_object_id, max_results, is_fetch=real_fetch)
        all_incidents += incidents
    sorted_incidents = sorted(all_incidents, key=lambda incident: incident.get('CreatedDateTime'))
    return sorted_incidents[:max_results]

def object_to_incident(obj):
    attachments_list = []
    attachments = obj.get('Attachments')
    if attachments:
        obj.pop('Attachments')
        for attachment in attachments:
            file_name = attachment.get('FileName')
            attachment_file = fileResult(file_name, attachment.get('Content'))
            attachments_list.append({
                'path': attachment_file.get('FileID'),
                'name': file_name
            })
    item = {
        'name': f'Record ID:{obj.get("RecID")}',
        'attachment': attachments_list,
        'rawJSON': json.dumps(obj)
    }

    return createContext(item, removeNull=True)

def save_incidents(objects_to_save):
    final_incidents = []
    for obj in objects_to_save:
        final_incidents.append(object_to_incident(obj))
    demisto.incidents(final_incidents)
    return

def fetch_incidents_attachments(incidents, is_fetch):
    for incident in incidents:
        rec_id = incident.get('RecID')
        business_object_id = incident.get('BusinessObjectId')
        incident['Attachments'] = []
        attachments = download_attachments('record_id', rec_id, business_object_type_id=business_object_id,
                                           is_fetch=is_fetch)
        if attachments:
            for attachment in attachments:
                new_attachment_obj = {
                    'Content': attachment.get('Content'),
                    'FileName': attachment.get('FileName')
                }
                incident['Attachments'].append(new_attachment_obj)
    return incidents

def validate_params_for_fetch(max_result, objects_to_fetch, real_fetch):
    # Check that max result is positive integer
    try:
        max_result = int(max_result)
        if max_result < 0:
            raise ValueError
    except ValueError:
        max_result_err_message = 'Max results to fetch must be a number grater than 0'
        raise_or_return_error(max_result_err_message, real_fetch)
    # Make sure that there are objects to fetch
    if len(objects_to_fetch) == 0:
        objects_to_fetch_err_message = 'No objects to fetch were given'
        raise_or_return_error(objects_to_fetch_err_message, real_fetch)
    return

def fetch_incidents(objects_names, fetch_time, max_results, query_string, fetch_attachments, real_fetch=False):
    validate_params_for_fetch(max_results, objects_names, real_fetch)
    max_results = int(max_results)
    last_run = demisto.getLastRun()
    last_objects_fetched = last_run.get('objects_names_to_fetch')
    if 'last_created_time' in last_run and last_objects_fetched == objects_names:
        last_created_time = last_run.get('last_created_time')
    else:
        try:
            last_created_time, _ = parse_date_range(fetch_time, date_format=DATE_FORMAT, to_timestamp=False)
        except ValueError:
            error_message = f'First fetch time stamp should be of the form: <number> <time unit>, e.g., 12 hours, ' \
                f'7 days. Received: "{fetch_time}"'
            raise_or_return_error(error_message, real_fetch)
    incidents = get_all_incidents(objects_names, last_created_time, max_results, query_string, real_fetch)
    if fetch_attachments:
        incidents = fetch_incidents_attachments(incidents, real_fetch)
    if real_fetch:
        save_incidents(incidents)
    return incidents

def upload_business_object_attachment(file_name, file_size, file_content, object_type_name, id_type, object_id, ):
    id_type_str = 'publicid' if id_type == 'public_id' else 'busobrecid'
    url = BASE_URL + f'/api/V1/uploadbusinessobjectattachment/' \
        f'filename/{file_name}/busobname/{object_type_name}/{id_type_str}/{object_id}/offset/0/totalsize/{file_size}'
    payload = file_content
    headers = HEADERS
    headers['Content-Type'] = "application/octet-stream"
    response = make_request('POST', url, payload, headers)
    return parse_response(response, f'Could not upload attachment {file_name}')

def upload_attachment(id_type, object_id, type_name, file_entry_id):
    file_data = demisto.getFilePath(file_entry_id)
    file_path = file_data.get('path')
    file_name = file_data.get('name')
    try:
        file_size = os.path.getsize(file_path)
        with open(file_path, 'rb') as f:
            file_content = f.read()
        attachment_id = upload_business_object_attachment(file_name, file_size, file_content, type_name, id_type,
                                                          object_id)
        return attachment_id
    except Exception as err:
        return_error(f'unable to open file: {err}')

def remove_attachment(id_type, object_id, type_name, attachment_id):
    id_type_str = 'publicid' if id_type == 'public_id' else 'busobrecid'
    url = BASE_URL + f'/api/V1/removebusinessobjectattachment/' \
        f'attachmentid/{attachment_id}/busobname/{type_name}/{id_type_str}/{object_id}'
    response = make_request('DELETE', url)
    parse_response(response, f'Could not remove attachment {attachment_id} from {type_name} {object_id}')
    return

def link_related_business_objects(action, parent_business_object_id, parent_business_object_record_id, relationship_id,
                                  business_object_id, business_object_record_id):
    url_action_str = 'linkrelatedbusinessobject' if action == 'link' else 'unlinkrelatedbusinessobject'
    url = BASE_URL + f"api/V1/{url_action_str}/parentbusobid/{parent_business_object_id}" \
        f"/parentbusobrecid/{parent_business_object_record_id}" \
        f"/relationshipid/{relationship_id}" \
        f"/busobid/{business_object_id}" \
        f"/busobrecid/{business_object_record_id}"
    http_method = 'GET' if action == 'link' else 'DELETE'
    response = make_request(http_method, url)
    parse_response(response, "Could not link business objects")
    return

def business_objects_relation_action(action, parent_type_name, parent_record_id, child_type_name, child_record_id,
                                     relationship_id):
    parent_business_object_id = resolve_business_object_id_by_name(parent_type_name)
    child_business_object_id = resolve_business_object_id_by_name(child_type_name)
    link_related_business_objects(action, parent_business_object_id, parent_record_id, relationship_id,
                                  child_business_object_id, child_record_id)
    return

def validate_query_list(query_list, is_fetch):
    for index, query in enumerate(query_list):
        if not len(query) == 3:
            length_err_message = f'Cannot parse query, should be of the form: `[["FieldName","Operator","Value"],' \
                f'["FieldName","Operator","Value"],...]`. Filter in index {index} is malformed: {query}'
            raise_or_return_error(length_err_message, is_fetch)
        if query[1] not in QUERY_OPERATORS:
            operator_err_message = f'Operator should be one of the following: {", ".join(QUERY_OPERATORS)}. Filter in' \
                f' index {index}, was: {query[1]}'
            raise_or_return_error(operator_err_message, is_fetch)
    return

def validate_query_for_fetch_incidents(objects_names, query_string, real_fetch):
    if not objects_names:
        no_objects_err_message = 'No business object name was given. \n In order to run advanced query, ' \
            'fill the integration parameter-`Objects to fetch` with exactly one business object name.'
        raise_or_return_error(no_objects_err_message, real_fetch)
    if len(objects_names) > 1:
        multiple_objects_error_message = f'Advanced query operation is supported for a single business object. ' \
            f'{len(objects_names)} objects were given: {",".join(objects_names)}'
        raise_or_return_error(multiple_objects_error_message, real_fetch)
    return parse_string_query_to_list(query_string, real_fetch)

def build_query_dict(query, filed_ids_dict, is_fetch):
    field_name = query[0]
    operator = query[1]
    value = query[2]
    field_id = filed_ids_dict.get(field_name)
    if not field_id:
        err_message = f'Field name: {field_name} does not exit in the given business objects'
        raise_or_return_error(err_message, is_fetch)
    return {
        'fieldId': filed_ids_dict.get(field_name),
        'operator': operator,
        'value': value
    }

def build_query_dict_list(query_list, filed_ids_dict, is_fetch):
    query_dict_list = []
    for query in query_list:
        query_dict = build_query_dict(query, filed_ids_dict, is_fetch)
        query_dict_list.append(query_dict)
    return query_dict_list

def query_business_object(query_list, business_object_id, max_results, is_fetch=False):
    filed_ids_dict = get_key_value_dict_from_template('name', 'fieldId', business_object_id, is_fetch=is_fetch)
    filters = build_query_dict_list(query_list, filed_ids_dict, is_fetch=is_fetch)
    query_result = run_query_on_business_objects(business_object_id, filters, max_results, is_fetch=is_fetch)
    business_objects = parse_fields_from_business_object_list(query_result)
    return business_objects, query_result

def parse_string_query_to_list(query_string, is_fetch=False):
    try:
        query_list = json.loads(query_string)
    except (ValueError, TypeError):
        err_message = 'Cannot parse query, should be of the form: `[["FieldName","Operator","Value"],' \
            '["FieldName","Operator","Value"]]`.'
        raise_or_return_error(err_message, is_fetch)
    validate_query_list(query_list, is_fetch)
    return query_list

def query_business_object_string(business_object_name, query_string, max_results):
    if max_results:
        try:
            int(max_results)
        except ValueError:
            return return_error('`max_results` argument received is not a number')
    business_object_id = resolve_business_object_id_by_name(business_object_name)
    query_filters_list = parse_string_query_to_list(query_string)
    return query_business_object(query_filters_list, business_object_id, max_results)

def get_field_info(type, field_property):
    business_object_id = resolve_business_object_id_by_name(type)
    template = get_business_object_template(business_object_id)
    business_object_fields = template.get('fields')
    field_to_return = None
    for field in business_object_fields:
        if field.get('displayName') == field_property or \
                field.get('fieldId') == field_property or \
                field.get('name') == field_property:
            field_to_return = field
    if field_to_return:
        field_to_return = {
            'DisplayName': field_to_return.get('displayName'),
            'Name': field_to_return.get('name'),
            'FieldId': field_to_return.get('fieldId')
        }
    else:
        return_error(f'Field with the value {field_property} was not found')
    return field_to_return

def cherwell_run_saved_search(association_id, scope, scope_owner, search_name):
    search_payload = {
        "Association": association_id,
        "scope": scope,
        "scopeOwner": scope_owner,
        "searchName": search_name,
        "includeAllFields": True,
    }

    results = get_search_results(search_payload)
    return parse_fields_from_business_object_list(results)

def cherwell_get_business_object_id(business_object_name):
    business_object_id = resolve_business_object_id_by_name(business_object_name)
    business_object_info = {
        'BusinessObjectId': business_object_id,
        'BusinessObjectName': business_object_name
    }
    return business_object_info

def raise_or_return_error(msg, raise_flag):
    """
    This function handles errors occurred in functions that are within the fetch incidents flow.
    If the error occurred as part of a fetch-incidents flow then an exception will be thrown otherwise a regular error
    entry will be returned.
    This is needed when running fetch-incidents process since regular error entries are not handled correctly by the
    server
    :param msg: error msg to raise/return
    :param raise_flag: if true should raise, otherwise throw
    """
    if raise_flag:
        raise Exception(msg)
    else:
        return_error(msg)

def get_one_step_actions(bus_id, is_fetch=False):
    url = BASE_URL + f'api/V1/getonestepactions/association/{bus_id}'
    response = make_request('GET', url, is_fetch=is_fetch)
    return parse_response(response, "Could not get one step actions", is_fetch=is_fetch)

def get_one_step_actions_recursive(root, actions):
    if root.get('childItems'):
        actions_list = []
        for item in root.get('childItems'):
            actions_list.append(item)
        actions[root.get('name')] = actions_list

    for folder in root.get('childFolders', []):
        get_one_step_actions_recursive(folder, actions)
    return actions

def run_one_step_action(payload):
    url = BASE_URL + "api/V1/runonestepaction"
    response = make_request("POST", url, json.dumps(payload))
    return parse_response(response, "Could not run one step action")

########################################################################################################################

'''
Commands
'''

def test_command():
    if FETCHES_INCIDENTS:
        fetch_incidents(OBJECTS_TO_FETCH, FETCH_TIME, MAX_RESULT, QUERY_STRING, FETCH_ATTACHMENTS)
    else:
        get_access_token(True)
    return

def create_business_object_command():
    args = demisto.args()
    type_name = args.get('type')
    data_json = json.loads(args.get('json'))
    result = create_business_object(type_name, data_json)
    ids = {
        'PublicId': result.get('busObPublicId'),
        'RecordId': result.get('busObRecId')
    }
    md = tableToMarkdown(f'New {type_name.capitalize()} was created', ids, headerTransform=pascalToSpace)

    return {
        'Type': entryTypes['note'],
        'ContentsFormat': formats['json'],
        'Contents': result,
        'HumanReadable': md,
        'EntryContext': {
            'Cherwell.BusinessObjects(val.RecordId == obj.RecordId)': ids
        }
    }

def update_business_object_command():
    args = demisto.args()
    type_name = args.get('type')
    data_json = json.loads(args.get('json'))
    object_id = args.get('id_value')
    id_type = args.get('id_type')
    result = update_business_object(type_name, data_json, object_id, id_type)
    ids = {
        'PublicId': result.get('busObPublicId'),
        'RecordId': result.get('busObRecId')
    }
    md = tableToMarkdown(f'{type_name.capitalize()} {object_id} was updated', ids, headerTransform=pascalToSpace)

    return {
        'Type': entryTypes['note'],
        'ContentsFormat': formats['json'],
        'Contents': result,
        'HumanReadable': md,
        'EntryContext': {
            'Cherwell.BusinessObjects(val.RecordId == obj.RecordId)': ids
        }
    }

def get_business_object_command():
    args = demisto.args()
    type_name = args.get('type')
    id_type = args.get('id_type')
    object_id = args.get('id_value')
    business_object, results = get_business_object(type_name, object_id, id_type)
    md = tableToMarkdown(f'{type_name.capitalize()}: {object_id}', business_object,
                         headerTransform=pascalToSpace)

    return {
        'Type': entryTypes['note'],
        'ContentsFormat': formats['json'],
        'Contents': results,
        'HumanReadable': md,
        'EntryContext': {
            'Cherwell.BusinessObjects(val.RecordId == obj.RecordId)': createContext(business_object)
        }
    }

def delete_business_object_command():
    args = demisto.args()
    type_name = args.get('type')
    id_type = args.get('id_type')
    object_id = args.get('id_value')
    results = delete_business_object(type_name, object_id, id_type)
    md = f'### Record {object_id} of type {type_name} was deleted.'

    return {
        'Type': entryTypes['note'],
        'ContentsFormat': formats['json'],
        'Contents': results,
        'HumanReadable': md
    }

def fetch_incidents_command():
    objects_names_to_fetch = OBJECTS_TO_FETCH
    fetch_attachments = FETCH_ATTACHMENTS
    max_result = MAX_RESULT
    fetch_time = FETCH_TIME
    query_string = QUERY_STRING
    incidents = fetch_incidents(objects_names_to_fetch, fetch_time, max_result, query_string, fetch_attachments,
                                real_fetch=True)
    if incidents:
        last_incident_created_time = incidents[-1].get('CreatedDateTime')
        next_created_time_to_fetch = \
            (datetime.strptime(last_incident_created_time, DATE_FORMAT) + timedelta(seconds=1)).strftime(DATE_FORMAT)
        demisto.setLastRun({
            'last_created_time': next_created_time_to_fetch,
            'objects_names_to_fetch': objects_names_to_fetch
        })
    return

def download_attachments_command():
    args = demisto.args()
    id_type = args.get('id_type')
    object_id = args.get('id_value')
    type_name = args.get('type')
    attachments = download_attachments(id_type, object_id, business_object_type_name=type_name)
    if not attachments:
        return_error(f'No attachments were found for {type_name}:{object_id}')
    return attachment_results(attachments)

def upload_attachment_command():
    args = demisto.args()
    id_type = args.get('id_type')
    object_id = args.get('id_value')
    type_name = args.get('type')
    file_entry_id = args.get('file_entry_id')
    attachment_id = upload_attachment(id_type, object_id, type_name, file_entry_id)
    entry_context = {
        'AttachmentFileId': attachment_id,
        'BusinessObjectType': type_name,
        string_to_context_key(id_type): object_id
    }
    md = f'### Attachment: {attachment_id}, was successfully attached to {type_name} {object_id}'
    return {
        'Type': entryTypes['note'],
        'ContentsFormat': formats['text'],
        'Contents': {'attachment_id': attachment_id},
        'EntryContext': {'Cherwell.UploadedAttachments(val.AttachmentId == obj.AttachmentId)': entry_context},
        'HumanReadable': md,
    }

def remove_attachment_command():
    args = demisto.args()
    id_type = args.get('id_type')
    object_id = args.get('id_value')
    type_name = args.get('type')
    attachment_id = args.get('attachment_id')
    remove_attachment(id_type, object_id, type_name, attachment_id)
    md = f'### Attachment: {attachment_id}, was successfully removed from {type_name} {object_id}'
    return {
        'Type': entryTypes['note'],
        'ContentsFormat': formats['text'],
        'Contents': md,
        'HumanReadable': md,
    }

def get_attachments_info_command():
    args = demisto.args()
    id_type = args.get('id_type')
    object_id = args.get('id_value')
    type_name = args.get('type')
    attachment_type = args.get('attachment_type')
    attachments_info, raw_result = get_attachments_info(id_type, object_id, attachment_type,
                                                        business_object_type_name=type_name)
    md = tableToMarkdown(f'{type_name.capitalize()} {object_id} attachments:', attachments_info,
                         headerTransform=pascalToSpace) if attachments_info \
        else f'### {type_name.capitalize()} {object_id} has no attachments'

    entry = {
        'Type': entryTypes['note'],
        'ContentsFormat': formats['text'],
        'Contents': raw_result,
        'HumanReadable': md
    }
    if attachments_info:
        entry['EntryContext'] = {
            'Cherwell.AttachmentsInfo': attachments_info}
    return entry

def link_business_objects_command():
    args = demisto.args()
    parent_type = args.get('parent_type')
    parent_record_id = args.get('parent_record_id')
    child_type = args.get('child_type')
    child_record_id = args.get('child_record_id')
    relationship_id = args.get('relationship_id')
    business_objects_relation_action('link', parent_type, parent_record_id, child_type, child_record_id,
                                     relationship_id)
    message = \
        f'{parent_type.capitalize()} {parent_record_id} and {child_type.capitalize()} {child_record_id} were linked'
    md = f'### {message}'
    return {
        'Type': entryTypes['note'],
        'ContentsFormat': formats['text'],
        'Contents': message,
        'HumanReadable': md,
    }

def unlink_business_objects_command():
    args = demisto.args()
    parent_type = args.get('parent_type')
    parent_record_id = args.get('parent_record_id')
    child_type = args.get('child_type')
    child_record_id = args.get('child_record_id')
    relationship_id = args.get('relationship_id')
    business_objects_relation_action('unlink', parent_type, parent_record_id, child_type, child_record_id,
                                     relationship_id)
    message = \
        f'{parent_type.capitalize()} {parent_record_id} and {child_type.capitalize()} {child_record_id} were unlinked'
    md = f'### {message}'
    return {
        'Type': entryTypes['note'],
        'ContentsFormat': formats['text'],
        'Contents': message,
        'HumanReadable': md,
    }

def query_business_object_command():
    args = demisto.args()
    type_name = args.get('type')
    query_string = args.get('query')
    max_results = args.get('max_results')
    results, raw_response = query_business_object_string(type_name, query_string, max_results)
    md = tableToMarkdown('Query Results', results, headerTransform=pascalToSpace)
    return {
        'Type': entryTypes['note'],
        'ContentsFormat': formats['text'],
        'Contents': raw_response,
        'EntryContext': {'Cherwell.QueryResults': results},
        'HumanReadable': md,
    }

def get_field_info_command():
    args = demisto.args()
    type_name = args.get('type')
    field_property = args.get('field_property')
    results = get_field_info(type_name, field_property)
    md = tableToMarkdown('Field info:', results, headerTransform=pascalToSpace)
    return {
        'Type': entryTypes['note'],
        'ContentsFormat': formats['text'],
        'Contents': results,
        'EntryContext': {'Cherwell.FieldInfo(val.FieldId == obj.FieldId)': results},
        'HumanReadable': md
    }

def cherwell_run_saved_search_command():
    args = demisto.args()
    association_id = args.get('association_id')
    scope = args.get('scope')
    scope_owner = args.get('scope_owner')
    search_name = args.get('search_name')
    results = cherwell_run_saved_search(association_id, scope, scope_owner, search_name)
    md = tableToMarkdown(f'{search_name} results:', results, headerTransform=pascalToSpace)
    return {
        'Type': entryTypes['note'],
        'ContentsFormat': formats['text'],
        'Contents': results,
        'EntryContext': {'Cherwell.SearchOperation(val.RecordId == obj.RecordId)': results},
        'HumanReadable': md
    }

def cherwell_get_business_object_id_command():
    args = demisto.args()
    business_object_name = args.get('business_object_name')
    result = cherwell_get_business_object_id(business_object_name)
    md = tableToMarkdown('Business Object Info:', result, headerTransform=pascalToSpace)
    return {
        'Type': entryTypes['note'],
        'ContentsFormat': formats['text'],
        'Contents': result,
        'EntryContext': {'Cherwell.BusinessObjectInfo(val.BusinessObjectId == obj.BusinessObjectId)': result},
        'HumanReadable': md
    }

def cherwell_get_business_object_summary_command():
    args = demisto.args()
    business_object_name = args.get('name')
    business_object_id = args.get('id')

    if not business_object_id and not business_object_name:
        raise DemistoException('No name or ID were specified. Please specify at least one of them.')
    elif business_object_id:
        result = get_business_object_summary_by_id(business_object_id)
    else:
        result = get_business_object_summary_by_name(business_object_name)

    md = tableToMarkdown('Business Object Summary:', result, headerTransform=pascalToSpace)

    return CommandResults(outputs=result, readable_output=md, outputs_key_field='busObId',
                          outputs_prefix='Cherwell.BusinessObjectSummary', raw_response=result)

def cherwell_get_one_step_actions_command():
    args = demisto.args()
    business_object_id = args.get('busobjectid')
    result = get_one_step_actions(business_object_id)

    actions: dict = {}
    ec = {}
    md = ''

    get_one_step_actions_recursive(result.get('root'), actions)

    if actions:
        for key, action in actions.items():
            md += tableToMarkdown(f'{key} one-step actions:', action,
                                  headerTransform=pascalToSpace, headers=ONE_STEP_ACTION_HEADERS)
        ec = {'BusinessObjectId': business_object_id, 'Actions': actions}
    else:
        md = f'No one-step actions found for business object ID {business_object_id}'

    return CommandResults(outputs=ec, readable_output=md, outputs_key_field='BusinessObjectId',
                          outputs_prefix='Cherwell.OneStepActions', raw_response=result)

def cherwell_run_one_step_action_command():
    prompt_values = {}
    args = demisto.args()
    business_object_id = args.get('busobjectid')
    rec_id = args.get('busobrecid')
    stand_in_key = args.get('oneStepAction_StandInKey')
    prompt_values_arg = args.get('prompt_values')

    if prompt_values_arg:
        prompt_values = json.loads(prompt_values_arg)

    payload = {'acquireLicense': True, 'busObId': business_object_id, 'busObRecId': rec_id,
               'oneStepActionStandInKey': stand_in_key, 'promptValues': prompt_values}

    result = run_one_step_action(payload)

    return CommandResults(readable_output='One-Step action has been executed successfully.', raw_response=result)

#######################################################################################################################

def main():
    global FETCHES_INCIDENTS, FETCH_TIME, FETCH_ATTACHMENTS, OBJECTS_TO_FETCH, MAX_RESULT, USERNAME, PASSWORD, SERVER, \
        SECURED, CLIENT_ID, QUERY_STRING, DATE_FORMAT, BASE_URL

    params = demisto.params()

    FETCHES_INCIDENTS = params.get('isFetch')
    FETCH_TIME = params.get('fetch_time')
    FETCH_ATTACHMENTS = params.get('fetch_attachments')
    OBJECTS_TO_FETCH = params.get('objects_to_fetch').split(',')
    MAX_RESULT = params.get('max_results')
    USERNAME = params.get('credentials').get('identifier')
    PASSWORD = params.get('credentials').get('password')
    # Remove trailing slash to prevent wrong URL path to service
    SERVER = params['url'][:-1] if (params['url'] and params['url'].endswith('/')) else params['url']
    SECURED = not params.get('insecure')
    CLIENT_ID = params.get('client_id')
    QUERY_STRING = params.get('query_string')
    DATE_FORMAT = '%m/%d/%Y %I:%M:%S %p'
    # Service base URL
    BASE_URL = SERVER + '/CherwellAPI/'

    try:
        handle_proxy()

        command = demisto.command()
        demisto.debug(f'Command being called is {command}')

        commands = {
            'cherwell-create-business-object': create_business_object_command,
            'cherwell-update-business-object': update_business_object_command,
            'cherwell-get-business-object': get_business_object_command,
            'cherwell-delete-business-object': delete_business_object_command,
            'cherwell-download-attachments': download_attachments_command,
            'cherwell-get-attachments-info': get_attachments_info_command,
            'cherwell-upload-attachment': upload_attachment_command,
            'cherwell-remove-attachment': remove_attachment_command,
            'cherwell-link-business-objects': link_business_objects_command,
            'cherwell-unlink-business-objects': unlink_business_objects_command,
            'cherwell-query-business-object': query_business_object_command,
            'cherwell-get-field-info': get_field_info_command,
            'cherwell-run-saved-search': cherwell_run_saved_search_command,
            'cherwell-get-business-object-id': cherwell_get_business_object_id_command,
            'cherwell-get-business-object-summary': cherwell_get_business_object_summary_command,
            'cherwell-get-one-step-actions-for-business-object': cherwell_get_one_step_actions_command,
            'cherwell-run-one-step-action-on-business-object': cherwell_run_one_step_action_command
        }
        if command == 'test-module':
            test_command()
            demisto.results('ok')
        elif command == 'fetch-incidents':
            fetch_incidents_command()
        elif command in commands:
            return_results(commands[command]())
        else:
            raise NotImplementedError(f'{command} command is not implemented.')

    # Log exceptions
    except Exception as e:
        message = f'Unexpected error: {e}.'
        return_error(message, error=traceback.format_exc())

if __name__ in ['__main__', 'builtin', 'builtins']:
    main()


text:
layout: page
title: Growth Air Seminar
date: 2016-05-24
author: Theresa Beasley
tags: weekly links, java
status: published
summary: In condimentum orci hendrerit tortor aliquam congue. Praesent tellus.
banner: images/banner/wedding.jpg
booking:
  startDate: 05/24/2017
  endDate: 05/28/2017
  ctyhocn: GFIMAHX
  groupCode: GAS
published: true
---
Aliquam efficitur, tellus vel consectetur tristique, nibh ante posuere magna, sed interdum nisi mi eu arcu. Nunc et molestie felis. Pellentesque iaculis posuere libero vel convallis. Nunc ultricies ligula eu rhoncus pretium. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Quisque nec massa felis. Mauris nec elementum lectus. In pretium vulputate ligula id dictum. Integer viverra malesuada diam, non maximus leo eleifend ac.

1 Proin tincidunt nibh tincidunt vulputate volutpat
1 Phasellus bibendum felis eu eros consequat, hendrerit fringilla turpis pellentesque
1 Mauris a nisi feugiat, scelerisque lacus at, congue eros
1 Praesent et diam a nisl ultricies mollis id nec nisl
1 Nunc id justo lobortis, iaculis justo ac, placerat mauris
1 Sed ac felis eget sapien maximus pretium vel at tellus.

Etiam sed ligula vitae velit auctor hendrerit et at dolor. Pellentesque tortor urna, feugiat ut ultrices in, placerat eget neque. Aliquam in scelerisque arcu. Integer sit amet dapibus sapien, interdum pharetra tortor. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Aliquam sit amet arcu magna. Mauris ut sem diam. Aliquam condimentum odio in lobortis egestas. Donec interdum ultricies dolor et placerat. Curabitur vestibulum lectus urna, eu blandit augue volutpat non. Quisque eros sem, molestie eu ex sit amet, imperdiet tempus tellus. Fusce eleifend arcu a est scelerisque, ut ullamcorper nulla eleifend.
Vestibulum eros leo, pulvinar non eros sit amet, tincidunt malesuada justo. Duis a congue tortor. Duis dapibus diam justo, vel vestibulum est pretium quis. Quisque ultrices magna tellus, at pellentesque leo molestie eget. Pellentesque lobortis placerat sapien at pulvinar. Vestibulum et ipsum at tellus tempor ultricies vitae a velit. Phasellus lacinia sem ac sapien elementum, vel venenatis nulla malesuada. Curabitur molestie justo at diam elementum ultrices. Vivamus eget maximus ligula. Proin vulputate libero id quam porta condimentum. Duis vel volutpat risus. Vivamus sodales lorem at massa consequat pulvinar. Phasellus blandit magna in lacus ultricies, dictum mattis nulla blandit.

redpajama_set_name: RedPajamaGithub


text:
OrderProduct
============

.. php:namespace:: DreamCommerce\Resource
.. php:class:: OrderProduct

Check: :doc:`../resource`.


text:
package blogr.vpm.fr.blogr.publish;

import android.content.Context;
import android.content.Intent;

/**
 * Created by vincent on 29/08/14.
 */
public class TPPostPublisher extends StdEmailPostPublisher {

  public TPPostPublisher(Context context) {
    super(context);
    setFormatter(new IdentityFormatter());
  }

  @Override
  protected void putEmailContent(Intent intent, String content) {
    intent.putExtra(Intent.EXTRA_TEXT, content);
  }
}


text:
require "rails_helper"

RSpec.describe Admin::OfflinePaymentMethodsController, type: :controller do
  before do
    logged_in_as_admin
  end

  describe "POST create" do
    let(:name) { SecureRandom.hex }
    let(:valid_params) { {"offline_payment_method" => {"name" => name}} }
    let(:invalid_params) { {"offline_payment_method" => {"name" => ""}} }

    before { post :create, params: params }

    context "with valid params" do
      let(:params) { valid_params }

      it "creates a new offline payment method with the given params" do
        expect(OfflinePaymentMethod.find_by(name: name)).to be
      end

      it "sets a flash notice" do
        expect(flash[:notice]).to eq I18n.t("controllers.admin.offline_payment_methods.create.created")
      end

      it "redirects to the offline payment method index" do
        expect(response).to redirect_to admin_offline_payment_methods_path
      end
    end
  end

  describe "DELETE destroy" do
    let(:opm) { FactoryBot.create(:offline_payment_method) }

    before do
      delete :destroy, params: {id: opm.id}
    end

    it "destroys the payment method" do
      expect(OfflinePaymentMethod.find_by(id: opm.id)).to be_nil
    end

    it "redirects to index" do
      expect(response).to redirect_to admin_offline_payment_methods_path
    end
  end
end


text:
using System.Collections.Generic;
using UIKit;
using CoreGraphics;
using Foundation;

namespace Xamarin.Utilities.DialogElements
{
    public class Section : IEnumerable<Element> {
        object header, footer;
        private readonly List<Element> _elements = new List<Element> ();

        public RootElement Root
        {
            get;
            internal set;
        }

        // X corresponds to the alignment, Y to the height of the password
        public CGSize EntryAlignment;

        public Section()
        {
        }

        public Section(UIView header, UIView footer = null)
        {
            HeaderView = header;
            FooterView = footer;
        }

        public Section(string header, string footer = null)
        {
            Header = header;
            Footer = footer;
        }

        /// <summary>
        ///    The section header, as a string
        /// </summary>
        public string Header {
            get {
                return header as string;
            }
            set {
                header = value;
            }
        }

        /// <summary>
        /// The section footer, as a string.
        /// </summary>
        public string Footer {
            get {
                return footer as string;
            }

            set {
                footer = value;
            }
        }

        /// <summary>
        /// The section's header view.
        /// </summary>
        public UIView HeaderView {
            get {
                return header as UIView;
            }
            set {
                header = value;
            }
        }

        /// <summary>
        /// The section's footer view.
        /// </summary>
        public UIView FooterView {
            get {
                return footer as UIView;
            }
            set {
                footer = value;
            }
        }

        /// <summary>
        /// Adds a new child Element to the Section
        /// </summary>
        /// <param name="element">
        /// An element to add to the section.
        /// </param>
        public void Add (Element element)
        {
            if (element == null)
                return;

            _elements.Add (element);
            element.Section = this;

            if (Root != null)
                InsertVisual (_elements.Count-1, UITableViewRowAnimation.None, 1);
        }

        public void Add(IEnumerable<Element> elements)
        {
            AddAll(elements);
        }

        /// <summary>
        ///    Add version that can be used with LINQ
        /// </summary>
        /// <param name="elements">
        /// An enumerable list that can be produced by something like:
        ///    from x in ... select (Element) new MyElement (...)
        /// </param>
        public int AddAll(IEnumerable<Element> elements)
        {
            int count = 0;
            foreach (var e in elements){
                Add (e);
                count++;
            }
            return count;
        }

        /// <summary>
        /// Inserts a series of elements into the Section using the specified animation
        /// </summary>
        /// <param name="idx">
        /// The index where the elements are inserted
        /// </param>
        /// <param name="anim">
        /// The animation to use
        /// </param>
        /// <param name="newElements">
        /// A series of elements.
        /// </param>
        public void Insert (int idx, UITableViewRowAnimation anim, params Element [] newElements)
        {
            if (newElements == null)
                return;

            int pos = idx;
            foreach (var e in newElements)
            {
                _elements.Insert (pos++, e);
                e.Section = this;
            }

            if (Root != null && Root.TableView != null)
            {
                if (anim == UITableViewRowAnimation.None)
                    Root.TableView.ReloadData ();
                else
                    InsertVisual (idx, anim, newElements.Length);
            }
        }

        public int Insert (int idx, UITableViewRowAnimation anim, IEnumerable<Element> newElements)
        {
            if (newElements == null)
                return 0;

            int pos = idx;
            int count = 0;
            foreach (var e in newElements)
            {
                _elements.Insert (pos++, e);
                e.Section = this;
                count++;
            }

            if (Root != null && Root.TableView != null)
            {
                if (anim == UITableViewRowAnimation.None)
                    Root.TableView.ReloadData ();
                else
                    InsertVisual (idx, anim, pos-idx);
            }
            return count;
        }

        void InsertVisual (int idx, UITableViewRowAnimation anim, int count)
        {
            if (Root == null || Root.TableView == null)
                return;

            int sidx = Root.IndexOf (this);
            var paths = new NSIndexPath [count];
            for (int i = 0; i < count; i++)
                paths [i] = NSIndexPath.FromRowSection (idx+i, sidx);
            Root.TableView.InsertRows (paths, anim);
        }

        public void Insert (int index, params Element [] newElements)
        {
            Insert (index, UITableViewRowAnimation.None, newElements);
        }

        public void Remove (Element e, UITableViewRowAnimation animation = UITableViewRowAnimation.Fade)
        {
            if (e == null)
                return;

            for (int i = _elements.Count; i > 0;)
            {
                i--;
                if (_elements [i] == e)
                {
                    RemoveRange (i, 1, animation);
                    e.Section = null;
                    return;
                }
            }
        }

        public void Remove (int idx)
        {
            RemoveRange (idx, 1);
        }

        /// <summary>
        /// Remove a range of elements from the section with the given animation
        /// </summary>
        /// <param name="start">
        /// Starting position
        /// </param>
        /// <param name="count">
        /// Number of elements to remove form the section
        /// </param>
        /// <param name="anim">
        /// The animation to use while removing the elements
        /// </param>
        public void RemoveRange (int start, int count, UITableViewRowAnimation anim = UITableViewRowAnimation.Fade)
        {
            if (start < 0 || start >= _elements.Count)
                return;
            if (count == 0)
                return;

            if (start+count > _elements.Count)
                count = _elements.Count-start;

            _elements.RemoveRange (start, count);

            if (Root != null && Root.TableView != null)
            {
//                int sidx = Root.IndexOf(this);
//                var paths = new NSIndexPath [count];
//                for (int i = 0; i < count; i++)
//                    paths[i] = NSIndexPath.FromRowSection(start + i, sidx);
//                Root.TableView.DeleteRows(paths, anim);

                Root.TableView.ReloadData();
            }
        }

        public int Count
        {
            get { return _elements.Count; }
        }

        public IEnumerator<Element> GetEnumerator()
        {
            return _elements.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public Element this [int idx]
        {
            get { return _elements[idx]; }
        }

        public void Clear ()
        {
            foreach (var e in _elements)
                e.Section = null;
            _elements.Clear();
            if (Root != null && Root.TableView != null)
                Root.TableView.ReloadData ();
        }

        public void Reset(IEnumerable<Element> elements, UITableViewRowAnimation animation = UITableViewRowAnimation.Fade)
        {
            _elements.Clear();
            _elements.AddRange(elements);
            foreach (var e in _elements)
                e.Section = this;
            if (Root != null && Root.TableView != null)
                Root.TableView.ReloadData();
        }
    }
}


text:
var gulp = require('gulp');
var sass = require('gulp-sass');
var renames = require('gulp-rename');
var concats = require('gulp-concat');
var uglify = require('gulp-uglify');
var sourcemaps = require('gulp-sourcemaps');
var watch = require('gulp-watch');

gulp.task('default',['sass','js']);

gulp.task('sass',function(){
    gulp.src('src/sass/**/*.scss')
        .pipe(sourcemaps.init())
        .pipe(sass({outputStyle:'compressed'}))
        .pipe(renames('rola.min.css'))
        .pipe(sourcemaps.write('./'))
        .pipe(gulp.dest('./dist/css'));
});

// This task only compile SASS
gulp.task('sass:compile',function(){
    gulp.src('src/sass/**/*.scss')
        .pipe(sass())
        .pipe(renames('rola.min.css'))
        .pipe(gulp.dest('./dist/css'));
});

gulp.task('js',function(){
    gulp.src(['src/libs/js/**/*.js','src/js/**/*.js'])
        .pipe(sourcemaps.init())
        .pipe(uglify())
        .pipe(concats('all.js'))
        .pipe(renames('rola.min.js'))
        .pipe(sourcemaps.write('./'))
        .pipe(gulp.dest('./dist/js'));
});

gulp.task('watch:sass',function(){
    return gulp.watch('src/sass/**/*.scss',['sass'])
});


text:
'use strict';

demoModule.controller('WriteBarrierCtrl',  [ '$scope', 'apiService',function($scope,$apiService) {
    $scope.success = false;
    $scope.error = false;
    $scope.writebarrier = {
        shift: 10,
        value: "value1"
    };

    $scope.delete = function () {
        $apiService.writeBarrierDelete($scope.writebarrier.shift,
            $apiService.createSuccess($scope),
            $apiService.createError($scope));
    };

    $scope.insertValue = function () {
        $apiService.writeBarrierInsertValue($scope.writebarrier.value,
            $apiService.createSuccess($scope),
            $apiService.createError($scope));
    };
} ]);


text:
package net.jloop.rejoice.functions;

import net.jloop.rejoice.Env;
import net.jloop.rejoice.Function;
import net.jloop.rejoice.types.Quote;
import net.jloop.rejoice.types.Stack;

import java.util.Deque;

public final class F_apply implements Function {

    @Override
    public Stack call(Env env, Stack data, Deque<Quote> call) {
        call.push(data.consume(Quote.class));
        return data;
    }
}


text:
package org.kiji.avro.mapreduce;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;

import org.apache.avro.Schema;
import org.apache.avro.file.DataFileReader;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.hadoop.io.AvroKeyValue;
import org.apache.avro.io.DatumReader;
import org.apache.avro.specific.SpecificDatumReader;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

public class TestKeyValueWordCount {
  @Rule
  public TemporaryFolder mTempDir = new TemporaryFolder();

  public static class LineCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
    private IntWritable mOne;

    @Override
    protected void setup(Context context) {
      mOne = new IntWritable(1);
    }

    @Override
    protected void map(LongWritable fileByteOffset, Text line, Context context)
        throws IOException, InterruptedException {
      context.write(line, mOne);
    }
  }

  public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
    @Override
    protected void reduce(Text word, Iterable<IntWritable> counts, Context context)
        throws IOException, InterruptedException {
      int sum = 0;
      for (IntWritable count : counts) {
        sum += count.get();
      }
      context.write(word, new IntWritable(sum));
    }
  }

  private Configuration mConf;

  @Before
  public final void setup() {
    mConf = new Configuration();
    mConf.set("fs.defaultFS", "file:///");
    mConf.set("mapred.job.tracker", "local");
  }

  @Test
  public void testKeyValueMapReduce()
      throws ClassNotFoundException, IOException, InterruptedException, URISyntaxException {
    // Configure a word count job over our test input file.
    final Job job = new Job(mConf);

    FileInputFormat.setInputPaths(job, new Path(getClass()
            .getResource("/org/apache/avro/mapreduce/mapreduce-test-input.txt")
            .toURI().toString()));
    job.setInputFormatClass(TextInputFormat.class);

    job.setMapperClass(LineCountMapper.class);
    job.setMapOutputKeyClass(Text.class);
    job.setMapOutputValueClass(IntWritable.class);

    job.setReducerClass(IntSumReducer.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);

    job.setOutputFormatClass(AvroKeyValueOutputFormat.class);
    Path outputPath = new Path(mTempDir.getRoot().getPath() + "/out-wordcount");
    FileOutputFormat.setOutputPath(job, outputPath);

    // Run the job.
    assertTrue(job.waitForCompletion(true));

    // Verify that the Avro container file generated had the right KeyValuePair generic records.
    File avroFile = new File(outputPath.toString(), "part-r-00000.avro");
    DatumReader<GenericRecord> datumReader = new SpecificDatumReader<GenericRecord>(
        AvroKeyValue.getSchema(Schema.create(Schema.Type.STRING), Schema.create(Schema.Type.INT)));
    DataFileReader<GenericRecord> avroFileReader
        = new DataFileReader<GenericRecord>(avroFile, datumReader);

    assertTrue(avroFileReader.hasNext());
    AvroKeyValue<CharSequence, Integer> appleRecord
        = new AvroKeyValue<CharSequence, Integer>(avroFileReader.next());
    assertNotNull(appleRecord.get());
    assertEquals("apple", appleRecord.getKey().toString());
    assertEquals(3, appleRecord.getValue().intValue());

    assertTrue(avroFileReader.hasNext());
    AvroKeyValue<CharSequence, Integer> bananaRecord
        = new AvroKeyValue<CharSequence, Integer>(avroFileReader.next());
    assertNotNull(bananaRecord.get());
    assertEquals("banana", bananaRecord.getKey().toString());
    assertEquals(2, bananaRecord.getValue().intValue());

    assertTrue(avroFileReader.hasNext());
    AvroKeyValue<CharSequence, Integer> carrotRecord
        = new AvroKeyValue<CharSequence, Integer>(avroFileReader.next());
    assertEquals("carrot", carrotRecord.getKey().toString());
    assertEquals(1, carrotRecord.getValue().intValue());

    assertFalse(avroFileReader.hasNext());
    avroFileReader.close();
  }
}


text:
@interface PullToRefreshViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel * label;
@property (weak, nonatomic) IBOutlet NBURefreshControl * refreshControl;

- (IBAction)refresh:(id)sender;

- (IBAction)testUpdated:(id)sender;
- (IBAction)testFailedToUpdate:(id)sender;

@end


text:
package ssh

import (
    "encoding/binary"
    "errors"
)

var bigendian = binary.BigEndian

// Encoder type. Don't touch the internals.
type Encoder struct {
	buf []byte
	commit func([]byte) (int, error)
}

// Create a new printer with empty output.
func NewEncoder() *Encoder {
	return &Encoder{make([]byte, 0, 32), nil}
}

// Create a new printer with output prefixed with the given byte slice.
func NewEncoderWith(data []byte) *Encoder {
	return &Encoder{data, nil}
}

func NewEncoderWithCommit(data []byte, commit func([]byte) (int, error)) *Encoder {
	return &Encoder{data, commit}
}

// Output a byte.
func (p *Encoder) Byte(d byte) *Encoder {
	p.buf = append(p.buf, d)
	return p
}

// Output 2 bigendian bytes.
func (p *Encoder) U16(d uint16) *Encoder {
	p.buf = append(p.buf, byte(d>>8), byte(d))
	return p
}

// Output 4 bigendian bytes.
func (p *Encoder) U32(d uint32) *Encoder {
	p.buf = append(p.buf, byte(d>>24), byte(d>>16), byte(d>>8), byte(d))
	return p
}

// Output 4 bigendian bytes.
func (p *Encoder) U64(d uint64) *Encoder {
	p.buf = append(p.buf, byte(d>>56), byte(d>>48), byte(d>>40), byte(d>>32), byte(d>>24), byte(d>>16), byte(d>>8), byte(d))
	return p
}

// Output a raw byte slice with no length prefix.
func (p *Encoder) Bytes(d []byte) *Encoder {
	p.buf = append(p.buf, d...)
	return p
}

// Output a raw string with no length prefix.
func (p *Encoder) String(d string) *Encoder {
	p.buf = append(p.buf, []byte(d)...)
	return p
}

// Output a string with a 4 byte bigendian length prefix and no trailing null.
func (p *Encoder) U32String(d string) *Encoder {
	return p.U32(uint32(len(d))).String(d)
}

// Output bytes with a 4 byte bigendian length prefix and no trailing null.
func (p *Encoder) U32Bytes(d []byte) *Encoder {
	return p.U32(uint32(len(d))).Bytes(d)
}

// Output a string with a 2 byte bigendian length prefix and no trailing null.
func (p *Encoder) U16String(d string) *Encoder {
	if len(d) > 0xffff {
		panic(errors.New("string length overflows uint16"))
	}
	return p.U16(uint16(len(d))).String(d)
}

// Output a string with a 1 byte bigendian length prefix and no trailing null.
func (p *Encoder) U8String(d string) *Encoder {
	if len(d) > 0xff {
		panic(errors.New("string length overflows uint8"))
	}
	return p.Byte(byte(len(d))).String(d)
}

// Output a string terminated by a null-byte
func (p *Encoder)String0(d string) *Encoder {
	return p.String(d).Byte(0)
}

// Get the output as a byte slice.
func (p *Encoder) Out() []byte {
	return p.buf
}

func (p *Encoder) Commit() (int, error) {
    return p.commit(p.buf)
}


text:
package com.bradmcevoy.http;

/**
 * Implementation of milton's cookie interface which just wraps a javax.servlet cookie
 *
 * The reference to the wrapped cookie is final, but the cookie itself is
 * mutable.
 *
 * @author brad
 */
public class ServletCookie implements Cookie {

    private final javax.servlet.http.Cookie cookie;

    public ServletCookie( javax.servlet.http.Cookie cookie ) {
        this.cookie = cookie;
    }

    public javax.servlet.http.Cookie getWrappedCookie() {
        return cookie;
    }

    public int getVersion() {
        return cookie.getVersion();
    }

    public void setVersion( int version ) {
        cookie.setVersion( version );
    }

    public String getName() {
        return cookie.getName();
    }

    public String getValue() {
        return cookie.getValue();
    }

    public void setValue( String value ) {
        cookie.setValue( value );
    }

    public boolean getSecure() {
        return cookie.getSecure();
    }

    public void setSecure( boolean secure ) {
        cookie.setSecure( secure );
    }

    public int getExpiry() {
        return cookie.getMaxAge();
    }

    public void setExpiry( int expiry ) {
        cookie.setMaxAge( expiry );
    }

    public String getPath() {
        return cookie.getPath();
    }

    public void setPath( String path ) {
        cookie.setPath( path );
    }

    public String getDomain() {
        return cookie.getDomain();
    }

    public void setDomain( String domain ) {
        cookie.setDomain( domain );
    }

}