Java tutorial
/** * Copyright (c) 2015-present Jorge Daz All rights reserved. * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library 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 Lesser General Public License for more * details. */ package jorgediazest.missingrefchecker.chk; import com.liferay.portal.kernel.language.LanguageUtil; import com.liferay.portal.kernel.util.LocaleUtil; import com.liferay.portal.kernel.util.LocalizationUtil; import com.liferay.portal.kernel.util.StringPool; import com.liferay.portal.kernel.util.Validator; import com.liferay.portal.kernel.security.auth.CompanyThreadLocal; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.TreeSet; import jorgediazest.missingrefchecker.ref.Reference; import jorgediazest.util.data.Data; /** * @author Jorge Daz */ public class DefaultLanguageReferenceChecker extends ValuesReferenceChecker { @Override public boolean isValidData(Reference reference, boolean ignoreNullValues, Data d) { Long groupId = (Long) d.get("groupId"); Long companyId = (Long) d.get("companyId"); for (Entry<String, Object> e : d.getMap().entrySet()) { if (!validateAttribute(d, e.getKey())) { continue; } if (!isValidLanguage(e.getValue().toString(), groupId, companyId)) { return false; } } return true; } public boolean isValidLanguage(String value, Long groupId, Long companyId) { if (Validator.isNull(value)) { return true; } String defaultLanguageId = LocalizationUtil.getDefaultLanguageId(value, null); if (Validator.isNull(defaultLanguageId)) { return true; } if (groupId != null) { Set<String> availableLanguagesSet = availableLanguagesByGroup.get(groupId); if (availableLanguagesSet == null) { availableLanguagesSet = new TreeSet<String>(); Set<Locale> availableLocales = LanguageUtil.getAvailableLocales(groupId); for (Locale availableLocale : availableLocales) { availableLanguagesSet.add(LocaleUtil.toLanguageId(availableLocale)); } availableLanguagesByGroup.put(groupId, availableLanguagesSet); } return (availableLanguagesSet.contains(defaultLanguageId)); } if (companyId != null) { Set<String> availableLanguagesSet = availableLanguagesByCompany.get(companyId); if (availableLanguagesSet == null) { availableLanguagesSet = new TreeSet<String>(); long oldCompanyId = CompanyThreadLocal.getCompanyId(); CompanyThreadLocal.setCompanyId(companyId); Set<Locale> availableLocales = LanguageUtil.getAvailableLocales(); CompanyThreadLocal.setCompanyId(oldCompanyId); for (Locale availableLocale : availableLocales) { availableLanguagesSet.add(LocaleUtil.toLanguageId(availableLocale)); } availableLanguagesByCompany.put(companyId, availableLanguagesSet); } return (availableLanguagesSet.contains(defaultLanguageId)); } return false; } @Override public String printData(Reference reference, boolean ignoreNullValues, Data d) { List<String> defaultLanguageIds = new ArrayList<String>(); for (Entry<String, Object> e : d.getMap().entrySet()) { if (!validateAttribute(d, e.getKey())) { continue; } defaultLanguageIds.add(LocalizationUtil.getDefaultLanguageId(e.getValue().toString(), null)); } return StringPool.BLANK + d.getPrimaryKey() + StringPool.BLANK + defaultLanguageIds; } @Override public boolean validParameters(Reference reference, boolean ignoreNullValues) { List<String> attributes = reference.getOriginAttributesList(); if (!attributes.contains("groupId") && !attributes.contains("companyId")) { return false; } return true; } protected boolean validateAttribute(Data d, String attribute) { if (Validator.isNull(attribute) || "groupId".equals(attribute) || "companyId".equals(attribute) || d.getModel().getPrimaryKeyAttribute().equals(attribute)) { return false; } return true; } protected Map<Long, Set<String>> availableLanguagesByCompany = new HashMap<Long, Set<String>>(); protected Map<Long, Set<String>> availableLanguagesByGroup = new HashMap<Long, Set<String>>(); }