// UnitTestも考慮してPartnerSoapSforceCom.cls をwrapperする
public with sharing class SoapApi {
    private static final PartnerSoapSforceCom.Soap soap;

    // https://developer.salesforce.com/forums/ForumsMain?id=906F000000092a6IAA
    private static Map<String, String> domainInstanceMap { get; private set; }
    static {
        soap = new PartnerSoapSforceCom.Soap();
        soap.SessionHeader = new PartnerSoapSforceCom.SessionHeader_element();
        // This is important, give session id, to let the call work.
        soap.SessionHeader.sessionId = UserInfo.getSessionId();
        soap.endpoint_x = URL.getSalesforceBaseUrl().toExternalForm() + '/services/Soap/u/33.0';
    }

    /*
    PartnerSoapSforceCom.DescribeLayoutResult dlr = SoapApi.describeLayout('Repair__c', null, new String[]{'01210000000QmS9'});
    System.debug('layoutId=' + dlr.layouts[0].id);
    System.debug('recordTypeId=' + dlr.recordTypeMappings[0].recordTypeId);
    */
    public static PartnerSoapSforceCom.DescribeLayoutResult describeLayout(String sObjectType, String layoutName, String[] recordTypeIds) {
        if (System.Test.isRunningTest()) {
            // UnitTest 用
            PartnerSoapSforceCom.DescribeLayoutResult dlr = new PartnerSoapSforceCom.DescribeLayoutResult();
            PartnerSoapSforceCom.RecordTypeMapping rtMap = new PartnerSoapSforceCom.RecordTypeMapping();
            PartnerSoapSforceCom.DescribeLayout editLayout = new PartnerSoapSforceCom.DescribeLayout();
            PartnerSoapSforceCom.DescribeLayoutSection editLayoutSection = new PartnerSoapSforceCom.DescribeLayoutSection();
            PartnerSoapSforceCom.DescribeLayoutRow layoutRow = new PartnerSoapSforceCom.DescribeLayoutRow();
            PartnerSoapSforceCom.DescribeLayoutItem layoutItem = new PartnerSoapSforceCom.DescribeLayoutItem();
            PartnerSoapSforceCom.DescribeLayoutComponent layoutComponent = new PartnerSoapSforceCom.DescribeLayoutComponent();
            dlr.recordTypeMappings = new List<PartnerSoapSforceCom.RecordTypeMapping>();
            dlr.recordTypeMappings.add(rtMap);
            rtMap.recordTypeId = 'recordTypeId';
            dlr.layouts = new List<PartnerSoapSforceCom.DescribeLayout>();
            dlr.layouts.add(editLayout);
            editLayout.editLayoutSections = new List<PartnerSoapSforceCom.DescribeLayoutSection>();
            editLayout.editLayoutSections.add(editLayoutSection);
            editLayoutSection.layoutRows = new List<PartnerSoapSforceCom.DescribeLayoutRow>();
            editLayoutSection.layoutRows.add(layoutRow);
            layoutRow.layoutItems = new List<PartnerSoapSforceCom.DescribeLayoutItem>();
            layoutRow.layoutItems.add(layoutItem);
            layoutItem.layoutComponents = new List<PartnerSoapSforceCom.DescribeLayoutComponent>();
            layoutItem.layoutComponents.add(layoutComponent);
            return dlr;
        } else {
            return soap.describeLayout(sObjectType, layoutName, recordTypeIds);
        }
    }

    // 編集用レイズとの項目の r,w,wm の情報を 返す
    // Mapには、RecordTypeId(18桁) => {api名 => r,w,wm}
    // 如果Object中设定记录类型，该方法不能取出对应记录类型的项目，修改后方法名：getEditRWByRecordType --下一个方法
    public static Map<String, Map<String, String>> getEditLayoutItemRW(String sObjectType, String[] recordTypeIds) {
        PartnerSoapSforceCom.DescribeLayoutResult dlr = describeLayout(sObjectType, null, recordTypeIds);
        Map<String, Map<String, String>> rtn = new Map<String, Map<String, String>>();
        for (Integer lidx = 0; lidx < dlr.layouts.size(); lidx++) {
            String recordTypeId = dlr.recordTypeMappings[lidx].recordTypeId;
            Map<String, String> rtnInner = new Map<String, String>();
            rtn.put(recordTypeId, rtnInner);
            System.debug('recordTypeId=' + recordTypeId);
            for (PartnerSoapSforceCom.DescribeLayoutSection section : dlr.layouts[lidx].editLayoutSections) {
                for (PartnerSoapSforceCom.DescribeLayoutRow row : section.layoutRows) {
                    for (PartnerSoapSforceCom.DescribeLayoutItem item : row.layoutItems) {
                        if (item.layoutComponents != null && item.layoutComponents.size() > 0
                                && String.isBlank(item.layoutComponents[0].value) == false) {
                            rtnInner.put(item.layoutComponents[0].value, 'r');
                            if (item.editableForUpdate) {
                                rtnInner.put(item.layoutComponents[0].value, 'w');
                            }
                            if (item.required) {
                                rtnInner.put(item.layoutComponents[0].value, 'wm');
                            }
                        }
                    }
                }
            }
        }
        return rtn;
    }

    // 編集用レイズとの項目の r,w,wm の情報を 返す
    // Mapには、RecordTypeId(18桁) => {api名 => r,w,wm}
    public static Map<String, Map<String, String>> getEditRWByRecordType(String sObjectType, String[] recordTypeIds) {
        PartnerSoapSforceCom.DescribeLayoutResult dlr = describeLayout(sObjectType, null, recordTypeIds);
        Map<String, Map<String, String>> rtn = new Map<String, Map<String, String>>();
        //
        Map<String, PartnerSoapSforceCom.DescribeLayout> layputMap = new Map<String, PartnerSoapSforceCom.DescribeLayout>();
        for (PartnerSoapSforceCom.DescribeLayout layout : dlr.layouts) {
            layputMap.put(layout.id, layout);
        }
        //for (Integer lidx = 0; lidx < dlr.layouts.size(); lidx++) {
        for (Integer lidx = 0; lidx < dlr.recordTypeMappings.size(); lidx++) {
            String recordTypeId = dlr.recordTypeMappings[lidx].recordTypeId;
            Map<String, String> rtnInner = new Map<String, String>();
            rtn.put(recordTypeId, rtnInner);
            System.debug('recordTypeId=' + recordTypeId);
            PartnerSoapSforceCom.DescribeLayout layout = layputMap.get(dlr.recordTypeMappings[lidx].layoutId);
            //for (PartnerSoapSforceCom.DescribeLayoutSection section : dlr.layouts[lidx].editLayoutSections) {
            for (PartnerSoapSforceCom.DescribeLayoutSection section : layout.editLayoutSections) {
                for (PartnerSoapSforceCom.DescribeLayoutRow row : section.layoutRows) {
                    for (PartnerSoapSforceCom.DescribeLayoutItem item : row.layoutItems) {
                        if (item.layoutComponents != null && item.layoutComponents.size() > 0
                                && String.isBlank(item.layoutComponents[0].value) == false) {
                            rtnInner.put(item.layoutComponents[0].value, 'r');
                            if (item.editableForUpdate) {
                                rtnInner.put(item.layoutComponents[0].value, 'w');
                            }
                            if (item.required) {
                                rtnInner.put(item.layoutComponents[0].value, 'wm');
                            }
                        }
                    }
                }
            }
        }
        return rtn;
    }
}