/*
 * Copyright (C) 2000 - 2011 Silverpeas
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * As a special exception to the terms and conditions of version 3.0 of
 * the GPL, you may redistribute this Program in connection withWriter Free/Libre
 * Open Source Software ("FLOSS") applications as described in Silverpeas's
 * FLOSS exception.  You should have recieved a copy of the text describing
 * the FLOSS exception, and it is also available here:
 * "http://www.silverpeas.org/legal/licensing"
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.silverpeas.pdc.service;

import com.silverpeas.node.notification.NodeDeletionNotification;
import com.silverpeas.notification.DefaultNotificationSubscriber;
import com.silverpeas.notification.NotificationTopic;
import com.silverpeas.notification.SilverpeasNotification;
import com.stratelia.silverpeas.silverpeasinitialize.CallBack;
import com.stratelia.silverpeas.silverpeasinitialize.CallBackManager;
import com.stratelia.webactiv.util.node.model.NodePK;
import javax.inject.Inject;
import javax.inject.Named;
import static com.silverpeas.notification.NotificationTopic.onTopic;
import static com.silverpeas.notification.RegisteredTopics.*;

/**
 * Listener of notifications about some events that can have an impact on the PdC or on the
 * classification on the PdC.
 */
@Named
public class PdcNotificationListener extends DefaultNotificationSubscriber implements CallBack {
  
  @Inject
  private PdcClassificationService service;
  
  @Override
  public void subscribeOnTopics() {
    CallBackManager callBackManager = CallBackManager.get();
    //callBackManager.subscribeAction(CallBackManager.ACTION_BEFORE_REMOVE_COMPONENT, this);
    subscribeForNotifications(onTopic(NODE_TOPIC.getTopicName()));
  }

  @Override
  public void unsubscribeOnTopics() {
    CallBackManager callBackManager = CallBackManager.get();
    callBackManager.unsubscribeAll(this);
    unsubscribeForNotifications(onTopic(NODE_TOPIC.getTopicName()));
  }

  @Override
  public void onNotification(SilverpeasNotification notification, NotificationTopic onTopic) {
    if (onTopic.getName().equals(NODE_TOPIC.getTopicName())) {
      NodeDeletionNotification deletion = (NodeDeletionNotification) notification;
      NodePK node = deletion.getNodePK();
      service.deleteAllPreDefinedClassifications(node.getId(), node.getInstanceId());
    }
  }

  @Override
  public void subscribe() {
  }

  @Override
  public void doInvoke(int action, int iParam, String sParam, Object extraParam) {
    if (action == CallBackManager.ACTION_BEFORE_REMOVE_COMPONENT) {
        String componentId = sParam;
        service.deleteAllPreDefinedClassifications(null, componentId);
    }
  }
}

@@ -2184,18 +2184,16 @@
 ();%0A    
-//
 callBack

remove an old commit here.