001/* 002 * cilla - Blog Management System 003 * 004 * Copyright (C) 2012 Richard "Shred" Körber 005 * http://cilla.shredzone.org 006 * 007 * This program is free software: you can redistribute it and/or modify 008 * it under the terms of the GNU Affero General Public License as published 009 * by the Free Software Foundation, either version 3 of the License, or 010 * (at your option) any later version. 011 * 012 * This program is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 015 * GNU General Public License for more details. 016 * 017 * You should have received a copy of the GNU Affero General Public License 018 * along with this program. If not, see <http://www.gnu.org/licenses/>. 019 */ 020package org.shredzone.cilla.ws.assembler; 021 022import javax.annotation.Resource; 023 024import org.hibernate.criterion.ProjectionList; 025import org.hibernate.criterion.Projections; 026import org.hibernate.criterion.Property; 027import org.shredzone.cilla.core.model.Role; 028import org.shredzone.cilla.core.model.User; 029import org.shredzone.cilla.core.repository.LanguageDao; 030import org.shredzone.cilla.core.repository.RoleDao; 031import org.shredzone.cilla.service.UserService; 032import org.shredzone.cilla.ws.exception.CillaServiceException; 033import org.shredzone.cilla.ws.user.UserDto; 034import org.springframework.stereotype.Component; 035 036/** 037 * {@link Assembler} for {@link UserDto} and {@link User}. 038 * <p> 039 * Projections are supported. 040 * 041 * @author Richard "Shred" Körber 042 */ 043@Component 044public class UserAssembler extends AbstractAssembler<User, UserDto> { 045 046 private @Resource LanguageDao languageDao; 047 private @Resource RoleDao roleDao; 048 private @Resource UserService userService; 049 050 @Override 051 public UserDto assemble(User entity) throws CillaServiceException { 052 UserDto dto = new UserDto(); 053 dto.setId(entity.getId()); 054 dto.setLogin(entity.getLogin()); 055 dto.setName(entity.getName()); 056 dto.setMail(entity.getMail()); 057 dto.setRoleName(entity.getRole().getName()); 058 dto.setLanguageId(entity.getLanguage().getId()); 059 dto.setLanguage(entity.getLanguage().getLocale()); 060 dto.setTimeZone(entity.getTimeZone()); 061 return dto; 062 } 063 064 @Override 065 public void merge(UserDto dto, User entity) throws CillaServiceException { 066 super.merge(dto, entity); 067 entity.setName(dto.getName()); 068 entity.setMail(dto.getMail()); 069 entity.setTimeZone(dto.getTimeZone()); 070 071 if (!(entity.getLogin().equals(dto.getLogin()))) { 072 userService.changeLogin(entity, dto.getLogin()); 073 } 074 075 Role role = roleDao.fetchByName(dto.getRoleName()); 076 if (!(entity.getRole().equals(role))) { 077 userService.changeRole(entity, role); 078 } 079 080 entity.setLanguage(languageDao.fetch(dto.getLanguageId())); 081 } 082 083 @Override 084 public ProjectionList projection() { 085 ProjectionList projection = Projections.projectionList(); 086 projection.add(Projections.id(), "id"); 087 projection.add(Property.forName("login") .as("login")); 088 projection.add(Property.forName("name") .as("name")); 089 projection.add(Property.forName("mail") .as("mail")); 090 projection.add(Property.forName("timeZone") .as("timeZone")); 091 projection.add(Property.forName("r.name") .as("roleName")); 092 projection.add(Property.forName("l.id") .as("languageId")); 093 projection.add(Property.forName("l.locale") .as("language")); 094 return projection; 095 } 096 097}