Skip to content

Commit

Permalink
#269 AutoRegistrableDocumentViewSelectionFactory
Browse files Browse the repository at this point in the history
to be able to easily plugin more factories in the future
  • Loading branch information
teosarca committed Mar 27, 2017
1 parent b0bc638 commit fcd13ba
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

import org.compiere.util.CCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import de.metas.ui.web.process.descriptor.ProcessDescriptorsFactory;
import de.metas.ui.web.view.AutoRegistrableDocumentViewSelectionFactory;
import de.metas.ui.web.view.IDocumentViewSelection;
import de.metas.ui.web.view.IDocumentViewSelectionFactory;
import de.metas.ui.web.view.descriptor.DocumentViewLayout;
Expand Down Expand Up @@ -46,7 +46,7 @@
* #L%
*/

@Service
@AutoRegistrableDocumentViewSelectionFactory(windowId = WEBUI_HU_Constants.WEBUI_HU_Window_ID, viewType = JSONViewDataType.grid)
public class HUDocumentViewSelectionFactory implements IDocumentViewSelectionFactory
{
@Autowired
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package de.metas.ui.web.view;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.stereotype.Service;

import de.metas.ui.web.window.datatypes.json.JSONViewDataType;

/*
* #%L
* metasfresh-webui-api
* %%
* Copyright (C) 2017 metas GmbH
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/

/**
* Used to annotate {@link IDocumentViewSelectionFactory} implementations which shall be automatically discovered and registered.
*
* @author metas-dev <dev@metasfresh.com>
*
*/
@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Service
public @interface AutoRegistrableDocumentViewSelectionFactory
{
int windowId();

JSONViewDataType viewType();
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package de.metas.ui.web.view;

import java.util.Map;

import javax.annotation.PostConstruct;
import java.util.concurrent.ConcurrentHashMap;

import org.compiere.util.Util.ArrayKey;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableMap;
import com.google.common.base.Preconditions;

import de.metas.ui.web.handlingunits.HUDocumentViewSelectionFactory;
import de.metas.ui.web.handlingunits.WEBUI_HU_Constants;
import de.metas.logging.LogManager;
import de.metas.ui.web.view.json.JSONCreateDocumentViewRequest;
import de.metas.ui.web.view.json.JSONDocumentViewLayout;
import de.metas.ui.web.window.datatypes.json.JSONOptions;
Expand All @@ -32,39 +31,38 @@
*
* 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
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/

@Service
@Primary
public class CompositeDocumentViewSelectionFactory implements IDocumentViewSelectionFactory
public class CompositeDocumentViewSelectionFactory
{
private Map<ArrayKey, IDocumentViewSelectionFactory> factories = ImmutableMap.of();
private static final transient Logger logger = LogManager.getLogger(CompositeDocumentViewSelectionFactory.class);

private final ConcurrentHashMap<ArrayKey, IDocumentViewSelectionFactory> factories = new ConcurrentHashMap<>();
@Autowired
private SqlDocumentViewSelectionFactory defaultFactory;
@Autowired
private HUDocumentViewSelectionFactory huViewFactory;

public CompositeDocumentViewSelectionFactory()
@Autowired
public CompositeDocumentViewSelectionFactory(final ApplicationContext context)
{
super();
//
// Discover context factories
for (final Object factoryObj : context.getBeansWithAnnotation(AutoRegistrableDocumentViewSelectionFactory.class).values())
{
final IDocumentViewSelectionFactory factory = (IDocumentViewSelectionFactory)factoryObj;
final AutoRegistrableDocumentViewSelectionFactory annotation = factoryObj.getClass().getAnnotation(AutoRegistrableDocumentViewSelectionFactory.class);
registerFactory(annotation.windowId(), annotation.viewType(), factory);
}
}

@PostConstruct
private void init()
{
factories = ImmutableMap.<ArrayKey, IDocumentViewSelectionFactory> builder()
.put(mkKey(WEBUI_HU_Constants.WEBUI_HU_Window_ID, JSONViewDataType.grid), huViewFactory)
.build();
}

@Override
public String toString()
{
Expand All @@ -74,35 +72,40 @@ public String toString()
.toString();
}

public void registerFactory(final int windowId, final JSONViewDataType viewType, final IDocumentViewSelectionFactory factory)
{
Preconditions.checkNotNull(factory, "factory is null");
factories.put(mkKey(windowId, viewType), factory);
logger.info("Registered {} for windowId={}, viewType={}", factory, windowId, viewType);
}

private final IDocumentViewSelectionFactory getFactory(final int adWindowId, final JSONViewDataType viewType)
{
IDocumentViewSelectionFactory factory = factories.get(mkKey(adWindowId, viewType));
if(factory != null)
if (factory != null)
{
return factory;
}

factory = factories.get(mkKey(adWindowId, null));
if(factory != null)
if (factory != null)
{
return factory;
}

return defaultFactory;
}

private static final ArrayKey mkKey(final int adWindowId, final JSONViewDataType viewType)
{
return ArrayKey.of((adWindowId <= 0 ? 0 : adWindowId), viewType);
return ArrayKey.of(adWindowId <= 0 ? 0 : adWindowId, viewType);
}

@Override
public JSONDocumentViewLayout getViewLayout(final int adWindowId, final JSONViewDataType viewDataType, final JSONOptions jsonOpts)
{
return getFactory(adWindowId, viewDataType).getViewLayout(adWindowId, viewDataType, jsonOpts);
}

@Override
public IDocumentViewSelection createView(final JSONCreateDocumentViewRequest jsonRequest)
{
final int adWindowId = jsonRequest.getAD_Window_ID();
Expand Down

0 comments on commit fcd13ba

Please sign in to comment.