فهرست منبع

通讯录导出vcf

luojing 5 سال پیش
والد
کامیت
c343081fa5

+ 45 - 61
o2server/x_organization_assemble_control/src/main/java/com/x/organization/assemble/control/jaxrs/personcard/ActionExportPersonalVcf.java

@@ -1,112 +1,96 @@
 package com.x.organization.assemble.control.jaxrs.personcard;
 
-import java.io.BufferedWriter;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.OutputStreamWriter;
+import java.io.ByteArrayOutputStream;
 
-import java.util.ArrayList;
+
+import java.util.Arrays;
 import java.util.List;
 
 
 
 
-import com.google.gson.JsonElement;
 import com.x.base.core.container.EntityManagerContainer;
 import com.x.base.core.container.factory.EntityManagerContainerFactory;
-import com.x.base.core.project.annotation.FieldDescribe;
 import com.x.base.core.project.exception.ExceptionPersonNotExist;
-import com.x.base.core.project.gson.GsonPropertyObject;
 import com.x.base.core.project.http.ActionResult;
 import com.x.base.core.project.http.EffectivePerson;
-import com.x.base.core.project.jaxrs.WoId;
-
-import com.x.organization.assemble.control.Business;
-import com.x.organization.assemble.control.staticconfig.FollowConfig;
+import com.x.base.core.project.jaxrs.WoFile;
+import com.x.base.core.project.logger.Logger;
+import com.x.base.core.project.logger.LoggerFactory;
 import com.x.organization.core.entity.PersonCard;
 
 
 
 class ActionExportPersonalVcf extends BaseAction {
 
-
+	private static Logger logger = LoggerFactory.getLogger(ActionExportPersonalVcf.class);
 	//导出通讯录vcf
-	ActionResult<String> exportPersonalVcf(EffectivePerson effectivePerson, JsonElement jsonElement) throws Exception {
+	ActionResult<Wo> exportPersonalVcf(EffectivePerson effectivePerson, String idList) throws Exception {
 		try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
-			ActionResult<String> result = new ActionResult<>();
-			Business business = new Business(emc);
-			Wi wi = this.convertToWrapIn(jsonElement, Wi.class);
-			List<String> personIdList = wi.getPersonList();
+			ActionResult<Wo> result = new ActionResult<>();
+			List<String> personIdList = Arrays.asList(idList.split(","));
+			Wo wo = null;
 			if(!personIdList.isEmpty()){
-				//String path = "D:\\tmp\\export_contacts.vcf";
-				String path = FollowConfig.VCFPATH+"export_personal_contacts"+System.currentTimeMillis()+".vcf";
-				File file = new File(path);
-				if (file.exists()) {
-					file.createNewFile();
-				}
-				BufferedWriter reader = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
+				ByteArrayOutputStream os = new ByteArrayOutputStream();
+				
 				for (String flag : personIdList) {
 					PersonCard personCard = emc.find(flag, PersonCard.class);
 					if (null == personCard) {
 						throw new ExceptionPersonNotExist(flag);
 					}
-					reader.write("BEGIN:VCARD");
-					reader.write("\r\n");
-					reader.write("VERSION:3.0");
-					reader.write("\r\n");
-					reader.write("N:"+personCard.getName()+";;;;");
-					reader.write("\r\n");
-					reader.write("FN:"+personCard.getName());
-					reader.write("\r\n");	    
+					os.write("BEGIN:VCARD".getBytes());
+					os.write("\r\n".getBytes());
+					os.write("VERSION:3.0".getBytes());
+					os.write("\r\n".getBytes());
+					os.write(("N:"+personCard.getName()+";;;;").getBytes());
+					os.write("\r\n".getBytes());
+					os.write(("FN:"+personCard.getName()).getBytes());
+					os.write("\r\n".getBytes());	    
 					if(personCard.getOfficePhone()!=null && !personCard.getOfficePhone().equals("")){
-						reader.write("TEL:"+personCard.getOfficePhone());
-						reader.write("\r\n");
+						os.write(("TEL:"+personCard.getOfficePhone()).getBytes());
+						os.write(("\r\n").getBytes());
 					}
 					if(personCard.getMobile()!=null && !personCard.getMobile().equals("")){
-						reader.write("TEL;CELL:"+personCard.getMobile());
-						reader.write("\r\n");
+						os.write(("TEL;CELL:"+personCard.getMobile()).getBytes());
+						os.write("\r\n".getBytes());
 					}
 					if(personCard.getAddress()!=null && !personCard.getAddress().equals("")){
-						reader.write("ADR;HOME;POSTAL:;;"+personCard.getAddress());
-						reader.write("\r\n");
+						os.write(("ADR;HOME;POSTAL:;;"+personCard.getAddress()).getBytes());
+						os.write("\r\n".getBytes());
 					}
 					if(personCard.getGroupType()!=null && !personCard.getGroupType().equals("")){
-						reader.write("ORG:"+personCard.getGroupType());
-						reader.write("\r\n");
+						os.write(("ORG:"+personCard.getGroupType()).getBytes());
+						os.write("\r\n".getBytes());
 					}
 					if(personCard.getDescription()!=null && !personCard.getDescription().equals("")){
-						reader.write("NOTE:"+personCard.getDescription());
-						reader.write("\r\n");
+						os.write(("NOTE:"+personCard.getDescription()).getBytes());
+						os.write("\r\n".getBytes());
 					}									
-					reader.write("END:VCARD");
-					reader.write("\r\n");
+					os.write("END:VCARD".getBytes());
+					os.write("\r\n".getBytes());
 					
 				}
-				reader.flush();
-				reader.close();
-				result.setData(path);
+				String fileName = "export_personal_contacts"+System.currentTimeMillis()+".vcf";
+				try {
+					wo = new Wo(os.toByteArray(), 
+							this.contentType(false, fileName), 
+							this.contentDisposition(false, fileName));
+				} finally {
+					os.close();
+				}
+				result.setData(wo);
 			}
 
 			return result;
 		}
 	}
 	
-	public static class Wi extends GsonPropertyObject {
+	public static class Wo extends WoFile {
 
-		@FieldDescribe("个人通讯录人员标识")
-		private List<String> personList = new ArrayList<>();
-
-		public List<String> getPersonList() {
-			return personList;
-		}
-
-		public void setPersonList(List<String> personList) {
-			this.personList = personList;
+		public Wo(byte[] bytes, String contentType, String contentDisposition) {
+			super(bytes, contentType, contentDisposition);
 		}
 
 	}
-	public static class Wo extends WoId {
-		
-	}
 
 }

+ 50 - 61
o2server/x_organization_assemble_control/src/main/java/com/x/organization/assemble/control/jaxrs/personcard/ActionExportVcf.java

@@ -1,11 +1,7 @@
 package com.x.organization.assemble.control.jaxrs.personcard;
 
-import java.io.BufferedWriter;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.OutputStreamWriter;
-import java.io.UnsupportedEncodingException;
-import java.util.ArrayList;
+import java.io.ByteArrayOutputStream;
+import java.util.Arrays;
 import java.util.List;
 
 
@@ -25,9 +21,11 @@ import com.x.base.core.project.exception.ExceptionPersonNotExist;
 import com.x.base.core.project.gson.GsonPropertyObject;
 import com.x.base.core.project.http.ActionResult;
 import com.x.base.core.project.http.EffectivePerson;
+import com.x.base.core.project.jaxrs.WoFile;
 import com.x.base.core.project.jaxrs.WoId;
 import com.x.base.core.project.tools.DateTools;
 import com.x.organization.assemble.control.Business;
+import com.x.organization.assemble.control.jaxrs.personcard.ActionExportPersonalVcf.Wo;
 import com.x.organization.assemble.control.staticconfig.FollowConfig;
 import com.x.organization.core.entity.Identity;
 import com.x.organization.core.entity.Identity_;
@@ -43,59 +41,54 @@ class ActionExportVcf extends BaseAction {
 
 
 	//导出通讯录vcf
-	ActionResult<String> exportVcf(EffectivePerson effectivePerson, JsonElement jsonElement) throws Exception {
+	ActionResult<Wo> exportVcf(EffectivePerson effectivePerson, String idList) throws Exception {
 		try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
-			ActionResult<String> result = new ActionResult<>();
+			ActionResult<Wo> result = new ActionResult<>();
 			Business business = new Business(emc);
-			Wi wi = this.convertToWrapIn(jsonElement, Wi.class);
-			List<String> personIdList = wi.getPersonList();
+			List<String> personIdList = Arrays.asList(idList.split(","));
+			Wo wo = null;
 			if(!personIdList.isEmpty()){
-				//String path = "D:\\tmp\\export_contacts.vcf";
-				String path = FollowConfig.VCFPATH+"export_contacts"+System.currentTimeMillis()+".vcf";
-				File file = new File(path);
-				if (file.exists()) {
-					file.createNewFile();
-				}
-				BufferedWriter reader = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
+				ByteArrayOutputStream reader = new ByteArrayOutputStream();
+				
 				for (String flag : personIdList) {
 					Person personCard = emc.find(flag, Person.class);
 					if (null == personCard) {
 						throw new ExceptionPersonNotExist(flag);
 					}
-					reader.write("BEGIN:VCARD");
-					reader.write("\r\n");
-					reader.write("VERSION:3.0");
-					reader.write("\r\n");
-					reader.write("N:"+personCard.getName()+";;;;");
-					reader.write("\r\n");
-					reader.write("FN:"+personCard.getName());
-					reader.write("\r\n");	    
+					reader.write("BEGIN:VCARD".getBytes());
+					reader.write("\r\n".getBytes());
+					reader.write("VERSION:3.0".getBytes());
+					reader.write("\r\n".getBytes());
+					reader.write(("N:"+personCard.getName()+";;;;").getBytes());
+					reader.write("\r\n".getBytes());
+					reader.write(("FN:"+personCard.getName()).getBytes());
+					reader.write("\r\n".getBytes());	    
 					if(personCard.getOfficePhone()!=null && !personCard.getOfficePhone().equals("")){
-						reader.write("TEL:"+personCard.getOfficePhone());
-						reader.write("\r\n");
+						reader.write(("TEL:"+personCard.getOfficePhone()).getBytes());
+						reader.write("\r\n".getBytes());
 					}
 					if(personCard.getMobile()!=null && !personCard.getMobile().equals("")){
-						reader.write("TEL;CELL:"+personCard.getMobile());
-						reader.write("\r\n");
+						reader.write(("TEL;CELL:"+personCard.getMobile()).getBytes());
+						reader.write("\r\n".getBytes());
 					}
 					/*if(!personCard.getAddress().equals("")){
 						content = content+"ADR;HOME;POSTAL:;;"+personCard.getAddress()+";;;;\n";
 					}*/
 					if(personCard.getMail()!=null && !personCard.getMail().equals("")){
-						reader.write("EMAIL:"+personCard.getMail());
-						reader.write("\r\n");
+						reader.write(("EMAIL:"+personCard.getMail()).getBytes());
+						reader.write("\r\n".getBytes());
 					}
 					if(personCard.getQq()!=null && !personCard.getQq().equals("")){
-						reader.write("X-QQ:"+personCard.getQq());
-						reader.write("\r\n");
+						reader.write(("X-QQ:"+personCard.getQq()).getBytes());
+						reader.write("\r\n".getBytes());
 					}
 					if(personCard.getWeixin()!=null && !personCard.getWeixin().equals("")){
-						reader.write("NOTE:微信:"+personCard.getWeixin());
-						reader.write("\r\n");
+						reader.write(("NOTE:微信:"+personCard.getWeixin()).getBytes());
+						reader.write("\r\n".getBytes());
 					}
 					if(personCard.getBirthday()!=null && !personCard.getBirthday().equals("")){
-						reader.write("BDAY:"+DateTools.formatDate(personCard.getBirthday()));
-						reader.write("\r\n");
+						reader.write(("BDAY:"+DateTools.formatDate(personCard.getBirthday())).getBytes());
+						reader.write("\r\n".getBytes());
 					}
 					if(personCard.getId()!=null && !personCard.getId().equals("")){
 						List<Identity> identityList = referenceIdentity(business,flag);
@@ -106,8 +99,8 @@ class ActionExportVcf extends BaseAction {
 								if (null == unit) {
 									throw new ExceptionEntityNotExist(flag);
 								}
-								reader.write("ORG:"+unit.getName());
-								reader.write("\r\n");
+								reader.write(("ORG:"+unit.getName()).getBytes());
+								reader.write("\r\n".getBytes());
 							}
 						}else{
 							String orgString = "";
@@ -124,45 +117,41 @@ class ActionExportVcf extends BaseAction {
 								}
 							}
 							if(!orgString.equals("")){
-								reader.write("ORG:"+orgString);
-								reader.write("\r\n");
+								reader.write(("ORG:"+orgString).getBytes());
+								reader.write("\r\n".getBytes());
 							}
 							if(!titleString.equals("")){
-								reader.write("TITLE:"+titleString);
-								reader.write("\r\n");
+								reader.write(("TITLE:"+titleString).getBytes());
+								reader.write("\r\n".getBytes());
 							}
 						}	
 					}
 					
-					reader.write("END:VCARD");
-					reader.write("\r\n");
+					reader.write("END:VCARD".getBytes());
+					reader.write("\r\n".getBytes());
 					
 				}
-				reader.flush();
-				reader.close();
-				result.setData(path);
+				String fileName = "export_person_contacts"+System.currentTimeMillis()+".vcf";
+				try {
+					wo = new Wo(reader.toByteArray(), 
+							this.contentType(false, fileName), 
+							this.contentDisposition(false, fileName));
+				} finally {
+					reader.close();
+				}
+				result.setData(wo);
 			}
 
 			return result;
 		}
 	}
 	
-	public static class Wi extends GsonPropertyObject {
-
-		@FieldDescribe("通讯录人员标识")
-		private List<String> personList = new ArrayList<>();
+	public static class Wo extends WoFile {
 
-		public List<String> getPersonList() {
-			return personList;
+		public Wo(byte[] bytes, String contentType, String contentDisposition) {
+			super(bytes, contentType, contentDisposition);
 		}
 
-		public void setPersonList(List<String> personList) {
-			this.personList = personList;
-		}
-
-	}
-	public static class Wo extends WoId {
-		
 	}
 	
 	public List<Identity> referenceIdentity(Business business,String flag) throws Exception {

+ 10 - 27
o2server/x_organization_assemble_control/src/main/java/com/x/organization/assemble/control/jaxrs/personcard/PersonCardAction.java

@@ -215,15 +215,15 @@ public class PersonCardAction extends StandardJaxrsAction {
 	}
 	
 	@JaxrsMethodDescribe(value = "导出组织通讯录vcf", action = ActionExportVcf.class) 
-	@POST
-	@Path("listVCf")
-	@Produces(HttpMediaType.APPLICATION_JSON_UTF_8)
+	@GET
+	@Path("listVCf/{idList}")
 	@Consumes(MediaType.APPLICATION_JSON)
-	public void listVCf(@Suspended final AsyncResponse asyncResponse, @Context HttpServletRequest request,JsonElement jsonElement) {
-		ActionResult<String> result = new ActionResult<>(); 
+	public void listVCf(@Suspended final AsyncResponse asyncResponse, @Context HttpServletRequest request,
+			@JaxrsParameterDescribe("名片id组") @PathParam("idList") String idList) {
+		ActionResult<ActionExportVcf.Wo> result = new ActionResult<>(); 
 		EffectivePerson effectivePerson = this.effectivePerson(request);
 		try {
-			result = new ActionExportVcf().exportVcf(effectivePerson, jsonElement);
+			result = new ActionExportVcf().exportVcf(effectivePerson, idList);
 		} catch (Exception e) {
 			logger.error(e, effectivePerson, request, null);  
 			result.error(e);
@@ -231,33 +231,16 @@ public class PersonCardAction extends StandardJaxrsAction {
 		asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result));
 	}
 	
-	/*@JaxrsMethodDescribe(value = "导出个人通讯录vcf", action = ActionExportPersonalVcf.class) 
-	@POST
-	@Path("listPersonalVCf")
-	@Produces(HttpMediaType.APPLICATION_JSON_UTF_8)
-	@Consumes(MediaType.APPLICATION_JSON)
-	public void listPersonalVCf(@Suspended final AsyncResponse asyncResponse, @Context HttpServletRequest request,JsonElement jsonElement) {
-		ActionResult<String> result = new ActionResult<>(); 
-		EffectivePerson effectivePerson = this.effectivePerson(request);
-		try {
-			result = new ActionExportPersonalVcf().exportPersonalVcf(effectivePerson, jsonElement);
-		} catch (Exception e) {
-			logger.error(e, effectivePerson, request, null);  
-			result.error(e);
-		}
-		asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result));
-	}*/
-	@JaxrsMethodDescribe(value = "导出个人通讯录vcf", action = ActionExportPersonalVcf2.class) 
+	@JaxrsMethodDescribe(value = "导出个人通讯录vcf", action = ActionExportPersonalVcf.class) 
 	@GET
 	@Path("listPersonalVCf/{idList}")
-	@Produces(HttpMediaType.APPLICATION_JSON_UTF_8)
 	@Consumes(MediaType.APPLICATION_JSON)
-	public void listPersonalVCf2(@Suspended final AsyncResponse asyncResponse, @Context HttpServletRequest request,
+	public void listPersonalVCf(@Suspended final AsyncResponse asyncResponse, @Context HttpServletRequest request,
 			@JaxrsParameterDescribe("名片id组") @PathParam("idList") String idList) {
-		ActionResult<ActionExportPersonalVcf2.Wo> result = new ActionResult<>(); 
+		ActionResult<ActionExportPersonalVcf.Wo> result = new ActionResult<>(); 
 		EffectivePerson effectivePerson = this.effectivePerson(request);
 		try {
-			result = new ActionExportPersonalVcf2().exportPersonalVcf(effectivePerson, idList);
+			result = new ActionExportPersonalVcf().exportPersonalVcf(effectivePerson, idList);
 		} catch (Exception e) {
 			logger.error(e, effectivePerson, request, null);  
 			result.error(e);