package kz.cafe.admin.panel_extensions;

import kz.cafe.admin.panels.*;
import kz.cafe.admin.panels.model.*;
import kz.cafe.db.*;
import kz.cafe.db.model.*;
import com.google.code.morphia.query.*;
import com.vaadin.ui.*;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.themes.*;

public class ExtraPanelUnPublishAll extends AbstractExtendedPanel implements java.io.Serializable{
   private static final long serialVersionUID = -8422307356783911085L;


   public ExtraPanelUnPublishAll(final DBListPanel<?> pnlParent){
      AbstractLayout panelLayout = (AbstractLayout) this.getContent();
      panelLayout.setMargin(false);
      
      this.setStyleName(Runo.PANEL_LIGHT);

      
      
      Button btPublishAll = new Button("Снять с публ.");
      btPublishAll.addListener(new Button.ClickListener() {
         private static final long serialVersionUID = -7828505809924280224L;

         @SuppressWarnings({ "unchecked", "rawtypes" })
         @Override
         public void buttonClick(ClickEvent event){
            CustomField customField = pnlParent.descriptor.mapDBFields.get(MAbstractEditableDataElement.F_PUBLISHED);
            
            if(customField == null){
            }
            else{
               for(Object id : pnlParent.table.getItemIds()){
                  pnlParent.table.getItem(id).getItemProperty(customField.name).setValue(false);

                  Query q = MongoService.ds.createQuery(pnlParent.classDB).filter(MAbstractEditableDataElement.F_ID, id);

                  UpdateOperations uOps =
                           MongoService.ds.createUpdateOperations(pnlParent.classDB)
                                          .set(MAbstractEditableDataElement.F_PUBLISHED, false)
                                          .set(MAbstractEditableDataElement.F_UPDATED, System.currentTimeMillis());
                  
                  try{
                     MongoService.ds.findAndModify(q, uOps);
                  }
                  catch (Exception e){
                  }
               }
            }
         }
      });
     
      this.addComponent(btPublishAll);
   }


   @Override
   public void changeFocus(){
   }
}
