/**
* ***************************************************************************
* Copyright (c) 2010 Qcadoo Limited
* Project: Qcadoo Framework
* Version: 1.4
*
* This file is part of Qcadoo.
*
* Qcadoo 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.
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
* ***************************************************************************
*/
package com.qcadoo.plugins.plugins.internal;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.google.common.base.Preconditions;
import com.qcadoo.model.api.DataDefinition;
import com.qcadoo.model.api.DataDefinitionService;
import com.qcadoo.model.api.Entity;
import com.qcadoo.plugin.constants.QcadooPluginConstants;
import com.qcadoo.view.api.ComponentState;
import com.qcadoo.view.api.ViewDefinitionState;
import com.qcadoo.view.api.components.GridComponent;
@Service
public class PluginManagmentViewHook {
@Autowired
private PluginManagmentPerformer pluginManagmentPerformer;
@Autowired
private DataDefinitionService dataDefinitionService;
public void onDownloadButtonClick(final ViewDefinitionState viewDefinitionState, final ComponentState triggerState,
final String[] args) {
viewDefinitionState.openModal("../pluginPages/downloadPage.html");
}
public void onEnableButtonClick(final ViewDefinitionState viewDefinitionState, final ComponentState triggerState,
final String[] args) {
viewDefinitionState.openModal(pluginManagmentPerformer.performEnable(getPluginIdentifiersFromView(viewDefinitionState)));
}
public void onDisableButtonClick(final ViewDefinitionState viewDefinitionState, final ComponentState triggerState,
final String[] args) {
viewDefinitionState.openModal(pluginManagmentPerformer.performDisable(getPluginIdentifiersFromView(viewDefinitionState)));
}
public void onRemoveButtonClick(final ViewDefinitionState viewDefinitionState, final ComponentState triggerState,
final String[] args) {
String url = pluginManagmentPerformer.performRemove(getPluginIdentifiersFromView(viewDefinitionState));
if (url.contains("type=success")) {
GridComponent grid = (GridComponent) viewDefinitionState.getComponentByReference("grid");
grid.setSelectedEntitiesIds(new HashSet<Long>());
}
viewDefinitionState.openModal(url);
}
private List<String> getPluginIdentifiersFromView(final ViewDefinitionState viewDefinitionState) {
List<String> pluginIdentifiers = new LinkedList<String>();
GridComponent grid = (GridComponent) viewDefinitionState.getComponentByReference("grid");
Preconditions.checkState(grid.getSelectedEntitiesIds().size() > 0, "No record selected");
DataDefinition pluginDataDefinition = dataDefinitionService.get(QcadooPluginConstants.PLUGIN_IDENTIFIER,
QcadooPluginConstants.MODEL_PLUGIN);
for (Long entityId : grid.getSelectedEntitiesIds()) {
Entity pluginEntity = pluginDataDefinition.get(entityId);
pluginIdentifiers.add(pluginEntity.getStringField("identifier"));
}
return pluginIdentifiers;
}
}