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.LayoutSet;
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 LayoutSetModelListener extends BaseModelListener<LayoutSet> {

	@Override
	public void onAfterRemove(LayoutSet layoutSet) {
		if ((layoutSet == null) || !layoutSet.isHead()) {
			return;
		}

		clearCache(layoutSet);
	}

	@Override
	public void onAfterUpdate(LayoutSet layoutSet) {
		if ((layoutSet == null) || !layoutSet.isHead()) {
			return;
		}

		clearCache(layoutSet);
	}

	protected void clearCache(LayoutSet layoutSet) {
		_fragmentRenderer.clearWidgetCache();
	}

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

	private FragmentRenderer _fragmentRenderer;

}