Program: 101227
Project: 101227
Buggy source:
/** 
 * Add Application to UiPage
 * @param event
 * @throws Exception
 */
private void addApplicationToPage(Event<UIAddNewApplication> event) throws Exception {
  UIPortal uiPortal=Util.getUIPortal();
  UIPortalApplication uiPortalApp=uiPortal.getAncestorOfType(UIPortalApplication.class);
  UIPage uiPage=null;
  if (uiPortal.isRendered()) {
    uiPage=uiPortal.findFirstComponentOfType(UIPage.class);
  }
 else {
    UIPortalToolPanel uiPortalToolPanel=uiPortalApp.findFirstComponentOfType(UIPortalToolPanel.class);
    uiPage=uiPortalToolPanel.findFirstComponentOfType(UIPage.class);
  }
  String applicationId=event.getRequestContext().getRequestParameter(UIComponent.OBJECTID);
  Application application=getApplication(uiPortal,applicationId);
  if (org.exoplatform.web.application.Application.EXO_PORTLET_TYPE.equals(application.getApplicationType())) {
    UIPortlet uiPortlet=uiPage.createUIComponent(UIPortlet.class,null,null);
    StringBuilder windowId=new StringBuilder(uiPage.getOwnerType());
    windowId.append('#').append(uiPage.getOwnerId());
    windowId.append("String_Node_Str").append(applicationId).append('/').append(uiPortlet.hashCode());
    uiPortlet.setWindowId(windowId.toString());
    if (application != null) {
      if (application.getDisplayName() != null) {
        uiPortlet.setTitle(application.getDisplayName());
      }
 else       if (application.getApplicationName() != null) {
        uiPortlet.setTitle(application.getApplicationName());
      }
      uiPortlet.setDescription(application.getDescription());
    }
    uiPage.addChild(uiPortlet);
  }
 else   if (org.exoplatform.web.application.Application.EXO_WIDGET_TYPE.equals(application.getApplicationType())) {
    UIWidget uiWidget=uiPage.createUIComponent(event.getRequestContext(),UIWidget.class,null,null);
    StringBuilder windowId=new StringBuilder(Util.getUIPortal().getOwner());
    windowId.append("String_Node_Str").append(applicationId).append('/').append(uiWidget.hashCode());
    uiWidget.setApplicationInstanceId(windowId.toString());
    uiWidget.setApplicationName(application.getApplicationName());
    uiWidget.setApplicationGroup(application.getApplicationGroup());
    uiWidget.setApplicationOwnerType(application.getApplicationType());
    int posX=(int)(Math.random() * 400);
    int posY=(int)(Math.random() * 200);
    uiWidget.getProperties().put(UIApplication.locationX,String.valueOf(posX));
    uiWidget.getProperties().put(UIApplication.locationY,String.valueOf(posY));
    uiPage.addChild(uiWidget);
  }
 else {
    UIExoApplication uiExoApp=uiPage.createUIComponent(UIExoApplication.class,null,null);
    StringBuilder windowId=new StringBuilder(Util.getUIPortal().getOwner());
    windowId.append("String_Node_Str").append(applicationId).append('/').append(uiExoApp.hashCode());
    uiExoApp.setApplicationInstanceId(windowId.toString());
    uiExoApp.init();
    uiPage.addChild(uiExoApp);
  }
  if (uiPage.isModifiable()) {
    Page page=PortalDataMapper.toPageModel(uiPage);
    UserPortalConfigService configService=uiPortalApp.getApplicationComponent(UserPortalConfigService.class);
    if (page.getChildren() == null)     page.setChildren(new ArrayList<Object>());
    configService.update(page);
  }
  PortalRequestContext pcontext=Util.getPortalRequestContext();
  UIWorkspace uiWorkingWS=uiPortalApp.findComponentById(UIPortalApplication.UI_WORKING_WS_ID);
  pcontext.addUIComponentToUpdateByAjax(uiWorkingWS);
  pcontext.setFullRender(true);
}

Fixed version (ground truth):
/** 
 * Add Application to UiPage
 * @param event
 * @throws Exception
 */
private void addApplicationToPage(Event<UIAddNewApplication> event) throws Exception {
  UIPortal uiPortal=Util.getUIPortal();
  UIPortalApplication uiPortalApp=uiPortal.getAncestorOfType(UIPortalApplication.class);
  UIPage uiPage=null;
  if (uiPortal.isRendered()) {
    uiPage=uiPortal.findFirstComponentOfType(UIPage.class);
  }
 else {
    UIPortalToolPanel uiPortalToolPanel=uiPortalApp.findFirstComponentOfType(UIPortalToolPanel.class);
    uiPage=uiPortalToolPanel.findFirstComponentOfType(UIPage.class);
  }
  String applicationId=event.getRequestContext().getRequestParameter(UIComponent.OBJECTID);
  Application application=event.getSource().getApplication(applicationId);
  if (org.exoplatform.web.application.Application.EXO_PORTLET_TYPE.equals(application.getApplicationType())) {
    UIPortlet uiPortlet=uiPage.createUIComponent(UIPortlet.class,null,null);
    StringBuilder windowId=new StringBuilder(uiPage.getOwnerType());
    windowId.append('#').append(uiPage.getOwnerId());
    windowId.append("String_Node_Str").append(applicationId).append('/').append(uiPortlet.hashCode());
    uiPortlet.setWindowId(windowId.toString());
    if (application != null) {
      if (application.getDisplayName() != null) {
        uiPortlet.setTitle(application.getDisplayName());
      }
 else       if (application.getApplicationName() != null) {
        uiPortlet.setTitle(application.getApplicationName());
      }
      uiPortlet.setDescription(application.getDescription());
    }
    uiPage.addChild(uiPortlet);
  }
 else   if (org.exoplatform.web.application.Application.EXO_WIDGET_TYPE.equals(application.getApplicationType())) {
    UIWidget uiWidget=uiPage.createUIComponent(event.getRequestContext(),UIWidget.class,null,null);
    StringBuilder windowId=new StringBuilder(Util.getUIPortal().getOwner());
    windowId.append("String_Node_Str").append(applicationId).append('/').append(uiWidget.hashCode());
    uiWidget.setApplicationInstanceId(windowId.toString());
    uiWidget.setApplicationName(application.getApplicationName());
    uiWidget.setApplicationGroup(application.getApplicationGroup());
    uiWidget.setApplicationOwnerType(application.getApplicationType());
    int posX=(int)(Math.random() * 400);
    int posY=(int)(Math.random() * 200);
    uiWidget.getProperties().put(UIApplication.locationX,String.valueOf(posX));
    uiWidget.getProperties().put(UIApplication.locationY,String.valueOf(posY));
    uiPage.addChild(uiWidget);
  }
  if (uiPage.isModifiable()) {
    Page page=PortalDataMapper.toPageModel(uiPage);
    UserPortalConfigService configService=uiPortalApp.getApplicationComponent(UserPortalConfigService.class);
    if (page.getChildren() == null)     page.setChildren(new ArrayList<Object>());
    configService.update(page);
  }
  PortalRequestContext pcontext=Util.getPortalRequestContext();
  UIWorkspace uiWorkingWS=uiPortalApp.findComponentById(UIPortalApplication.UI_WORKING_WS_ID);
  pcontext.addUIComponentToUpdateByAjax(uiWorkingWS);
  pcontext.setFullRender(true);
}