public static String updatePublishLinks(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        Security security = (Security)request.getAttribute("security");
        GenericValue userLogin = (GenericValue)session.getAttribute("userLogin");
        ServletContext servletContext = session.getServletContext();
        String webSiteId = WebSiteWorker.getWebSiteId(request);
        Delegator delegator = (Delegator)request.getAttribute("delegator");
        LocalDispatcher dispatcher = (LocalDispatcher)request.getAttribute("dispatcher");
        Map<String, Object> paramMap = UtilHttp.getParameterMap(request);
        //if (Debug.infoOn()) Debug.logInfo("in updatePublishLinks, paramMap:" + paramMap , module);
        String targContentId = (String)paramMap.get("contentId"); // The content to be linked to one or more sites
        String roles = null;
        String authorId = null;
        GenericValue authorContent = ContentManagementWorker.getAuthorContent(delegator, targContentId);
        if (authorContent != null) {
            authorId = authorContent.getString("contentId");
        } else {
            request.setAttribute("_ERROR_MESSAGE_", "authorContent is empty.");
            return "error";
        }
        // Determine if user is owner of target content
        String userLoginId = userLogin.getString("userLoginId");
        //if (Debug.infoOn()) Debug.logInfo("in updatePublishLinks, userLoginId:" + userLoginId + " authorId:" + authorId , module);
        List<String> roleTypeList = null;
        if (authorId != null && userLoginId != null && authorId.equals(userLoginId)) {
            roles = "OWNER";
            roleTypeList = StringUtil.split(roles, "|");
        }
        List<String> targetOperationList = UtilMisc.<String>toList("CONTENT_PUBLISH");
        List<String> contentPurposeList = null; //UtilMisc.toList("ARTICLE");
        //if (Debug.infoOn()) Debug.logInfo("in updatePublishLinks, roles:" + roles +" roleTypeList:" + roleTypeList , module);
        String permittedAction = (String)paramMap.get("permittedAction"); // The content to be linked to one or more sites
        String permittedOperations = (String)paramMap.get("permittedOperations"); // The content to be linked to one or more sites
        if (UtilValidate.isEmpty(targContentId)) {
            request.setAttribute("_ERROR_MESSAGE_", "targContentId is empty.");
            return "error";
        }
        // Get all the subSites that the user is permitted to link to
        List<Object []> origPublishedLinkList = null;
        try {
            // TODO: this needs to be given author userLogin
            EntityQuery.use(delegator).from("UserLogin").where("userLoginId", authorId).cache().queryOne();
            origPublishedLinkList = ContentManagementWorker.getPublishedLinks(delegator, targContentId, webSiteId, userLogin, security, permittedAction, permittedOperations, roles);
        } catch (GenericEntityException e) {
            request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
            return "error";
        } catch (GeneralException e2) {
            request.setAttribute("_ERROR_MESSAGE_", e2.getMessage());
            return "error";
        }
                //if (Debug.infoOn()) Debug.logInfo("in updatePublishLinks, origPublishedLinkList:" + origPublishedLinkList , module);
        // make a map of the values that are passed in using the top subSite as the key.
        // Content can only be linked to one subsite under a top site (ends with "_MASTER")
        Map<String, String> siteIdLookup = new HashMap<String, String>();
        for (String param : paramMap.keySet()) {
            int pos = param.indexOf("select_");
                //if (Debug.infoOn()) Debug.logInfo("in updatePublishLinks, param:" + param + " pos:" + pos , module);
            if (pos >= 0) {
                String siteId = param.substring(7);
                String subSiteVal = (String)paramMap.get(param);
                siteIdLookup.put(siteId, subSiteVal);
            }
        }
        //if (Debug.infoOn()) Debug.logInfo("in updatePublishLinks, siteIdLookup:" + siteIdLookup , module);
        // Loop thru all the possible subsites
        Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
        // int counter = 0;
        String responseMessage = null;
        String errorMessage = null;
        // String permissionMessage = null;
        boolean statusIdUpdated = false;
        Map<String, Object> results = null;
        for (Object [] arr : origPublishedLinkList) {
            //if (Debug.infoOn()) Debug.logInfo("in updatePublishLinks, arr:" + Arrays.asList(arr) , module);
            String contentId = (String)arr[0]; // main (2nd level) site id
            String origSubContentId = null;
            List<Object []> origSubList = UtilGenerics.checkList(arr[1]);
            // Timestamp topFromDate = (Timestamp)arr[3];
            Timestamp origFromDate = null;
            for (Object [] pubArr : origSubList) {
            // see if a link already exists by looking for non-null fromDate
                //if (Debug.infoOn()) Debug.logInfo("in updatePublishLinks, pubArr:" + Arrays.asList(pubArr) , module);
                Timestamp fromDate = (Timestamp)pubArr[2];
                origSubContentId = null;
                if (fromDate != null) {
                    origSubContentId = (String)pubArr[0];
                    origFromDate = fromDate;
                    break;
                }
            }
            String currentSubContentId = siteIdLookup.get(contentId);
            //if (Debug.infoOn()) Debug.logInfo("in updatePublishLinks, currentSubContentId:" + currentSubContentId , module);
            //if (Debug.infoOn()) Debug.logInfo("in updatePublishLinks, origSubContentId:" + origSubContentId , module);
            try {
                if (UtilValidate.isNotEmpty(currentSubContentId)) {
                    if (!currentSubContentId.equals(origSubContentId)) {
                        // disable existing link
                        if (UtilValidate.isNotEmpty(origSubContentId) && origFromDate != null) {
                            List<GenericValue> oldActiveValues = EntityQuery.use(delegator).from("ContentAssoc")
                                    .where("contentId", targContentId, 
                                            "contentIdTo", origSubContentId, 
                                            "contentAssocTypeId", "PUBLISH_LINK", 
                                            "thruDate", null)
                                    .queryList();
                            for (GenericValue cAssoc : oldActiveValues) {
                                cAssoc.set("thruDate", nowTimestamp);
                                cAssoc.store();
                                //if (Debug.infoOn()) Debug.logInfo("in updatePublishLinks, deactivating:" + cAssoc , module);
                            }
                        }
                        // create new link
                        Map<String, Object> serviceIn = new HashMap<String, Object>();
                        serviceIn.put("userLogin", userLogin);
                        serviceIn.put("contentId", targContentId);
                        serviceIn.put("contentAssocTypeId", "PUBLISH_LINK");
                        serviceIn.put("fromDate", nowTimestamp);
                        serviceIn.put("contentIdTo", currentSubContentId);
                        serviceIn.put("roleTypeList", roleTypeList);
                        serviceIn.put("targetOperationList", targetOperationList);
                        serviceIn.put("contentPurposeList", contentPurposeList);
                        results = dispatcher.runSync("createContentAssoc", serviceIn);
                        responseMessage = (String)results.get(ModelService.RESPONSE_MESSAGE);
                        if (UtilValidate.isNotEmpty(responseMessage)) {
                            errorMessage = (String)results.get(ModelService.ERROR_MESSAGE);
                            Debug.logError("in updatePublishLinks, serviceIn:" + serviceIn , module);
                            Debug.logError(errorMessage, module);
                            request.setAttribute("_ERROR_MESSAGE_", errorMessage);
                            return "error";
                        }
                        serviceIn = new HashMap<String, Object>();
                        serviceIn.put("userLogin", userLogin);
                        serviceIn.put("contentId", targContentId);
                        serviceIn.put("contentAssocTypeId", "PUBLISH_LINK");
                        serviceIn.put("fromDate", nowTimestamp);
                        serviceIn.put("contentIdTo", contentId);
                        serviceIn.put("roleTypeList", roleTypeList);
                        serviceIn.put("targetOperationList", targetOperationList);
                        serviceIn.put("contentPurposeList", contentPurposeList);
                        //if (Debug.infoOn()) Debug.logInfo("in updatePublishLinks, serviceIn(3b):" + serviceIn , module);
                        results = dispatcher.runSync("createContentAssoc", serviceIn);
                        //if (Debug.infoOn()) Debug.logInfo("in updatePublishLinks, results(3b):" + results , module);
                        if (!statusIdUpdated) {
                            try {
                                GenericValue targContent = EntityQuery.use(delegator).from("Content").where("contentId", targContentId).queryOne();
                                targContent.set("statusId", "CTNT_PUBLISHED");
                                targContent.store();
                                statusIdUpdated = true;
                            } catch (GenericEntityException e) {
                                Debug.logError(e.getMessage(), module);
                                request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
                                return "error";
                            }
                        }
                    }
                } else if (UtilValidate.isNotEmpty(origSubContentId)) {
                    // if no current link is passed in, look to see if there is an existing link(s) that must be disabled
                    List<GenericValue> oldActiveValues = EntityQuery.use(delegator).from("ContentAssoc")
                            .where("contentId", targContentId, 
                                    "contentIdTo", origSubContentId, 
                                    "contentAssocTypeId", "PUBLISH_LINK", 
                                    "thruDate", null)
                            .queryList();
                    for (GenericValue cAssoc : oldActiveValues) {
                        cAssoc.set("thruDate", nowTimestamp);
                        cAssoc.store();
                    }
                }
            } catch (GenericEntityException e) {
                Debug.logError(e.getMessage(), module);
                request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
                return "error";
            } catch (GenericServiceException e2) {
                Debug.logError(e2, module);
                request.setAttribute("_ERROR_MESSAGE_", e2.getMessage());
                return "error";
            }
        }
        return "success";
    }
public static String updatePublishLinks(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        Security security = (Security)request.getAttribute("security");
        GenericValue userLogin = (GenericValue)session.getAttribute("userLogin");
        ServletContext servletContext = session.getServletContext();
        String webSiteId = WebSiteWorker.getWebSiteId(request);
        Delegator delegator = (Delegator)request.getAttribute("delegator");
        LocalDispatcher dispatcher = (LocalDispatcher)request.getAttribute("dispatcher");
        Map<String, Object> paramMap = UtilHttp.getParameterMap(request);
       
        String targContentId = (String)paramMap.get("contentId");
        String roles = null;
        String authorId = null;
        GenericValue authorContent = ContentManagementWorker.getAuthorContent(delegator, targContentId);
        if (authorContent != null) {
            authorId = authorContent.getString("contentId");
        } else {
            request.setAttribute("_ERROR_MESSAGE_", "authorContent is empty.");
            return "error";
        }
       
        String userLoginId = userLogin.getString("userLoginId");
       
        List<String> roleTypeList = null;
        if (authorId != null && userLoginId != null && authorId.equals(userLoginId)) {
            roles = "OWNER";
            roleTypeList = StringUtil.split(roles, "|");
        }
        List<String> targetOperationList = UtilMisc.<String>toList("CONTENT_PUBLISH");
        List<String> contentPurposeList = null;
       
        String permittedAction = (String)paramMap.get("permittedAction");
        String permittedOperations = (String)paramMap.get("permittedOperations");
        if (UtilValidate.isEmpty(targContentId)) {
            request.setAttribute("_ERROR_MESSAGE_", "targContentId is empty.");
            return "error";
        }
       
        List<Object []> origPublishedLinkList = null;
        try {
           
            EntityQuery.use(delegator).from("UserLogin").where("userLoginId", authorId).cache().queryOne();
            origPublishedLinkList = ContentManagementWorker.getPublishedLinks(delegator, targContentId, webSiteId, userLogin, security, permittedAction, permittedOperations, roles);
        } catch (GenericEntityException e) {
            request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
            return "error";
        } catch (GeneralException e2) {
            request.setAttribute("_ERROR_MESSAGE_", e2.getMessage());
            return "error";
        }
               
       
       
        Map<String, String> siteIdLookup = new HashMap<String, String>();
        for (String param : paramMap.keySet()) {
            int pos = param.indexOf("select_");
               
            if (pos >= 0) {
                String siteId = param.substring(7);
                String subSiteVal = (String)paramMap.get(param);
                siteIdLookup.put(siteId, subSiteVal);
            }
        }
       
       
        Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
       
        String responseMessage = null;
        String errorMessage = null;
       
        boolean statusIdUpdated = false;
        Map<String, Object> results = null;
        for (Object [] arr : origPublishedLinkList) {
           
            String contentId = (String)arr[0];
            String origSubContentId = null;
            List<Object []> origSubList = UtilGenerics.checkList(arr[1]);
           
            Timestamp origFromDate = null;
            for (Object [] pubArr : origSubList) {
           
               
                Timestamp fromDate = (Timestamp)pubArr[2];
                origSubContentId = null;
                if (fromDate != null) {
                    origSubContentId = (String)pubArr[0];
                    origFromDate = fromDate;
                    break;
                }
            }
            String currentSubContentId = siteIdLookup.get(contentId);
           
           
            try {
                if (UtilValidate.isNotEmpty(currentSubContentId)) {
                    if (!currentSubContentId.equals(origSubContentId)) {
                       
                        if (UtilValidate.isNotEmpty(origSubContentId) && origFromDate != null) {
                            List<GenericValue> oldActiveValues = EntityQuery.use(delegator).from("ContentAssoc")
                                    .where("contentId", targContentId, 
                                            "contentIdTo", origSubContentId, 
                                            "contentAssocTypeId", "PUBLISH_LINK", 
                                            "thruDate", null)
                                    .queryList();
                            for (GenericValue cAssoc : oldActiveValues) {
                                cAssoc.set("thruDate", nowTimestamp);
                                cAssoc.store();
                               
                            }
                        }
                       
                        Map<String, Object> serviceIn = new HashMap<String, Object>();
                        serviceIn.put("userLogin", userLogin);
                        serviceIn.put("contentId", targContentId);
                        serviceIn.put("contentAssocTypeId", "PUBLISH_LINK");
                        serviceIn.put("fromDate", nowTimestamp);
                        serviceIn.put("contentIdTo", currentSubContentId);
                        serviceIn.put("roleTypeList", roleTypeList);
                        serviceIn.put("targetOperationList", targetOperationList);
                        serviceIn.put("contentPurposeList", contentPurposeList);
                        results = dispatcher.runSync("createContentAssoc", serviceIn);
                        responseMessage = (String)results.get(ModelService.RESPONSE_MESSAGE);
                        if (UtilValidate.isNotEmpty(responseMessage)) {
                            errorMessage = (String)results.get(ModelService.ERROR_MESSAGE);
                            Debug.logError("in updatePublishLinks, serviceIn:" + serviceIn , module);
                            Debug.logError(errorMessage, module);
                            request.setAttribute("_ERROR_MESSAGE_", errorMessage);
                            return "error";
                        }
                        serviceIn = new HashMap<String, Object>();
                        serviceIn.put("userLogin", userLogin);
                        serviceIn.put("contentId", targContentId);
                        serviceIn.put("contentAssocTypeId", "PUBLISH_LINK");
                        serviceIn.put("fromDate", nowTimestamp);
                        serviceIn.put("contentIdTo", contentId);
                        serviceIn.put("roleTypeList", roleTypeList);
                        serviceIn.put("targetOperationList", targetOperationList);
                        serviceIn.put("contentPurposeList", contentPurposeList);
                       
                        results = dispatcher.runSync("createContentAssoc", serviceIn);
                       
                        if (!statusIdUpdated) {
                            try {
                                GenericValue targContent = EntityQuery.use(delegator).from("Content").where("contentId", targContentId).queryOne();
                                targContent.set("statusId", "CTNT_PUBLISHED");
                                targContent.store();
                                statusIdUpdated = true;
                            } catch (GenericEntityException e) {
                                Debug.logError(e.getMessage(), module);
                                request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
                                return "error";
                            }
                        }
                    }
                } else if (UtilValidate.isNotEmpty(origSubContentId)) {
                   
                    List<GenericValue> oldActiveValues = EntityQuery.use(delegator).from("ContentAssoc")
                            .where("contentId", targContentId, 
                                    "contentIdTo", origSubContentId, 
                                    "contentAssocTypeId", "PUBLISH_LINK", 
                                    "thruDate", null)
                            .queryList();
                    for (GenericValue cAssoc : oldActiveValues) {
                        cAssoc.set("thruDate", nowTimestamp);
                        cAssoc.store();
                    }
                }
            } catch (GenericEntityException e) {
                Debug.logError(e.getMessage(), module);
                request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
                return "error";
            } catch (GenericServiceException e2) {
                Debug.logError(e2, module);
                request.setAttribute("_ERROR_MESSAGE_", e2.getMessage());
                return "error";
            }
        }
        return "success";
    }
public static string updatepublishlinks(httpservletrequest request, httpservletresponse response) { httpsession session = request.getsession(); security security = (security)request.getattribute("security"); genericvalue userlogin = (genericvalue)session.getattribute("userlogin"); servletcontext servletcontext = session.getservletcontext(); string websiteid = websiteworker.getwebsiteid(request); delegator delegator = (delegator)request.getattribute("delegator"); localdispatcher dispatcher = (localdispatcher)request.getattribute("dispatcher"); map<string, object> parammap = utilhttp.getparametermap(request); string targcontentid = (string)parammap.get("contentid"); string roles = null; string authorid = null; genericvalue authorcontent = contentmanagementworker.getauthorcontent(delegator, targcontentid); if (authorcontent != null) { authorid = authorcontent.getstring("contentid"); } else { request.setattribute("_error_message_", "authorcontent is empty."); return "error"; } string userloginid = userlogin.getstring("userloginid"); list<string> roletypelist = null; if (authorid != null && userloginid != null && authorid.equals(userloginid)) { roles = "owner"; roletypelist = stringutil.split(roles, "|"); } list<string> targetoperationlist = utilmisc.<string>tolist("content_publish"); list<string> contentpurposelist = null; string permittedaction = (string)parammap.get("permittedaction"); string permittedoperations = (string)parammap.get("permittedoperations"); if (utilvalidate.isempty(targcontentid)) { request.setattribute("_error_message_", "targcontentid is empty."); return "error"; } list<object []> origpublishedlinklist = null; try { entityquery.use(delegator).from("userlogin").where("userloginid", authorid).cache().queryone(); origpublishedlinklist = contentmanagementworker.getpublishedlinks(delegator, targcontentid, websiteid, userlogin, security, permittedaction, permittedoperations, roles); } catch (genericentityexception e) { request.setattribute("_error_message_", e.getmessage()); return "error"; } catch (generalexception e2) { request.setattribute("_error_message_", e2.getmessage()); return "error"; } map<string, string> siteidlookup = new hashmap<string, string>(); for (string param : parammap.keyset()) { int pos = param.indexof("select_"); if (pos >= 0) { string siteid = param.substring(7); string subsiteval = (string)parammap.get(param); siteidlookup.put(siteid, subsiteval); } } timestamp nowtimestamp = utildatetime.nowtimestamp(); string responsemessage = null; string errormessage = null; boolean statusidupdated = false; map<string, object> results = null; for (object [] arr : origpublishedlinklist) { string contentid = (string)arr[0]; string origsubcontentid = null; list<object []> origsublist = utilgenerics.checklist(arr[1]); timestamp origfromdate = null; for (object [] pubarr : origsublist) { timestamp fromdate = (timestamp)pubarr[2]; origsubcontentid = null; if (fromdate != null) { origsubcontentid = (string)pubarr[0]; origfromdate = fromdate; break; } } string currentsubcontentid = siteidlookup.get(contentid); try { if (utilvalidate.isnotempty(currentsubcontentid)) { if (!currentsubcontentid.equals(origsubcontentid)) { if (utilvalidate.isnotempty(origsubcontentid) && origfromdate != null) { list<genericvalue> oldactivevalues = entityquery.use(delegator).from("contentassoc") .where("contentid", targcontentid, "contentidto", origsubcontentid, "contentassoctypeid", "publish_link", "thrudate", null) .querylist(); for (genericvalue cassoc : oldactivevalues) { cassoc.set("thrudate", nowtimestamp); cassoc.store(); } } map<string, object> servicein = new hashmap<string, object>(); servicein.put("userlogin", userlogin); servicein.put("contentid", targcontentid); servicein.put("contentassoctypeid", "publish_link"); servicein.put("fromdate", nowtimestamp); servicein.put("contentidto", currentsubcontentid); servicein.put("roletypelist", roletypelist); servicein.put("targetoperationlist", targetoperationlist); servicein.put("contentpurposelist", contentpurposelist); results = dispatcher.runsync("createcontentassoc", servicein); responsemessage = (string)results.get(modelservice.response_message); if (utilvalidate.isnotempty(responsemessage)) { errormessage = (string)results.get(modelservice.error_message); debug.logerror("in updatepublishlinks, servicein:" + servicein , module); debug.logerror(errormessage, module); request.setattribute("_error_message_", errormessage); return "error"; } servicein = new hashmap<string, object>(); servicein.put("userlogin", userlogin); servicein.put("contentid", targcontentid); servicein.put("contentassoctypeid", "publish_link"); servicein.put("fromdate", nowtimestamp); servicein.put("contentidto", contentid); servicein.put("roletypelist", roletypelist); servicein.put("targetoperationlist", targetoperationlist); servicein.put("contentpurposelist", contentpurposelist); results = dispatcher.runsync("createcontentassoc", servicein); if (!statusidupdated) { try { genericvalue targcontent = entityquery.use(delegator).from("content").where("contentid", targcontentid).queryone(); targcontent.set("statusid", "ctnt_published"); targcontent.store(); statusidupdated = true; } catch (genericentityexception e) { debug.logerror(e.getmessage(), module); request.setattribute("_error_message_", e.getmessage()); return "error"; } } } } else if (utilvalidate.isnotempty(origsubcontentid)) { list<genericvalue> oldactivevalues = entityquery.use(delegator).from("contentassoc") .where("contentid", targcontentid, "contentidto", origsubcontentid, "contentassoctypeid", "publish_link", "thrudate", null) .querylist(); for (genericvalue cassoc : oldactivevalues) { cassoc.set("thrudate", nowtimestamp); cassoc.store(); } } } catch (genericentityexception e) { debug.logerror(e.getmessage(), module); request.setattribute("_error_message_", e.getmessage()); return "error"; } catch (genericserviceexception e2) { debug.logerror(e2, module); request.setattribute("_error_message_", e2.getmessage()); return "error"; } } return "success"; }