--- /dev/null
+/**
+ * Copyright (c) 2000-2012 Liferay, Inc. 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 com.pentila.entSavoie.cdt.service.impl;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import com.liferay.portal.kernel.log.Log;
+import com.liferay.portal.kernel.log.LogFactoryUtil;
+import com.pentila.entSavoie.cahierDeTexte.util.CDTColorUtil;
+import com.pentila.entSavoie.cdt.model.TeacherGroupColor;
+import com.pentila.entSavoie.cdt.service.base.TeacherGroupColorLocalServiceBaseImpl;
+
+/**
+ * The implementation of the teacher group color local service.
+ *
+ * <p>
+ * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link com.pentila.entSavoie.cdt.service.TeacherGroupColorLocalService} interface.
+ *
+ * <p>
+ * This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
+ * </p>
+ *
+ * @author Cedric Lecarpentier
+ * @see com.pentila.entSavoie.cdt.service.base.TeacherGroupColorLocalServiceBaseImpl
+ * @see com.pentila.entSavoie.cdt.service.TeacherGroupColorLocalServiceUtil
+ */
+public class TeacherGroupColorLocalServiceImpl
+ extends TeacherGroupColorLocalServiceBaseImpl {
+
+ private static Log _log = LogFactoryUtil.getLog(TeacherGroupColorLocalServiceImpl.class);
+
+ public List<TeacherGroupColor> getTeacherGroupColorForGroupIds(long teacherId, Long[] groupIdList) {
+
+ List<TeacherGroupColor> result = new ArrayList<TeacherGroupColor>();
+
+ // The list of groupIds for which we do not have a color yet
+ List<Long> missingColorGroupIds = new ArrayList<Long>();
+
+ // The number of groups for which we have a color defined
+ int nbExistingGroupColors = 0;
+
+ List<TeacherGroupColor> existingTeacherGroupColorList = null;
+ try {
+ existingTeacherGroupColorList = teacherGroupColorPersistence.findByteacherId(teacherId);
+ } catch (Exception e1) {
+ _log.error("Error when retrieving the list of colors for given teacherId "+teacherId);
+ }
+ if (existingTeacherGroupColorList != null) {
+ result.addAll(existingTeacherGroupColorList);
+ }
+
+ // Loop over list of groupIds
+ if (groupIdList != null) {
+
+ for (Long groupId : groupIdList) {
+
+ boolean colorExists = false;
+
+ // Loop over the list of existing groupIds
+ if (existingTeacherGroupColorList != null) {
+ for (TeacherGroupColor teacherGroupColor : existingTeacherGroupColorList) {
+ if (teacherGroupColor.getGroupId() == groupId) {
+ colorExists = true;
+ }
+ }
+ }
+
+ if (colorExists) {
+ nbExistingGroupColors++;
+ } else {
+ missingColorGroupIds.add(groupId);
+ }
+ }
+ }
+
+ // Now loop over the remaining groupIds for which a color needs to be defined
+ for (Long groupId : missingColorGroupIds) {
+
+ try {
+ long teacherGroupColorId = counterLocalService.increment();
+ TeacherGroupColor teacherGroupColor = teacherGroupColorPersistence.create(teacherGroupColorId);
+ teacherGroupColor.setGroupId(groupId);
+ teacherGroupColor.setTeacherId(teacherId);
+ String color = CDTColorUtil.getColorForIndex(nbExistingGroupColors);
+ teacherGroupColor.setColor(color);
+ teacherGroupColor = teacherGroupColorPersistence.update(teacherGroupColor, false);
+ result.add(teacherGroupColor);
+ _log.info("Adding color "+color+" to teacherId "+teacherId+" and groupId "+groupId);
+ } catch (Exception e) {
+ _log.error("Could not create teacher group color", e);
+ }
+ nbExistingGroupColors++;
+
+ }
+
+ return result;
+ }
+
+ /**
+ * Returns TeacherGroupColor for given teacherId and groupId
+ * Create it if it does not exist
+ * @param teacherId
+ * @param groupId
+ * @return
+ */
+ public TeacherGroupColor getTeacherGroupColorForGroupId(long teacherId, long groupId) {
+
+ TeacherGroupColor res = null;
+ try {
+ List<TeacherGroupColor> teacherGroupColorList = teacherGroupColorPersistence.findByteacherId_groupId(teacherId, groupId);
+ if (teacherGroupColorList != null && teacherGroupColorList.size() > 0) {
+ res = teacherGroupColorList.get(0);
+ } else {
+ // Create new one
+ long teacherGroupColorId = counterLocalService.increment();
+ TeacherGroupColor teacherGroupColor = teacherGroupColorPersistence.create(teacherGroupColorId);
+ teacherGroupColor.setGroupId(groupId);
+ teacherGroupColor.setTeacherId(teacherId);
+ String color = CDTColorUtil.getRandomColor();
+ teacherGroupColor.setColor(color);
+ teacherGroupColor = teacherGroupColorPersistence.update(teacherGroupColor, false);
+ res = teacherGroupColor;
+ }
+ } catch (Exception e) {
+ _log.error("Error when retrieving the list of colors for teacherId "+teacherId + " and groupId "+groupId);
+ }
+ return res;
+ }
+}
\ No newline at end of file