/**
 * Copyright 2010 OpenEngSB Division, Vienna University of Technology
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.openengsb.ui.common.wicket.taskbox;

import java.lang.reflect.Constructor;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.markup.html.panel.Panel;
import org.openengsb.core.common.BundleContextAware;
import org.openengsb.core.common.persistence.PersistenceException;
import org.openengsb.core.common.persistence.PersistenceManager;
import org.openengsb.core.common.persistence.PersistenceService;
import org.openengsb.core.common.taskbox.TaskboxException;
import org.openengsb.core.common.taskbox.model.Task;
import org.openengsb.core.workflow.taskbox.TaskboxServiceImpl;
import org.openengsb.ui.common.wicket.taskbox.web.TaskOverviewPanel;
import org.openengsb.ui.common.wicket.taskbox.web.TaskPanel;
import org.osgi.framework.BundleContext;

public class WebTaskboxServiceImpl extends TaskboxServiceImpl implements WebTaskboxService, BundleContextAware {
    private Log log = LogFactory.getLog(getClass());

    private PersistenceService persistence;
    private PersistenceManager persistenceManager;
    private BundleContext bundleContext;

    @Override
    public void init() {
        persistence = persistenceManager.getPersistenceForBundle(bundleContext.getBundle());
    }

    @Override
    public void setPersistenceManager(PersistenceManager persistenceManager) {
        this.persistenceManager = persistenceManager;
    }

    @Override
    public void setBundleContext(BundleContext bundleContext) {
        this.bundleContext = bundleContext;
    }

    @Override
    public Panel getOverviewPanel() {
        return new TaskOverviewPanel("OverviewPanel");
    }

    @Override
    public Panel getTaskPanel(Task task, String wicketPanelId) throws TaskboxException {
        List<PanelRegistryEntry> panels = persistence.query(new PanelRegistryEntry(task.getTaskType()));

        Class<? extends Panel> panelClass;
        if (panels.size() > 0) {
            panelClass = panels.get(0).getPanelClass();
        } else {
            panelClass = TaskPanel.class;
        }

        try {
            Constructor<? extends Panel> panelConstructor = panelClass.getConstructor(String.class, Task.class);
            return panelConstructor.newInstance(wicketPanelId, task);
        } catch (Exception e) {
            throw new TaskboxException(e);
        }
    }

    @Override
    public void registerTaskPanel(String taskType, Class<? extends Panel> panelClass) throws TaskboxException {
        try {
            persistence.delete(new PanelRegistryEntry(taskType));
            persistence.create(new PanelRegistryEntry(taskType, panelClass));
            log.info("Successfully registered " + panelClass.getName() + " for task type " + taskType);
        } catch (PersistenceException e) {
            throw new TaskboxException(e);
        }
    }
}

@@ -3227,32 +3227,115 @@
 %0A        try %7B%0D%0A
+            if(persistence.query(new PanelRegistryEntry(taskType)).size()%3E0)%7B%0D%0A    
             pers
@@ -3381,24 +3381,39 @@
 askType));%0D%0A
+            %7D%0D%0A
             

Fix error with registerTaskPanel (no longer throws errors when adding a panel that cannot be deleted)