Java tutorial
/* * Copyright (C) 2012-2013 NS Solutions Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package com.htmlhifive.sync.resource; import java.util.ArrayList; import java.util.Collections; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.PersistenceException; import org.hibernate.exception.ConstraintViolationException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.jpa.repository.JpaRepository; import com.htmlhifive.sync.config.SyncConfigurationParameter; import com.htmlhifive.sync.resource.common.ResourceItemCommonData; import com.htmlhifive.sync.resource.common.ResourceItemCommonDataId; import com.htmlhifive.sync.resource.common.ResourceItemCommonDataRepository; import com.htmlhifive.sync.resource.common.SyncAction; import com.htmlhifive.sync.resource.update.UpdateStrategy; import com.htmlhifive.sync.service.SyncRequestCommonData; /** * @author kishigam */ public class DefaultSynchronizer implements Synchronizer { /** * sync? */ private SyncConfigurationParameter syncConfigurationParameter; /** * JPA EntityManager.<br> * getNew????????{@link JpaRepository JpaRepository}??????. */ @PersistenceContext private EntityManager entityManager; /** * ????? */ @Autowired private ResourceItemCommonDataRepository resourceItemCommonDataRepository; /** * ???. */ private UpdateStrategy defaultUpdateStrategy; /** * ??ID??????????.<br/> * ???????{@link DefaultSynchronizer#modify(ResourceItemCommonData)} ?????????. * * @param itemCommonId ?ID * @return ??? */ @Override public ResourceItemCommonData getNew(ResourceItemCommonDataId itemCommonId) { ResourceItemCommonData common = new ResourceItemCommonData(itemCommonId); try { entityManager.persist(common); entityManager.flush(); } catch (PersistenceException e) { // ???(????) if (!(e.getCause() instanceof ConstraintViolationException)) { throw e; } common.setSyncAction(SyncAction.DUPLICATE); } return common; } /** * ??ID??("for update")??????. * * @param itemCommonId ?ID * @return */ @Override public ResourceItemCommonData getForUpdate(ResourceItemCommonDataId itemCommonId) { return resourceItemCommonDataRepository.findOneForUpdate(itemCommonId); } /** * ???ID????????????. * * @param resourceName ?? * @param targetItemIdList ?ID? * @param modifiedSince ?? * @return ??? */ @Override public List<ResourceItemCommonData> getModified(String resourceName, List<String> targetItemIdList, long modifiedSince) { List<ResourceItemCommonData> commonList = new ArrayList<>(); // ID??? List<String> tempList = new ArrayList<>(targetItemIdList); Collections.sort(tempList); for (String id : tempList) { ResourceItemCommonData common = resourceItemCommonDataRepository.findModified(resourceName, id, modifiedSince); if (common != null) { commonList.add(common); } } return commonList; } /** * ???ID????????("for update")??????.<br/> * ID???????????ID?????????. * * @param resourceName ?? * @param targetItemIdList ?ID? * @param modifiedSince ?? * @return ??? */ @Override public List<ResourceItemCommonData> getModifiedForUpdate(String resourceName, List<String> targetItemIdList, long modifiedSince) { List<ResourceItemCommonData> commonList = new ArrayList<>(); // ID??? List<String> tempList = new ArrayList<>(targetItemIdList); Collections.sort(tempList); for (String id : tempList) { ResourceItemCommonData common = resourceItemCommonDataRepository.findModifiedForUpdate(resourceName, id, modifiedSince); if (common != null) { commonList.add(common); } } return commonList; } /** * ?????????????true???. * * @param clientItemCommon update(/delete)? * @param currentItemCommon ????????? * @return ??????true. */ @Override public boolean isConflicted(ResourceItemCommonData clientItemCommon, ResourceItemCommonData currentItemCommon, SyncRequestCommonData requestCommon) { if (currentItemCommon.getLastModified() <= clientItemCommon.getLastModified()) { return false; } // ????????????? // sync?????????????? return currentItemCommon.getLastModified() != requestCommon.getSyncTime(); } /** * ???????. * * @param itemCommon * @return ? */ @Override public ResourceItemCommonData modify(ResourceItemCommonData itemCommon) { ResourceItemCommonData saved = resourceItemCommonDataRepository.save(itemCommon); return saved; } @Override public SyncConfigurationParameter getSyncConfigurationParameter() { return syncConfigurationParameter; } @Override public void setSyncConfigurationParameter(SyncConfigurationParameter syncConfigurationParameter) { this.syncConfigurationParameter = syncConfigurationParameter; } @Override public UpdateStrategy getDefaultUpdateStrategy() { return defaultUpdateStrategy; } @Override public void setDefaultUpdateStrategy(UpdateStrategy defaultUpdateStrategy) { this.defaultUpdateStrategy = defaultUpdateStrategy; } }