public with sharing class OFSCommentPopupController {
    public SObject sobj { get; private set; }
    public String label { get; private set; }
    //public List<> itemList { get; private set; }
    public List<ItemBean> itemList { get; private set; }
    
    public void init() {
        String tbl = ApexPages.currentPage().getParameters().get('table');
        String id = ApexPages.currentPage().getParameters().get('id');
        String api = ApexPages.currentPage().getParameters().get('api');
        String rw = ApexPages.currentPage().getParameters().get('rw');
        //String asave = ApexPages.currentPage().getParameters().get('afterSave');
        
        List<String> apiList = api.split(',', -1);
        List<String> authList = rw.split(',', -1);
        // apiの数とRead/Writeの数は必ず一緒
        if (apiList.size() != authList.size()) {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, 'parameter error!'));
            return;
        }
        
        // 動的SOQL
        String soql = 'select Id';
        Set<String> apiTempSet = new Set<String>();
        for (String field : apiList) {
            if (String.isBlank(field) == false && apiTempSet.contains(field) == false) {
                soql += ', ' + field;
                apiTempSet.add(field);
            }
        }
        soql += ' from ' + tbl + ' where Id = \'' + id + '\'';
        sobj = Database.query(soql);
        label = sobj.getSObjectType().getDescribe().getLabel();
        
        itemList = new List<ItemBean>();
        for (Integer i = 0; i < apiList.size(); i++) {
            itemList.add(new ItemBean(apiList[i], authList[i]));
        }
    }
    
    public void save() {
        Savepoint sp = Database.setSavepoint();
        try {
            for (ItemBean ib : itemList) {
                if (String.isBlank(ib.solidStr) == false) {
                    sobj.put(ib.api, ib.solidStr);
                }
            }
            upsert sobj;
            
            // 承認プロセス走る
            String asave = ApexPages.currentPage().getParameters().get('afterSave');
            if (asave == 'Approve') {
                Approval.ProcessSubmitRequest psr = new Approval.ProcessSubmitRequest();
                psr.setObjectId(sobj.id);
                Approval.ProcessResult submitResult = Approval.process(psr);
            }
        } catch(Exception ex) {
            Database.rollback(sp);
            ApexPages.addMessages(ex);
            return;
        }
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, 'save complete'));
    }
    
    public class ItemBean {
        public String api { get; set; }
        public Boolean isRead { get; set; }
        public Boolean isWrite { get; set; }
        public Boolean isMandatory { get; set; }
        public String solidStr { get; set; }
        
        public ItemBean(String api, String auth) {
            this.defaultSet();
            
            this.api = api.trim();
            if (auth.trim() == 'r') {
                isRead = true;
            }
            else if (auth.trim() == 'w') {
                isWrite = true;
            }
            else if (auth.trim() == 'wm') {
                isWrite = true;
                isMandatory = true;
            }
            else {
                solidStr = auth;
            }
        }
        
        private void defaultSet() {
            isRead = false;
            isWrite = false;
            isMandatory = false;
        }
    }
}