package com.liferay.fragment.internal.model.listener;

import com.liferay.fragment.renderer.FragmentRenderer;
import com.liferay.portal.kernel.model.BaseModelListener;
import com.liferay.portal.kernel.model.Layout;
import com.liferay.portal.kernel.model.ModelListener;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

@Component(immediate = true, service = ModelListener.class)
public class LayoutModelListener extends BaseModelListener<Layout> {

	@Override
	public void onAfterRemove(Layout layout) {
		clearCache(layout);
	}

	@Override
	public void onAfterUpdate(Layout layout) {
		clearCache(layout);
	}

	protected void clearCache(Layout layout) {
		if (layout == null) {
			return;
		}

		_fragmentRenderer.clearWidgetCache();
	}

	@Reference(
		target = "(fragment.renderer=fragmentEntryFragmentRenderer)",
		unbind = "-"
	)
	protected void setFragmentRenderer(FragmentRenderer fragmentRenderer) {
		_fragmentRenderer = fragmentRenderer;
	}

	private FragmentRenderer _fragmentRenderer;

}