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

import com.liferay.fragment.renderer.FragmentRenderer;
import com.liferay.journal.model.JournalArticle;
import com.liferay.portal.kernel.model.BaseModelListener;
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 JournalArticleModelListener
	extends BaseModelListener<JournalArticle> {

	@Override
	public void onAfterRemove(JournalArticle journalArticle) {
		clearCache(journalArticle);
	}

	@Override
	public void onAfterUpdate(JournalArticle journalArticle) {
		clearCache(journalArticle);
	}

	protected void clearCache(JournalArticle journalArticle) {
		if (journalArticle == null) {
			return;
		}

		_fragmentRenderer.clearWidgetCache();
	}

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

	private FragmentRenderer _fragmentRenderer;

}