001/*
002 * (C) Copyright 2015 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 */
018
019package org.nuxeo.scim.server.mapper;
020
021import java.net.URI;
022
023import org.nuxeo.ecm.core.api.DocumentModel;
024import org.nuxeo.ecm.core.api.NuxeoException;
025import org.nuxeo.ecm.platform.usermanager.UserManager;
026import org.nuxeo.runtime.api.Framework;
027
028import com.unboundid.scim.data.GroupResource;
029import com.unboundid.scim.data.Meta;
030import com.unboundid.scim.data.UserResource;
031import com.unboundid.scim.schema.CoreSchema;
032
033/**
034 * Base class used for mappers
035 *
036 * @author tiry
037 * @since 7.4
038 */
039public abstract class AbstractMapper {
040
041    protected UserManager um;
042
043    protected final String baseUrl;
044
045    public AbstractMapper(String baseUrl) {
046        um = Framework.getLocalService(UserManager.class);
047        this.baseUrl = baseUrl;
048    }
049
050    public GroupResource getGroupResourceFromNuxeoGroup(DocumentModel groupModel) throws Exception {
051
052        GroupResource groupResource = new GroupResource(CoreSchema.GROUP_DESCRIPTOR);
053
054        String groupId = (String) groupModel.getProperty(um.getGroupSchemaName(), um.getGroupIdField());
055
056        URI location = new URI(baseUrl + "/" + groupId);
057        Meta meta = new Meta(null, null, location, "1");
058        groupResource.setMeta(meta);
059
060        String groupLabel = (String) groupModel.getProperty(um.getGroupSchemaName(), um.getGroupLabelField());
061
062        groupResource.setDisplayName(groupLabel);
063        groupResource.setId(groupId);
064        groupResource.setExternalId(groupId);
065
066        return groupResource;
067    }
068
069    public DocumentModel createGroupModelFromGroupResource(GroupResource group) throws NuxeoException {
070
071        if (um.getGroup(group.getId()) == null) {
072            DocumentModel newGroup = um.getBareGroupModel();
073
074            String groupId = group.getId();
075            if (groupId == null || groupId.isEmpty()) {
076                groupId = group.getDisplayName();
077            }
078            newGroup.setProperty(um.getGroupSchemaName(), um.getGroupIdField(), groupId);
079            updateGroupModel(newGroup, group);
080            return um.createGroup(newGroup);
081        } else {
082            return updateGroupModelFromGroupResource(group.getId(), group);
083        }
084    }
085
086    public DocumentModel updateGroupModelFromGroupResource(String uid, GroupResource group) throws NuxeoException {
087
088        DocumentModel groupModel = um.getGroupModel(uid);
089        if (groupModel == null) {
090            return null;
091        }
092        updateGroupModel(groupModel, group);
093        um.updateGroup(groupModel);
094        return groupModel;
095    }
096
097    protected void updateGroupModel(DocumentModel userModel, GroupResource groupResouce) throws NuxeoException {
098        String displayName = groupResouce.getDisplayName();
099        if (displayName != null && !displayName.isEmpty()) {
100            userModel.setProperty(um.getGroupSchemaName(), um.getGroupLabelField(), displayName);
101        }
102    }
103
104    public abstract UserResource getUserResourceFromNuxeoUser(DocumentModel userModel) throws Exception;
105
106    public abstract DocumentModel createNuxeoUserFromUserResource(UserResource user) throws NuxeoException;
107
108    public abstract DocumentModel updateNuxeoUserFromUserResource(String uid, UserResource user);
109
110}